Skip to main content

try_clone_with_clone

Attribute Macro try_clone_with_clone 

#[try_clone_with_clone]
Expand description

This macro attribute implements the TryClone trait for the given struct or enum, and uses it to implement the Clone trait.

It accepts a single argument, which is the type name of error type to use with the TryClone trait.

§Example

This definition:

pub struct MyError;

#[try_clone_with_clone(MyError)]
struct MyStruct {
    a: u32,
    b: bool,
}

will generate the following code, in addition to the struct definition:

impl TryClone for MyStruct {
    type Error = MyError;

    fn try_clone(&self) -> Result<Self, Self::Error> {
        Ok(MyStruct {
            a: TryClone::try_clone(&self.a)?,
            b: TryClone::try_clone(&self.b)?,
        })
    }
}

impl Clone for MyStruct {
    fn clone(&self) -> Self {
        TryClone::try_clone(self).expect("[MyStruct::try_clone] should not fail.")
    }
}