pub trait CopyType: Sized {
    type Copy: CopySelector;
}
Expand description

Marker trait for data specifying whether it is zero-copy or deep-copy.

The trait comes in two flavors: CopySelector<Type=Zero> and CopySelector<Type=Deep>. To each of these flavors corresponds two dependent traits, ZeroCopy (which requires implementing MaxSizeOf) and DeepCopy, which are automatically implemented.

use epserde::traits::*;

struct MyType {}

impl CopyType for MyType {
    type Copy = Deep;
}
// Now MyType implements DeepCopy

You should not implement this trait manually, but rather use the provided derive macro.

We use this trait to implement a different behavior for ZeroCopy and DeepCopy types, in particular on arrays, vectors, and boxed slices, working around the bug that prevents the compiler from understanding that implementations for the two flavors of CopySelector are mutually exclusive.

For an array of elements of type T to be zero-copy serializable and deserializable, T must implement CopySelector<Type=Zero>. The conditions for this marker trait are that T is a copy type, that it has a fixed memory layout, and that it does not contain any reference (in particular, that it has 'static lifetime). If this happen vectors of T or boxed slices of T can be ε-copy deserialized using a reference to a slice of T.

You can make zero-copy your own types, but you must ensure that they do not contain references and that they have a fixed memory layout; for structures, this requires repr(C). ε-serde will track these conditions at compile time and check them at runtime: in case of failure, serialization will panic.

Since we cannot use negative trait bounds, every type that is used as a parameter of an array, vector or boxed slice must implement either CopySelector<Type=Zero> or CopySelector<Type=Deep>. In the latter case, slices will be deserialized element by element, and the result will be a fully deserialized vector or boxed slice. If you do not implement either of these traits, the type will not be serializable inside vectors or boxed slices but error messages will be very unhelpful due to the contrived way we have to implement mutually exclusive types.

If you use the provided derive macros all this logic will be hidden from you. You’ll just have to add #[zero_copy] to your structures (if you want them to be zero-copy) and ε-serde will do the rest.

Required Associated Types§

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl CopyType for bool

§

type Copy = Zero

source§

impl CopyType for char

§

type Copy = Zero

source§

impl CopyType for f32

§

type Copy = Zero

source§

impl CopyType for f64

§

type Copy = Zero

source§

impl CopyType for i8

§

type Copy = Zero

source§

impl CopyType for i16

§

type Copy = Zero

source§

impl CopyType for i32

§

type Copy = Zero

source§

impl CopyType for i64

§

type Copy = Zero

source§

impl CopyType for i128

§

type Copy = Zero

source§

impl CopyType for isize

§

type Copy = Zero

source§

impl CopyType for u8

§

type Copy = Zero

source§

impl CopyType for u16

§

type Copy = Zero

source§

impl CopyType for u32

§

type Copy = Zero

source§

impl CopyType for u64

§

type Copy = Zero

source§

impl CopyType for u128

§

type Copy = Zero

source§

impl CopyType for ()

§

type Copy = Zero

source§

impl CopyType for usize

§

type Copy = Zero

source§

impl CopyType for Box<str>

§

type Copy = Deep

source§

impl CopyType for String

§

type Copy = Deep

source§

impl CopyType for NonZeroI8

§

type Copy = Zero

source§

impl CopyType for NonZeroI16

§

type Copy = Zero

source§

impl CopyType for NonZeroI32

§

type Copy = Zero

source§

impl CopyType for NonZeroI64

§

type Copy = Zero

source§

impl CopyType for NonZeroI128

§

type Copy = Zero

source§

impl CopyType for NonZeroIsize

§

type Copy = Zero

source§

impl CopyType for NonZeroU8

§

type Copy = Zero

source§

impl CopyType for NonZeroU16

§

type Copy = Zero

source§

impl CopyType for NonZeroU32

§

type Copy = Zero

source§

impl CopyType for NonZeroU64

§

type Copy = Zero

source§

impl CopyType for NonZeroU128

§

type Copy = Zero

source§

impl CopyType for NonZeroUsize

§

type Copy = Zero

source§

impl<T0: ZeroCopy, T1: ZeroCopy, T2: ZeroCopy, T3: ZeroCopy, T4: ZeroCopy, T5: ZeroCopy, T6: ZeroCopy, T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T1: ZeroCopy, T2: ZeroCopy, T3: ZeroCopy, T4: ZeroCopy, T5: ZeroCopy, T6: ZeroCopy, T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T2: ZeroCopy, T3: ZeroCopy, T4: ZeroCopy, T5: ZeroCopy, T6: ZeroCopy, T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T2, T3, T4, T5, T6, T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T3: ZeroCopy, T4: ZeroCopy, T5: ZeroCopy, T6: ZeroCopy, T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T3, T4, T5, T6, T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T4: ZeroCopy, T5: ZeroCopy, T6: ZeroCopy, T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T4, T5, T6, T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T5: ZeroCopy, T6: ZeroCopy, T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T5, T6, T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T6: ZeroCopy, T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T6, T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T7: ZeroCopy, T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T7, T8, T9, T10)

§

type Copy = Zero

source§

impl<T8: ZeroCopy, T9: ZeroCopy, T10: ZeroCopy> CopyType for (T8, T9, T10)

§

type Copy = Zero

source§

impl<T9: ZeroCopy, T10: ZeroCopy> CopyType for (T9, T10)

§

type Copy = Zero

source§

impl<T10: ZeroCopy> CopyType for (T10,)

§

type Copy = Zero

source§

impl<T> CopyType for Option<T>

§

type Copy = Deep

source§

impl<T> CopyType for Box<[T]>

§

type Copy = Deep

source§

impl<T> CopyType for Vec<T>

§

type Copy = Deep

source§

impl<T: CopyType, const N: usize> CopyType for [T; N]

§

type Copy = <T as CopyType>::Copy

source§

impl<T: ?Sized> CopyType for PhantomData<T>

§

type Copy = Zero

Implementors§