zerofrom

Trait ZeroFrom

Source
pub trait ZeroFrom<'zf, C: ?Sized>: 'zf {
    // Required method
    fn zero_from(other: &'zf C) -> Self;
}
Expand description

Trait for types that can be created from a reference to a different type C with no allocations, i.e. a zero-copy (zero-alloc) version of “From”

A type can be the ZeroFrom target of multiple other types.

The intention is for ZeroFrom to produce a struct from a other with as little work as possible. Although it is technically possible to implement ZeroFrom without being zero-copy (using heap allocations), doing so defeats the purpose of ZeroFrom.

For example, impl ZeroFrom<C> for Cow<str> should return a Cow::Borrowed pointing at data in the other type C, even if the other type is itself fully owned.

One can use the #[derive(ZeroFrom)] custom derive to automatically implement this trait.

§Examples

Implementing ZeroFrom on a custom data struct:

use std::borrow::Cow;
use zerofrom::ZeroFrom;

struct MyStruct<'data> {
    message: Cow<'data, str>,
}

// Reference from a borrowed version of self
impl<'zf> ZeroFrom<'zf, MyStruct<'_>> for MyStruct<'zf> {
    fn zero_from(other: &'zf MyStruct<'_>) -> Self {
        MyStruct {
            message: Cow::Borrowed(&other.message),
        }
    }
}

// Reference from a string slice directly
impl<'zf> ZeroFrom<'zf, str> for MyStruct<'zf> {
    fn zero_from(other: &'zf str) -> Self {
        MyStruct {
            message: Cow::Borrowed(other),
        }
    }
}

Required Methods§

Source

