/rust/registry/src/index.crates.io-6f17d22bba15001f/yoke-0.7.5/src/zero_from.rs
Line | Count | Source (jump to first uncovered line) |
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 | | |
5 | | use crate::trait_hack::YokeTraitHack; |
6 | | use crate::Yoke; |
7 | | use crate::Yokeable; |
8 | | |
9 | | use core::ops::Deref; |
10 | | use stable_deref_trait::StableDeref; |
11 | | |
12 | | use crate::ZeroFrom; |
13 | | |
14 | | impl<'zf, C: ?Sized, T> ZeroFrom<'zf, C> for YokeTraitHack<T> |
15 | | where |
16 | | T: ZeroFrom<'zf, C>, |
17 | | { |
18 | | #[inline] |
19 | 0 | fn zero_from(cart: &'zf C) -> Self { |
20 | 0 | YokeTraitHack(T::zero_from(cart)) |
21 | 0 | } |
22 | | } |
23 | | |
24 | | impl<Y, C> Yoke<Y, C> |
25 | | where |
26 | | Y: for<'a> Yokeable<'a>, |
27 | | for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: ZeroFrom<'a, <C as Deref>::Target>, |
28 | | C: StableDeref + Deref, |
29 | | <C as Deref>::Target: 'static, |
30 | | { |
31 | | /// Construct a [`Yoke`]`<Y, C>` from a cart implementing `StableDeref` by zero-copy cloning |
32 | | /// the cart to `Y` and then yokeing that object to the cart. |
33 | | /// |
34 | | /// The type `Y` must implement [`ZeroFrom`]`<C::Target>`. This trait is auto-implemented |
35 | | /// on many common types and can be custom implemented or derived in order to make it easier |
36 | | /// to construct a `Yoke`. |
37 | | /// |
38 | | /// # Example |
39 | | /// |
40 | | /// Attach to a cart: |
41 | | /// |
42 | | /// ``` |
43 | | /// use std::borrow::Cow; |
44 | | /// use yoke::Yoke; |
45 | | /// |
46 | | /// let yoke = Yoke::<Cow<'static, str>, String>::attach_to_zero_copy_cart( |
47 | | /// "demo".to_owned(), |
48 | | /// ); |
49 | | /// |
50 | | /// assert_eq!("demo", yoke.get()); |
51 | | /// ``` |
52 | 0 | pub fn attach_to_zero_copy_cart(cart: C) -> Self { |
53 | 0 | Yoke::<Y, C>::attach_to_cart(cart, |c| { |
54 | 0 | YokeTraitHack::<<Y as Yokeable>::Output>::zero_from(c).0 |
55 | 0 | }) |
56 | 0 | } |
57 | | } |