Coverage Report

Created: 2025-10-10 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/fallible_collections-0.5.1/src/rc.rs
Line
Count
Source
1
//! Implement a Fallible Rc
2
use super::FallibleBox;
3
use crate::TryReserveError;
4
use alloc::boxed::Box;
5
use alloc::rc::Rc;
6
7
/// trait to implement Fallible Rc
8
#[cfg_attr(
9
    any(not(feature = "unstable"), feature = "rust_1_57"),
10
    deprecated(
11
        since = "0.4.9",
12
        note = "⚠️️️this function is not completely fallible, it can panic!, see [issue](https://github.com/vcombey/fallible_collections/issues/13). help wanted"
13
    )
14
)]
15
pub trait FallibleRc<T> {
16
    /// try creating a new Rc, returning a Result<Box<T>,
17
    /// TryReserveError> if allocation failed
18
    fn try_new(t: T) -> Result<Self, TryReserveError>
19
    where
20
        Self: Sized;
21
}
22
23
#[allow(deprecated)]
24
impl<T> FallibleRc<T> for Rc<T> {
25
0
    fn try_new(t: T) -> Result<Self, TryReserveError> {
26
0
        let b = <Box<T> as FallibleBox<T>>::try_new(t)?;
27
0
        Ok(Rc::from(b))
28
0
    }
29
}
30
31
#[cfg(test)]
32
mod test {
33
    #[test]
34
    fn fallible_rc() {
35
        use std::rc::Rc;
36
37
        let mut x = Rc::new(3);
38
        *Rc::get_mut(&mut x).unwrap() = 4;
39
        assert_eq!(*x, 4);
40
41
        let _y = Rc::clone(&x);
42
        assert!(Rc::get_mut(&mut x).is_none());
43
    }
44
}