summaryrefslogtreecommitdiffstats
path: root/vendor/yoke/src/yoke.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:42 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:42 +0000
commitcec1877e180393eba0f6ddb0cf97bf3a791631c7 (patch)
tree47b4dac2a9dd9a40c30c251b4d4a72d7ccf77e9f /vendor/yoke/src/yoke.rs
parentAdding debian version 1.74.1+dfsg1-1. (diff)
downloadrustc-cec1877e180393eba0f6ddb0cf97bf3a791631c7.tar.xz
rustc-cec1877e180393eba0f6ddb0cf97bf3a791631c7.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/yoke/src/yoke.rs')
-rw-r--r--vendor/yoke/src/yoke.rs99
1 files changed, 68 insertions, 31 deletions
diff --git a/vendor/yoke/src/yoke.rs b/vendor/yoke/src/yoke.rs
index ef76aa86a..22f14d3dc 100644
--- a/vendor/yoke/src/yoke.rs
+++ b/vendor/yoke/src/yoke.rs
@@ -5,6 +5,7 @@
use crate::either::EitherCart;
#[cfg(feature = "alloc")]
use crate::erased::{ErasedArcCart, ErasedBoxCart, ErasedRcCart};
+use crate::kinda_sorta_dangling::KindaSortaDangling;
use crate::trait_hack::YokeTraitHack;
use crate::Yokeable;
use core::marker::PhantomData;
@@ -74,14 +75,39 @@ use alloc::sync::Arc;
/// assert_eq!(&**yoke.get(), "hello");
/// assert!(matches!(yoke.get(), &Cow::Borrowed(_)));
/// ```
-#[derive(Debug)]
pub struct Yoke<Y: for<'a> Yokeable<'a>, C> {
// must be the first field for drop order
// this will have a 'static lifetime parameter, that parameter is a lie
- yokeable: Y,
+ yokeable: KindaSortaDangling<Y>,
cart: C,
}
+// Manual `Debug` implementation, since the derived one would be unsound.
+// See https://github.com/unicode-org/icu4x/issues/3685
+impl<Y: for<'a> Yokeable<'a>, C: core::fmt::Debug> core::fmt::Debug for Yoke<Y, C>
+where
+ for<'a> <Y as Yokeable<'a>>::Output: core::fmt::Debug,
+{
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ f.debug_struct("Yoke")
+ .field("yokeable", self.get())
+ .field("cart", self.backing_cart())
+ .finish()
+ }
+}
+
+#[test]
+fn test_debug() {
+ let local_data = "foo".to_owned();
+ let y1 = Yoke::<alloc::borrow::Cow<'static, str>, Rc<String>>::attach_to_zero_copy_cart(
+ Rc::new(local_data),
+ );
+ assert_eq!(
+ format!("{y1:?}"),
+ r#"Yoke { yokeable: "foo", cart: "foo" }"#,
+ );
+}
+
impl<Y: for<'a> Yokeable<'a>, C: StableDeref> Yoke<Y, C>
where
<C as Deref>::Target: 'static,
@@ -129,7 +155,7 @@ where
{
let deserialized = f(cart.deref());
Self {
- yokeable: unsafe { Y::make(deserialized) },
+ yokeable: KindaSortaDangling::new(unsafe { Y::make(deserialized) }),
cart,
}
}
@@ -145,7 +171,7 @@ where
{
let deserialized = f(cart.deref())?;
Ok(Self {
- yokeable: unsafe { Y::make(deserialized) },
+ yokeable: KindaSortaDangling::new(unsafe { Y::make(deserialized) }),
cart,
})
}
@@ -418,7 +444,10 @@ impl<Y: for<'a> Yokeable<'a>> Yoke<Y, ()> {
/// assert_eq!(yoke.get(), "hello");
/// ```
pub fn new_always_owned(yokeable: Y) -> Self {
- Self { yokeable, cart: () }
+ Self {
+ yokeable: KindaSortaDangling::new(yokeable),
+ cart: (),
+ }
}
/// Obtain the yokeable out of a `Yoke<Y, ()>`
@@ -427,7 +456,7 @@ impl<Y: for<'a> Yokeable<'a>> Yoke<Y, ()> {
/// fine for `Yoke<Y, ()>` since there are no actual internal
/// references
pub fn into_yokeable(self) -> Y {
- self.yokeable
+ self.yokeable.into_inner()
}
}
@@ -456,9 +485,9 @@ impl<Y: for<'a> Yokeable<'a>, C: StableDeref> Yoke<Y, Option<C>> {
///
/// assert_eq!(yoke.get(), "hello");
/// ```
- pub fn new_owned(yokeable: Y) -> Self {
+ pub const fn new_owned(yokeable: Y) -> Self {
Self {
- yokeable,
+ yokeable: KindaSortaDangling::new(yokeable),
cart: None,
}
}
@@ -470,7 +499,7 @@ impl<Y: for<'a> Yokeable<'a>, C: StableDeref> Yoke<Y, Option<C>> {
pub fn try_into_yokeable(self) -> Result<Y, Self> {
match self.cart {
Some(_) => Err(self),
- None => Ok(self.yokeable),
+ None => Ok(self.yokeable.into_inner()),
}
}
}
@@ -516,7 +545,7 @@ where
// We have an &T not a T, and we can clone YokeTraitHack<T>
let this_hack = YokeTraitHack(this).into_ref();
Yoke {
- yokeable: unsafe { Y::make(this_hack.clone().0) },
+ yokeable: KindaSortaDangling::new(unsafe { Y::make(this_hack.clone().0) }),
cart: self.cart.clone(),
}
}
@@ -630,9 +659,9 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
PhantomData<&'a ()>,
) -> <P as Yokeable<'a>>::Output,
{
- let p = f(self.yokeable.transform_owned(), PhantomData);
+ let p = f(self.yokeable.into_inner().transform_owned(), PhantomData);
Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart,
}
}
@@ -653,7 +682,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
{
let p = f(self.get(), PhantomData);
Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart.clone(),
}
}
@@ -728,9 +757,9 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
PhantomData<&'a ()>,
) -> Result<<P as Yokeable<'a>>::Output, E>,
{
- let p = f(self.yokeable.transform_owned(), PhantomData)?;
+ let p = f(self.yokeable.into_inner().transform_owned(), PhantomData)?;
Ok(Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart,
})
}
@@ -751,7 +780,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
{
let p = f(self.get(), PhantomData)?;
Ok(Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart.clone(),
})
}
@@ -772,9 +801,13 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
where
P: for<'a> Yokeable<'a>,
{
- let p = f(self.yokeable.transform_owned(), capture, PhantomData);
+ let p = f(
+ self.yokeable.into_inner().transform_owned(),
+ capture,
+ PhantomData,
+ );
Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart,
}
}
@@ -799,7 +832,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
{
let p = f(self.get(), capture, PhantomData);
Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart.clone(),
}
}
@@ -822,9 +855,13 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
where
P: for<'a> Yokeable<'a>,
{
- let p = f(self.yokeable.transform_owned(), capture, PhantomData)?;
+ let p = f(
+ self.yokeable.into_inner().transform_owned(),
+ capture,
+ PhantomData,
+ )?;
Ok(Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart,
})
}
@@ -850,7 +887,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
{
let p = f(self.get(), capture, PhantomData)?;
Ok(Yoke {
- yokeable: unsafe { P::make(p) },
+ yokeable: KindaSortaDangling::new(unsafe { P::make(p) }),
cart: self.cart.clone(),
})
}
@@ -870,6 +907,8 @@ impl<Y: for<'a> Yokeable<'a>, C: 'static + Sized> Yoke<Y, Rc<C>> {
/// In case the cart type `C` is not already an `Rc<T>`, you can use
/// [`Yoke::wrap_cart_in_rc()`] to wrap it.
///
+ /// ✨ *Enabled with the `alloc` Cargo feature.*
+ ///
/// # Example
///
/// ```rust
@@ -891,8 +930,6 @@ impl<Y: for<'a> Yokeable<'a>, C: 'static + Sized> Yoke<Y, Rc<C>> {
///
/// // Now erased1 and erased2 have the same type!
/// ```
- ///
- /// Available with the `"alloc"` Cargo feature enabled.
pub fn erase_rc_cart(self) -> Yoke<Y, ErasedRcCart> {
unsafe {
// safe because the cart is preserved, just
@@ -916,6 +953,8 @@ impl<Y: for<'a> Yokeable<'a>, C: 'static + Sized + Send + Sync> Yoke<Y, Arc<C>>
/// In case the cart type `C` is not already an `Arc<T>`, you can use
/// [`Yoke::wrap_cart_in_arc()`] to wrap it.
///
+ /// ✨ *Enabled with the `alloc` Cargo feature.*
+ ///
/// # Example
///
/// ```rust
@@ -937,8 +976,6 @@ impl<Y: for<'a> Yokeable<'a>, C: 'static + Sized + Send + Sync> Yoke<Y, Arc<C>>
///
/// // Now erased1 and erased2 have the same type!
/// ```
- ///
- /// Available with the `"alloc"` Cargo feature enabled.
pub fn erase_arc_cart(self) -> Yoke<Y, ErasedArcCart> {
unsafe {
// safe because the cart is preserved, just
@@ -962,6 +999,8 @@ impl<Y: for<'a> Yokeable<'a>, C: 'static + Sized> Yoke<Y, Box<C>> {
/// In case the cart type `C` is not already `Box<T>`, you can use
/// [`Yoke::wrap_cart_in_box()`] to wrap it.
///
+ /// ✨ *Enabled with the `alloc` Cargo feature.*
+ ///
/// # Example
///
/// ```rust
@@ -983,8 +1022,6 @@ impl<Y: for<'a> Yokeable<'a>, C: 'static + Sized> Yoke<Y, Box<C>> {
///
/// // Now erased1 and erased2 have the same type!
/// ```
- ///
- /// Available with the `"alloc"` Cargo feature enabled.
pub fn erase_box_cart(self) -> Yoke<Y, ErasedBoxCart> {
unsafe {
// safe because the cart is preserved, just
@@ -999,7 +1036,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
/// Helper function allowing one to wrap the cart type `C` in a `Box<T>`.
/// Can be paired with [`Yoke::erase_box_cart()`]
///
- /// Available with the `"alloc"` Cargo feature enabled.
+ /// ✨ *Enabled with the `alloc` Cargo feature.*
#[inline]
pub fn wrap_cart_in_box(self) -> Yoke<Y, Box<C>> {
unsafe {
@@ -1011,7 +1048,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
/// Can be paired with [`Yoke::erase_rc_cart()`], or generally used
/// to make the [`Yoke`] cloneable.
///
- /// Available with the `"alloc"` Cargo feature enabled.
+ /// ✨ *Enabled with the `alloc` Cargo feature.*
#[inline]
pub fn wrap_cart_in_rc(self) -> Yoke<Y, Rc<C>> {
unsafe {
@@ -1023,7 +1060,7 @@ impl<Y: for<'a> Yokeable<'a>, C> Yoke<Y, C> {
/// Can be paired with [`Yoke::erase_arc_cart()`], or generally used
/// to make the [`Yoke`] cloneable.
///
- /// Available with the `"alloc"` Cargo feature enabled.
+ /// ✨ *Enabled with the `alloc` Cargo feature.*
#[inline]
pub fn wrap_cart_in_arc(self) -> Yoke<Y, Arc<C>> {
unsafe {