fn zero_from(other: &'zf C) -> Self

Clone the other C into a struct that may retain references into C.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a> ZeroFrom<'a, bool> for bool

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, char> for char

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, i8> for i8

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, i16> for i16

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, i32> for i32

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, i64> for i64

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, i128> for i128

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, isize> for isize

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, u8> for u8

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, u16> for u16

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, u32> for u32

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, u64> for u64

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, u128> for u128

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a> ZeroFrom<'a, usize> for usize

Source§

fn zero_from(this: &'a Self) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 1]> for [T; 1]

Source§

fn zero_from(this: &'a [C; 1]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 2]> for [T; 2]

Source§

fn zero_from(this: &'a [C; 2]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 3]> for [T; 3]

Source§

fn zero_from(this: &'a [C; 3]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 4]> for [T; 4]

Source§

fn zero_from(this: &'a [C; 4]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 5]> for [T; 5]

Source§

fn zero_from(this: &'a [C; 5]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 6]> for [T; 6]

Source§

fn zero_from(this: &'a [C; 6]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 7]> for [T; 7]

Source§

fn zero_from(this: &'a [C; 7]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 8]> for [T; 8]

Source§

fn zero_from(this: &'a [C; 8]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 9]> for [T; 9]

Source§

fn zero_from(this: &'a [C; 9]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 10]> for [T; 10]

Source§

fn zero_from(this: &'a [C; 10]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 11]> for [T; 11]

Source§

fn zero_from(this: &'a [C; 11]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 12]> for [T; 12]

Source§

fn zero_from(this: &'a [C; 12]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 13]> for [T; 13]

Source§

fn zero_from(this: &'a [C; 13]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 14]> for [T; 14]

Source§

fn zero_from(this: &'a [C; 14]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 15]> for [T; 15]

Source§

fn zero_from(this: &'a [C; 15]) -> Self

Source§

impl<'a, C, T: ZeroFrom<'a, C>> ZeroFrom<'a, [C; 16]> for [T; 16]

Source§

fn zero_from(this: &'a [C; 16]) -> Self

Source§

impl<'zf> ZeroFrom<'zf, str> for &'zf str

Source§

fn zero_from(other: &'zf str) -> Self

Source§

impl<'zf> ZeroFrom<'zf, str> for Cow<'zf, str>

Source§

fn zero_from(other: &'zf str) -> Self

Source§

impl<'zf> ZeroFrom<'zf, String> for &'zf str

Source§

fn zero_from(other: &'zf String) -> Self

Source§

impl<'zf> ZeroFrom<'zf, String> for Cow<'zf, str>

Source§

fn zero_from(other: &'zf String) -> Self

Source§

impl<'zf, B: ToOwned + ?Sized> ZeroFrom<'zf, Cow<'_, B>> for Cow<'zf, B>

Source§

fn zero_from(other: &'zf Cow<'_, B>) -> Self

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>> ZeroFrom<'zf, (C1, C2)> for (T1, T2)

Source§

fn zero_from(other: &'zf (C1, C2)) -> Self

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>> ZeroFrom<'zf, (C1, C2, C3)> for (T1, T2, T3)

Source§

fn zero_from(other: &'zf (C1, C2, C3)) -> Self

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>, C4, T4: ZeroFrom<'zf, C4>> ZeroFrom<'zf, (C1, C2, C3, C4)> for (T1, T2, T3, T4)

Source§

fn zero_from(other: &'zf (C1, C2, C3, C4)) -> Self

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>, C4, T4: ZeroFrom<'zf, C4>, C5, T5: ZeroFrom<'zf, C5>> ZeroFrom<'zf, (C1, C2, C3, C4, C5)> for (T1, T2, T3, T4, T5)

Source§

fn zero_from(other: &'zf (C1, C2, C3, C4, C5)) -> Self

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>, C4, T4: ZeroFrom<'zf, C4>, C5, T5: ZeroFrom<'zf, C5>, C6, T6: ZeroFrom<'zf, C6>> ZeroFrom<'zf, (C1, C2, C3, C4, C5, C6)> for (T1, T2, T3, T4, T5, T6)

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>, C4, T4: ZeroFrom<'zf, C4>, C5, T5: ZeroFrom<'zf, C5>, C6, T6: ZeroFrom<'zf, C6>, C7, T7: ZeroFrom<'zf, C7>> ZeroFrom<'zf, (C1, C2, C3, C4, C5, C6, C7)> for (T1, T2, T3, T4, T5, T6, T7)

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>, C4, T4: ZeroFrom<'zf, C4>, C5, T5: ZeroFrom<'zf, C5>, C6, T6: ZeroFrom<'zf, C6>, C7, T7: ZeroFrom<'zf, C7>, C8, T8: ZeroFrom<'zf, C8>> ZeroFrom<'zf, (C1, C2, C3, C4, C5, C6, C7, C8)> for (T1, T2, T3, T4, T5, T6, T7, T8)

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>, C4, T4: ZeroFrom<'zf, C4>, C5, T5: ZeroFrom<'zf, C5>, C6, T6: ZeroFrom<'zf, C6>, C7, T7: ZeroFrom<'zf, C7>, C8, T8: ZeroFrom<'zf, C8>, C9, T9: ZeroFrom<'zf, C9>> ZeroFrom<'zf, (C1, C2, C3, C4, C5, C6, C7, C8, C9)> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)

Source§

impl<'zf, C1, T1: ZeroFrom<'zf, C1>, C2, T2: ZeroFrom<'zf, C2>, C3, T3: ZeroFrom<'zf, C3>, C4, T4: ZeroFrom<'zf, C4>, C5, T5: ZeroFrom<'zf, C5>, C6, T6: ZeroFrom<'zf, C6>, C7, T7: ZeroFrom<'zf, C7>, C8, T8: ZeroFrom<'zf, C8>, C9, T9: ZeroFrom<'zf, C9>, C10, T10: ZeroFrom<'zf, C10>> ZeroFrom<'zf, (C1, C2, C3, C4, C5, C6, C7, C8, C9, C10)> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

Source§

impl<'zf, C, T: ZeroFrom<'zf, C>> ZeroFrom<'zf, Option<C>> for Option<T>

Source§

fn zero_from(other: &'zf Option<C>) -> Self

Source§

impl<'zf, T> ZeroFrom<'zf, [T]> for &'zf [T]

Source§

fn zero_from(other: &'zf [T]) -> &'zf [T]

Source§

impl<'zf, T: ?Sized + 'zf> ZeroFrom<'zf, PhantomData<T>> for PhantomData<T>

Source§

fn zero_from(other: &'zf PhantomData<T>) -> Self

Source§

impl<'zf, T: ?Sized> ZeroFrom<'zf, &T> for &'zf T

Source§

fn zero_from(other: &'zf &T) -> &'zf T

Implementors§