yoke/zero_from.rs
1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use crate::Yoke;
6use crate::Yokeable;
7
8use core::ops::Deref;
9use stable_deref_trait::StableDeref;
10
11use crate::ZeroFrom;
12
13impl<Y, C> Yoke<Y, C>
14where
15 Y: for<'a> Yokeable<'a>,
16 for<'a> <Y as Yokeable<'a>>::Output: ZeroFrom<'a, <C as Deref>::Target>,
17 C: StableDeref + Deref,
18 <C as Deref>::Target: 'static,
19{
20 /// Construct a [`Yoke`]`<Y, C>` from a cart implementing `StableDeref` by zero-copy cloning
21 /// the cart to `Y` and then yokeing that object to the cart.
22 ///
23 /// The type `Y` must implement [`ZeroFrom`]`<C::Target>`. This trait is auto-implemented
24 /// on many common types and can be custom implemented or derived in order to make it easier
25 /// to construct a `Yoke`.
26 ///
27 /// # Example
28 ///
29 /// Attach to a cart:
30 ///
31 /// ```
32 /// use std::borrow::Cow;
33 /// use yoke::Yoke;
34 ///
35 /// let yoke = Yoke::<Cow<'static, str>, String>::attach_to_zero_copy_cart(
36 /// "demo".to_owned(),
37 /// );
38 ///
39 /// assert_eq!("demo", yoke.get());
40 /// ```
41 pub fn attach_to_zero_copy_cart(cart: C) -> Self {
42 Yoke::<Y, C>::attach_to_cart(cart, |c| <Y as Yokeable>::Output::zero_from(c))
43 }
44}