trait-theories-std 0.1.0

A collection of invariants of Rust std traits.
Documentation
pub trait PureClone {
    fn pure_clone(&self) -> Self;
}

#[cfg(pure_clone)]
mod impls {
    use super::PureClone;

    macro_rules! impl_copy_pureclone {
        ($($ty: ty,)*) => {
            $(
                impl PureClone for $ty {
                    fn pure_clone(&self) -> Self {
                        *self
                    }
                }
            )
        };
    }

    impl_copy_pureclone!(
        u8, u16, u32, u64,
        i8, i16, i32, i64,
        char,
    );

    macro_rules! impl_tuple_pureclone {
        ($($p: ident $n: tt,)*) => {
            impl_tuple_pureclone!($($p $n)*);

            impl<$($p)*> PureClone for ($($p)*)
            where
                $($p: PureClone,)*
            {
                fn pure_clone(&self) -> Self {
                    ($(self.$n.clone(),)*)
                }
            }
        };
    }

    macro_rules! impl_tuple_pureclone_rec {
        () => {};
        ($p0: ident $n0: tt, $($pr : ident $nr : tt,)*) => {
            impl_tuple_pureclone_rec!($($pr $nr,)*);
            impl_tuple_pureclone!($p0 $n0, $($pr $nr,)*);
        };
    }

    impl_tuple_pureclone_rec!(E 11, D 10, C 9, B 8, A 7, Z 6, Y 5, X 4, W 3, V 2, U 1, T 0);
}

#[cfg(not(pure_clone))]
impl<T> PureClone for T
where
    T: Clone,
{
    fn pure_clone(&self) -> Self {
        self.clone()
    }
}