Coverage Report

Created: 2025-11-09 06:34

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/boxed.rs
Line
Count
Source
1
//! Implement Fallible Box
2
use super::TryClone;
3
use crate::TryReserveError;
4
use alloc::boxed::Box;
5
use core::borrow::Borrow;
6
use core::mem::ManuallyDrop;
7
use core::ops::Deref;
8
9
/// trait to implement Fallible Box
10
pub trait FallibleBox<T> {
11
    /// try creating a new box, returning a Result<Box<T>,
12
    /// TryReserveError> if allocation failed
13
    fn try_new(t: T) -> Result<Self, TryReserveError>
14
    where
15
        Self: Sized;
16
}
17
/// TryBox is a thin wrapper around alloc::boxed::Box to provide support for
18
/// fallible allocation.
19
///
20
/// See the crate documentation for more.
21
pub struct TryBox<T> {
22
    inner: Box<T>,
23
}
24
25
impl<T> TryBox<T> {
26
    #[inline]
27
652
    pub fn try_new(t: T) -> Result<Self, TryReserveError> {
28
        Ok(Self {
29
652
            inner: <Box<T> as FallibleBox<T>>::try_new(t)?,
30
        })
31
652
    }
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseParser>>::try_new
Line
Count
Source
27
274
    pub fn try_new(t: T) -> Result<Self, TryReserveError> {
28
        Ok(Self {
29
274
            inner: <Box<T> as FallibleBox<T>>::try_new(t)?,
30
        })
31
274
    }
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseAvifParser>>::try_new
Line
Count
Source
27
378
    pub fn try_new(t: T) -> Result<Self, TryReserveError> {
28
        Ok(Self {
29
378
            inner: <Box<T> as FallibleBox<T>>::try_new(t)?,
30
        })
31
378
    }
Unexecuted instantiation: <fallible_collections::boxed::TryBox<_>>::try_new
32
33
    #[inline(always)]
34
652
    pub fn into_raw(b: TryBox<T>) -> *mut T {
35
652
        Box::into_raw(b.inner)
36
652
    }
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseParser>>::into_raw
Line
Count
Source
34
274
    pub fn into_raw(b: TryBox<T>) -> *mut T {
35
274
        Box::into_raw(b.inner)
36
274
    }
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseAvifParser>>::into_raw
Line
Count
Source
34
378
    pub fn into_raw(b: TryBox<T>) -> *mut T {
35
378
        Box::into_raw(b.inner)
36
378
    }
Unexecuted instantiation: <fallible_collections::boxed::TryBox<_>>::into_raw
37
38
    /// # Safety
39
    ///
40
    /// See std::boxed::from_raw
41
    #[inline(always)]
42
652
    pub unsafe fn from_raw(raw: *mut T) -> Self {
43
652
        Self {
44
652
            inner: Box::from_raw(raw),
45
652
        }
46
652
    }
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseParser>>::from_raw
Line
Count
Source
42
274
    pub unsafe fn from_raw(raw: *mut T) -> Self {
43
274
        Self {
44
274
            inner: Box::from_raw(raw),
45
274
        }
46
274
    }
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseAvifParser>>::from_raw
Line
Count
Source
42
378
    pub unsafe fn from_raw(raw: *mut T) -> Self {
43
378
        Self {
44
378
            inner: Box::from_raw(raw),
45
378
        }
46
378
    }
Unexecuted instantiation: <fallible_collections::boxed::TryBox<_>>::from_raw
47
}
48
49
impl<T: TryClone> TryClone for TryBox<T> {
50
0
    fn try_clone(&self) -> Result<Self, TryReserveError> {
51
0
        let clone: T = (*self.inner).try_clone()?;
52
0
        Self::try_new(clone)
53
0
    }
54
}
55
56
impl<T> Deref for TryBox<T> {
57
    type Target = T;
58
59
    #[inline(always)]
60
0
    fn deref(&self) -> &T {
61
0
        self.inner.deref()
62
0
    }
63
}
64
65
impl<T> FallibleBox<T> for Box<T> {
66
652
    fn try_new(t: T) -> Result<Self, TryReserveError> {
67
652
        let mut vec = alloc::vec::Vec::new();
68
652
        vec.try_reserve_exact(1)?;
69
652
        vec.push(t);
70
        // try_reserve_exact doesn't promise the exact size, but into_boxed_slice does.
71
        // in practice the size is going to be okay anyway, so it won't realloc.
72
652
        let ptr: *mut T = ManuallyDrop::new(vec.into_boxed_slice()).as_mut_ptr();
73
652
        Ok(unsafe { Box::from_raw(ptr) })
74
652
    }
<alloc::boxed::Box<mp4parse_capi::Mp4parseParser> as fallible_collections::boxed::FallibleBox<mp4parse_capi::Mp4parseParser>>::try_new
Line
Count
Source
66
274
    fn try_new(t: T) -> Result<Self, TryReserveError> {
67
274
        let mut vec = alloc::vec::Vec::new();
68
274
        vec.try_reserve_exact(1)?;
69
274
        vec.push(t);
70
        // try_reserve_exact doesn't promise the exact size, but into_boxed_slice does.
71
        // in practice the size is going to be okay anyway, so it won't realloc.
72
274
        let ptr: *mut T = ManuallyDrop::new(vec.into_boxed_slice()).as_mut_ptr();
73
274
        Ok(unsafe { Box::from_raw(ptr) })
74
274
    }
<alloc::boxed::Box<mp4parse_capi::Mp4parseAvifParser> as fallible_collections::boxed::FallibleBox<mp4parse_capi::Mp4parseAvifParser>>::try_new
Line
Count
Source
66
378
    fn try_new(t: T) -> Result<Self, TryReserveError> {
67
378
        let mut vec = alloc::vec::Vec::new();
68
378
        vec.try_reserve_exact(1)?;
69
378
        vec.push(t);
70
        // try_reserve_exact doesn't promise the exact size, but into_boxed_slice does.
71
        // in practice the size is going to be okay anyway, so it won't realloc.
72
378
        let ptr: *mut T = ManuallyDrop::new(vec.into_boxed_slice()).as_mut_ptr();
73
378
        Ok(unsafe { Box::from_raw(ptr) })
74
378
    }
Unexecuted instantiation: <alloc::boxed::Box<_> as fallible_collections::boxed::FallibleBox<_>>::try_new
75
}
76
77
impl<T: TryClone> TryClone for Box<T> {
78
    #[inline]
79
0
    fn try_clone(&self) -> Result<Self, TryReserveError> {
80
0
        <Self as FallibleBox<T>>::try_new(Borrow::<T>::borrow(self).try_clone()?)
81
0
    }
82
}
83
84
#[cfg(test)]
85
mod tests {
86
    use super::*;
87
    #[test]
88
    fn boxed() {
89
        let mut v = <Box<_> as FallibleBox<_>>::try_new(5).unwrap();
90
        assert_eq!(*v, 5);
91
        *v = 3;
92
        assert_eq!(*v, 3);
93
    }
94
    // #[test]
95
    // fn big_alloc() {
96
    //     let layout = Layout::from_size_align(1_000_000_000_000, 8).unwrap();
97
    //     let ptr = unsafe { alloc::alloc::alloc(layout) };
98
    //     assert!(ptr.is_null());
99
    // }
100
101
    #[test]
102
    fn trybox_zst() {
103
        let b = <Box<_> as FallibleBox<_>>::try_new(()).expect("ok");
104
        assert_eq!(b, Box::new(()));
105
    }
106
107
    struct NonCopyType;
108
109
    #[test]
110
    fn trybox_deref() {
111
        let try_box: TryBox<NonCopyType> = TryBox::try_new(NonCopyType {}).unwrap();
112
        let _derefed: &NonCopyType = try_box.deref();
113
    }
114
115
    #[test]
116
    fn trybox_as_deref() {
117
        let try_box_option: Option<TryBox<NonCopyType>> =
118
            Some(TryBox::try_new(NonCopyType).unwrap());
119
        let _ref_option: Option<&NonCopyType> = try_box_option.as_deref();
120
    }
121
}