[][src]Derive Macro evmap_derive::ShallowCopy

#[derive(ShallowCopy)]

Implementation for #[derive(ShallowCopy)]

evmap provides the ShallowCopy trait, which allows you to cheaply alias types that don't otherwise implement Copy. Basic implementations are provided for common types such as String and Vec, but it must be implemented manually for structs using these types.

This macro attempts to simplify this task. It only works on types whose members all implement ShallowCopy. If this is not possible, consider using CopyValue, Box, or Arc instead.

Usage example

#[derive(ShallowCopy)]
struct Thing { field: i32 }

#[derive(ShallowCopy)]
struct Generic<T> { field: T }

#[derive(ShallowCopy)]
enum Things<T> { One(Thing), Two(Generic<T>) }

Generated implementations

The generated implementation calls shallow_copy on all the members of the type, and lifts the ManuallyDrop wrappers to the top-level return type.

For generic types, the derive adds ShallowCopy bounds to all the type parameters.

For instance, for the following code...

#[derive(ShallowCopy)]
struct Generic<T> { field: T }

...the derive generates...

impl<T: ShallowCopy> ShallowCopy for Generic<T> {
    unsafe fn shallow_copy(&self) -> ManuallyDrop<Self> {
        ManuallyDrop::new(Self {
            field: ManuallyDrop::into_inner(ShallowCopy::shallow_copy(&self.field))
        })
    }
}