use super::FallibleBox;
use super::TryClone;
use alloc::boxed::Box;
use alloc::collections::TryReserveError;
use alloc::sync::Arc;
pub trait FallibleArc<T> {
fn try_new(t: T) -> Result<Self, TryReserveError>
where
Self: Sized;
}
impl<T> FallibleArc<T> for Arc<T> {
fn try_new(t: T) -> Result<Self, TryReserveError> {
let b = Box::try_new(t)?;
Ok(Arc::from(b))
}
}
impl<T: ?Sized> TryClone for Arc<T> {
fn try_clone(&self) -> Result<Self, alloc::collections::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());
}
}