diff options
Diffstat (limited to 'third_party/rust/fallible_collections/src/arc.rs')
-rw-r--r-- | third_party/rust/fallible_collections/src/arc.rs | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/third_party/rust/fallible_collections/src/arc.rs b/third_party/rust/fallible_collections/src/arc.rs index 282b8e5555..26a6ec9407 100644 --- a/third_party/rust/fallible_collections/src/arc.rs +++ b/third_party/rust/fallible_collections/src/arc.rs @@ -1,15 +1,20 @@ //! Implement a Fallible Arc +#[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] use super::FallibleBox; use super::TryClone; - use crate::TryReserveError; + +#[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] use alloc::boxed::Box; use alloc::sync::Arc; /// trait to implement Fallible Arc -#[deprecated( - since = "0.3.1", - note = "⚠️️️this function is not completely fallible, it can panic !, see [issue](https://github.com/vcombey/fallible_collections/issues/13). help wanted" +#[cfg_attr( + any(not(feature = "unstable"), feature = "rust_1_57"), + deprecated( + since = "0.3.1", + note = "⚠️️️this function is not completely fallible, it can panic !, see [issue](https://github.com/vcombey/fallible_collections/issues/13). help wanted" + ) )] pub trait FallibleArc<T> { /// try creating a new Arc, returning a Result<Box<T>, @@ -22,10 +27,24 @@ pub trait FallibleArc<T> { #[allow(deprecated)] impl<T> FallibleArc<T> for Arc<T> { fn try_new(t: T) -> Result<Self, TryReserveError> { - // doesn't work as the inner variable of arc are also stocked in the box - - let b = <Box<T> as FallibleBox<T>>::try_new(t)?; - Ok(Arc::from(b)) + #[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] + { + // doesn't work as the inner variable of arc are also stocked in the box + let b = <Box<T> as FallibleBox<T>>::try_new(t)?; + Ok(Arc::from(b)) + } + #[cfg(all(feature = "unstable", not(feature = "rust_1_57")))] + { + use alloc::alloc::Layout; + use alloc::collections::TryReserveErrorKind; + Arc::try_new(t).map_err(|_e| { + TryReserveErrorKind::AllocError { + layout: Layout::new::<Arc<T>>(), // This is bullshit + non_exhaustive: (), + } + .into() + }) + } } } |