diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/fallible_collections/src/arc.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/fallible_collections/src/arc.rs')
-rw-r--r-- | third_party/rust/fallible_collections/src/arc.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/third_party/rust/fallible_collections/src/arc.rs b/third_party/rust/fallible_collections/src/arc.rs new file mode 100644 index 0000000000..282b8e5555 --- /dev/null +++ b/third_party/rust/fallible_collections/src/arc.rs @@ -0,0 +1,52 @@ +//! Implement a Fallible Arc +use super::FallibleBox; +use super::TryClone; + +use crate::TryReserveError; +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" +)] +pub trait FallibleArc<T> { + /// try creating a new Arc, returning a Result<Box<T>, + /// TryReserveError> if allocation failed + fn try_new(t: T) -> Result<Self, TryReserveError> + where + Self: Sized; +} + +#[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)) + } +} + +/// Just a TryClone boilerplate for Arc +impl<T: ?Sized> TryClone for Arc<T> { + fn try_clone(&self) -> Result<Self, TryReserveError> { + Ok(self.clone()) + } +} + +#[cfg(test)] +mod test { + #[test] + fn fallible_rc() { + use std::sync::Arc; + + let mut x = Arc::new(3); + *Arc::get_mut(&mut x).unwrap() = 4; + assert_eq!(*x, 4); + + let _y = Arc::clone(&x); + assert!(Arc::get_mut(&mut x).is_none()); + } +} |