// This file is part of ICU4X. For terms of use, please see the file // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). use crate::trait_hack::YokeTraitHack; use crate::Yoke; use crate::Yokeable; use core::ops::Deref; use stable_deref_trait::StableDeref; use crate::ZeroFrom; impl<'zf, C: ?Sized, T> ZeroFrom<'zf, C> for YokeTraitHack where T: ZeroFrom<'zf, C>, { #[inline] fn zero_from(cart: &'zf C) -> Self { YokeTraitHack(T::zero_from(cart)) } } impl Yoke where Y: for<'a> Yokeable<'a>, for<'a> YokeTraitHack<>::Output>: ZeroFrom<'a, ::Target>, C: StableDeref + Deref, { /// Construct a [`Yoke`]`` from a cart implementing `StableDeref` by zero-copy cloning /// the cart to `Y` and then yokeing that object to the cart. /// /// The type `Y` must implement [`ZeroFrom`]``. This trait is auto-implemented /// on many common types and can be custom implemented or derived in order to make it easier /// to construct a `Yoke`. /// /// # Example /// /// Attach to a cart: /// /// ``` /// use std::borrow::Cow; /// use yoke::Yoke; /// /// let yoke = Yoke::, String>::attach_to_zero_copy_cart("demo".to_string()); /// /// assert_eq!("demo", yoke.get()); /// ``` pub fn attach_to_zero_copy_cart(cart: C) -> Self { Yoke::::attach_to_cart(cart, |c| { YokeTraitHack::<::Output>::zero_from(c).0 }) } }