Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize Clone::clone_from #72

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub use crate::Either::{Left, Right};
/// preference.
/// (For representing success or error, use the regular `Result` enum instead.)
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Either<L, R> {
/// A value of type `L`.
Left(L),
Expand Down Expand Up @@ -129,6 +129,23 @@ macro_rules! try_right {
};
}

impl<L: Clone, R: Clone> Clone for Either<L, R> {
fn clone(&self) -> Self {
match self {
Left(inner) => Left(inner.clone()),
Right(inner) => Right(inner.clone()),
}
}

fn clone_from(&mut self, source: &Self) {
match (self, source) {
(Left(dest), Left(source)) => dest.clone_from(source),
(Right(dest), Right(source)) => dest.clone_from(source),
(dest, source) => *dest = source.clone(),
}
}
}

impl<L, R> Either<L, R> {
/// Return true if the value is the `Left` variant.
///
Expand Down