Trait yoke::ZeroCopyFrom [−][src]
pub trait ZeroCopyFrom<C: ?Sized>: for<'a> Yokeable<'a> { fn zero_copy_from<'b>(cart: &'b C) -> <Self as Yokeable<'b>>::Output; }
Expand description
Trait for types that can be crated from a reference to a cart type C
with no allocations.
A type can be the ZeroCopyFrom
target of multiple cart types.
The intention is for ZeroCopyFrom
to produce a struct from a cart with as little work as
possible. Although it is technically possible to implement ZeroCopyFrom
without being
zero-copy (using heap allocations), doing so defeats the purpose of ZeroCopyFrom
.
For example, impl ZeroCopyFrom<C> for Cow<str>
should return a Cow::Borrowed
pointing at
data in the cart C
, even if the cart is itself fully owned.
Examples
Implementing ZeroCopyFrom
on a custom data struct:
use yoke::Yokeable; use yoke::ZeroCopyFrom; use std::borrow::Cow; struct MyStruct<'s> { message: Cow<'s, str>, } unsafe impl<'a> Yokeable<'a> for MyStruct<'static> { // (not shown; see `Yokeable` for examples) } // Reference from a borrowed version of self impl<'s> ZeroCopyFrom<MyStruct<'s>> for MyStruct<'static> { fn zero_copy_from<'b>(cart: &'b MyStruct<'s>) -> MyStruct<'b> { MyStruct { message: Cow::Borrowed(&cart.message) } } } // Reference from a string slice directly impl ZeroCopyFrom<str> for MyStruct<'static> { fn zero_copy_from<'b>(cart: &'b str) -> MyStruct<'b> { MyStruct { message: Cow::Borrowed(cart) } } }