/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 | 649 | pub fn try_new(t: T) -> Result<Self, TryReserveError> { |
28 | | Ok(Self { |
29 | 649 | inner: <Box<T> as FallibleBox<T>>::try_new(t)?, |
30 | | }) |
31 | 649 | } <fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseParser>>::try_new Line | Count | Source | 27 | 309 | pub fn try_new(t: T) -> Result<Self, TryReserveError> { | 28 | | Ok(Self { | 29 | 309 | inner: <Box<T> as FallibleBox<T>>::try_new(t)?, | 30 | | }) | 31 | 309 | } |
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseAvifParser>>::try_new Line | Count | Source | 27 | 340 | pub fn try_new(t: T) -> Result<Self, TryReserveError> { | 28 | | Ok(Self { | 29 | 340 | inner: <Box<T> as FallibleBox<T>>::try_new(t)?, | 30 | | }) | 31 | 340 | } |
Unexecuted instantiation: <fallible_collections::boxed::TryBox<_>>::try_new |
32 | | |
33 | | #[inline(always)] |
34 | 649 | pub fn into_raw(b: TryBox<T>) -> *mut T { |
35 | 649 | Box::into_raw(b.inner) |
36 | 649 | } <fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseParser>>::into_raw Line | Count | Source | 34 | 309 | pub fn into_raw(b: TryBox<T>) -> *mut T { | 35 | 309 | Box::into_raw(b.inner) | 36 | 309 | } |
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseAvifParser>>::into_raw Line | Count | Source | 34 | 340 | pub fn into_raw(b: TryBox<T>) -> *mut T { | 35 | 340 | Box::into_raw(b.inner) | 36 | 340 | } |
Unexecuted instantiation: <fallible_collections::boxed::TryBox<_>>::into_raw |
37 | | |
38 | | /// # Safety |
39 | | /// |
40 | | /// See std::boxed::from_raw |
41 | | #[inline(always)] |
42 | 649 | pub unsafe fn from_raw(raw: *mut T) -> Self { |
43 | 649 | Self { |
44 | 649 | inner: Box::from_raw(raw), |
45 | 649 | } |
46 | 649 | } <fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseParser>>::from_raw Line | Count | Source | 42 | 309 | pub unsafe fn from_raw(raw: *mut T) -> Self { | 43 | 309 | Self { | 44 | 309 | inner: Box::from_raw(raw), | 45 | 309 | } | 46 | 309 | } |
<fallible_collections::boxed::TryBox<mp4parse_capi::Mp4parseAvifParser>>::from_raw Line | Count | Source | 42 | 340 | pub unsafe fn from_raw(raw: *mut T) -> Self { | 43 | 340 | Self { | 44 | 340 | inner: Box::from_raw(raw), | 45 | 340 | } | 46 | 340 | } |
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 | 649 | fn try_new(t: T) -> Result<Self, TryReserveError> { |
67 | 649 | let mut vec = alloc::vec::Vec::new(); |
68 | 649 | vec.try_reserve_exact(1)?; |
69 | 649 | 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 | 649 | let ptr: *mut T = ManuallyDrop::new(vec.into_boxed_slice()).as_mut_ptr(); |
73 | 649 | Ok(unsafe { Box::from_raw(ptr) }) |
74 | 649 | } <alloc::boxed::Box<mp4parse_capi::Mp4parseParser> as fallible_collections::boxed::FallibleBox<mp4parse_capi::Mp4parseParser>>::try_new Line | Count | Source | 66 | 309 | fn try_new(t: T) -> Result<Self, TryReserveError> { | 67 | 309 | let mut vec = alloc::vec::Vec::new(); | 68 | 309 | vec.try_reserve_exact(1)?; | 69 | 309 | 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 | 309 | let ptr: *mut T = ManuallyDrop::new(vec.into_boxed_slice()).as_mut_ptr(); | 73 | 309 | Ok(unsafe { Box::from_raw(ptr) }) | 74 | 309 | } |
<alloc::boxed::Box<mp4parse_capi::Mp4parseAvifParser> as fallible_collections::boxed::FallibleBox<mp4parse_capi::Mp4parseAvifParser>>::try_new Line | Count | Source | 66 | 340 | fn try_new(t: T) -> Result<Self, TryReserveError> { | 67 | 340 | let mut vec = alloc::vec::Vec::new(); | 68 | 340 | vec.try_reserve_exact(1)?; | 69 | 340 | 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 | 340 | let ptr: *mut T = ManuallyDrop::new(vec.into_boxed_slice()).as_mut_ptr(); | 73 | 340 | Ok(unsafe { Box::from_raw(ptr) }) | 74 | 340 | } |
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 | | } |