diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:32 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:32 +0000 |
commit | 4547b622d8d29df964fa2914213088b148c498fc (patch) | |
tree | 9fc6b25f3c3add6b745be9a2400a6e96140046e9 /vendor/yoke/src/zero_from.rs | |
parent | Releasing progress-linux version 1.66.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-4547b622d8d29df964fa2914213088b148c498fc.tar.xz rustc-4547b622d8d29df964fa2914213088b148c498fc.zip |
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/yoke/src/zero_from.rs')
-rw-r--r-- | vendor/yoke/src/zero_from.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/vendor/yoke/src/zero_from.rs b/vendor/yoke/src/zero_from.rs new file mode 100644 index 000000000..679a28d59 --- /dev/null +++ b/vendor/yoke/src/zero_from.rs @@ -0,0 +1,54 @@ +// 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<T> +where + T: ZeroFrom<'zf, C>, +{ + #[inline] + fn zero_from(cart: &'zf C) -> Self { + YokeTraitHack(T::zero_from(cart)) + } +} + +impl<Y, C> Yoke<Y, C> +where + Y: for<'a> Yokeable<'a>, + for<'a> YokeTraitHack<<Y as Yokeable<'a>>::Output>: ZeroFrom<'a, <C as Deref>::Target>, + C: StableDeref + Deref, +{ + /// Construct a [`Yoke`]`<Y, C>` 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`]`<C::Target>`. 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::<Cow<'static, str>, 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::<Y, C>::attach_to_cart(cart, |c| { + YokeTraitHack::<<Y as Yokeable>::Output>::zero_from(c).0 + }) + } +} |