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.

Object Safety§

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§