Debian Codegraph @

third_party/rust/fallible_collections/src/lib.rs

raw
1 //! impl Fallible collections on allocation errors, quite as describe
2 //! in [RFC 2116](https://github.com/rust-lang/rfcs/blob/master/text/2116-alloc-me-maybe.md)
3 //! This was used in the turbofish OS hobby project to mitigate the
4 //! the lack of faillible allocation in rust.
5 //!
6 //! The `Try*` types in this module are thin wrappers around the stdlib types to add
7 //! support for fallible allocation. The API differences from the stdlib types ensure
8 //! that all operations which allocate return a `Result`. For the most part, this simply
9 //! means adding a `Result` return value to functions which return nothing or a
10 //! non-`Result` value. However, these types implement some traits whose API cannot
11 //! communicate failure, but which do require allocation, so it is important that these
12 //! wrapper types do not implement these traits.
13 //!
14 //! Specifically, these types must not implement any of the following traits:
15 //! - Clone
16 //! - Extend
17 //! - From
18 //! - FromIterator
19 //!
20 //! This list may not be exhaustive. Exercise caution when implementing
21 //! any new traits to ensure they won't potentially allocate in a way that
22 //! can't return a Result to indicate allocation failure.
23
24 #![cfg_attr(not(test), no_std)]
25 #![cfg_attr(feature = "unstable", feature(try_reserve_kind))]
26 #![cfg_attr(feature = "unstable", feature(min_specialization))]
27 #![cfg_attr(feature = "unstable", feature(allocator_api))]
28 #![cfg_attr(feature = "unstable", feature(dropck_eyepatch))]
29 #![cfg_attr(feature = "unstable", feature(ptr_internals))]
30 #![cfg_attr(feature = "unstable", feature(core_intrinsics))]
31 #![cfg_attr(feature = "unstable", feature(maybe_uninit_slice))]
32 #![cfg_attr(feature = "unstable", feature(maybe_uninit_uninit_array))]
33
34 #[cfg(all(feature = "unstable", feature = "rust_1_57"))]
35 compile_error!(
36 "The use of the 'unstable' feature combined with the \
37 'rust_1_57' feature, which is related to the partial stabilization \
38 of the allocator API since rustc version 1.57, does not make sense!"
39 );
40
41 extern crate alloc;
42 #[cfg(feature = "std")]
43 extern crate std;
44
45 pub mod boxed;
46 pub use boxed::*;
47 #[macro_use]
48 pub mod vec;
49 pub use vec::*;
50 pub mod rc;
51 pub use rc::*;
52 pub mod arc;
53 pub use arc::*;
54 #[cfg(feature = "unstable")]
55 pub mod btree;
56 #[cfg(all(feature = "hashmap", not(feature = "unstable")))]
57 pub mod hashmap;
58 #[cfg(all(feature = "hashmap", not(feature = "unstable")))]
59 pub use hashmap::*;
60 #[macro_use]
61 pub mod format;
62 pub mod try_clone;
63
64 pub mod try_reserve_error;
65 pub use try_reserve_error::TryReserveError;
66
67 #[cfg(feature = "std_io")]
68 pub use vec::std_io::*;
69
70 /// trait for trying to clone an elem, return an error instead of
71 /// panic if allocation failed
72 /// # Examples
73 ///
74 /// ```
75 /// use fallible_collections::TryClone;
76 /// let mut vec = vec![42, 100];
77 /// assert_eq!(vec.try_clone().unwrap(), vec)
78 /// ```
79 pub trait TryClone {
80 /// try clone method, (Self must be sized because of Result
81 /// constraint)
82 fn try_clone(&self) -> Result<Self, TryReserveError>
83 where
84 Self: core::marker::Sized;
85 }
86
87 #[cfg(feature = "rust_1_57")]
88 fn make_try_reserve_error(len: usize, additional: usize, elem_size: usize, align: usize) -> TryReserveError {
89 if let Some(size) = len.checked_add(additional).and_then(|l| l.checked_mul(elem_size)) {
90 if let Ok(layout) = alloc::alloc::Layout::from_size_align(size, align) {
91 return TryReserveError::AllocError { layout }
92 }
93 }
94 TryReserveError::CapacityOverflow
95 }