Skip to content

Commit

Permalink
Merge pull request #72 from cuviper/clone_from
Browse files Browse the repository at this point in the history
Specialize `Clone::clone_from`
  • Loading branch information
cuviper committed Jun 28, 2022
2 parents 26a6dc7 + f90883c commit f1eca77
Showing 1 changed file with 18 additions and 1 deletion.
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

0 comments on commit f1eca77

Please sign in to comment.