/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.14.5/src/raw/alloc.rs
Line | Count | Source |
1 | | pub(crate) use self::inner::{do_alloc, Allocator, Global}; |
2 | | |
3 | | // Nightly-case. |
4 | | // Use unstable `allocator_api` feature. |
5 | | // This is compatible with `allocator-api2` which can be enabled or not. |
6 | | // This is used when building for `std`. |
7 | | #[cfg(feature = "nightly")] |
8 | | mod inner { |
9 | | use crate::alloc::alloc::Layout; |
10 | | pub use crate::alloc::alloc::{Allocator, Global}; |
11 | | use core::ptr::NonNull; |
12 | | |
13 | | #[allow(clippy::map_err_ignore)] |
14 | | pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> { |
15 | | match alloc.allocate(layout) { |
16 | | Ok(ptr) => Ok(ptr.as_non_null_ptr()), |
17 | | Err(_) => Err(()), |
18 | | } |
19 | | } |
20 | | } |
21 | | |
22 | | // Basic non-nightly case. |
23 | | // This uses `allocator-api2` enabled by default. |
24 | | // If any crate enables "nightly" in `allocator-api2`, |
25 | | // this will be equivalent to the nightly case, |
26 | | // since `allocator_api2::alloc::Allocator` would be re-export of |
27 | | // `core::alloc::Allocator`. |
28 | | #[cfg(all(not(feature = "nightly"), feature = "allocator-api2"))] |
29 | | mod inner { |
30 | | use crate::alloc::alloc::Layout; |
31 | | pub use allocator_api2::alloc::{Allocator, Global}; |
32 | | use core::ptr::NonNull; |
33 | | |
34 | | #[allow(clippy::map_err_ignore)] |
35 | | pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> { |
36 | | match alloc.allocate(layout) { |
37 | | Ok(ptr) => Ok(ptr.cast()), |
38 | | Err(_) => Err(()), |
39 | | } |
40 | | } |
41 | | } |
42 | | |
43 | | // No-defaults case. |
44 | | // When building with default-features turned off and |
45 | | // neither `nightly` nor `allocator-api2` is enabled, |
46 | | // this will be used. |
47 | | // Making it impossible to use any custom allocator with collections defined |
48 | | // in this crate. |
49 | | // Any crate in build-tree can enable `allocator-api2`, |
50 | | // or `nightly` without disturbing users that don't want to use it. |
51 | | #[cfg(not(any(feature = "nightly", feature = "allocator-api2")))] |
52 | | mod inner { |
53 | | use crate::alloc::alloc::{alloc, dealloc, Layout}; |
54 | | use core::ptr::NonNull; |
55 | | |
56 | | #[allow(clippy::missing_safety_doc)] // not exposed outside of this crate |
57 | | pub unsafe trait Allocator { |
58 | | fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()>; |
59 | | unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout); |
60 | | } |
61 | | |
62 | | #[derive(Copy, Clone)] |
63 | | pub struct Global; |
64 | | |
65 | | unsafe impl Allocator for Global { |
66 | | #[inline] |
67 | 0 | fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()> { |
68 | 0 | unsafe { NonNull::new(alloc(layout)).ok_or(()) } |
69 | 0 | } |
70 | | #[inline] |
71 | 0 | unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { |
72 | 0 | dealloc(ptr.as_ptr(), layout); |
73 | 0 | } |
74 | | } |
75 | | |
76 | | impl Default for Global { |
77 | | #[inline] |
78 | 0 | fn default() -> Self { |
79 | 0 | Global |
80 | 0 | } |
81 | | } |
82 | | |
83 | 0 | pub(crate) fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> { |
84 | 0 | alloc.allocate(layout) |
85 | 0 | } Unexecuted instantiation: hashbrown::raw::inner::alloc::inner::do_alloc::<hashbrown::raw::inner::alloc::inner::Global> Unexecuted instantiation: hashbrown::raw::inner::alloc::inner::do_alloc::<hashbrown::raw::inner::alloc::inner::Global> Unexecuted instantiation: hashbrown::raw::inner::alloc::inner::do_alloc::<hashbrown::raw::inner::alloc::inner::Global> Unexecuted instantiation: hashbrown::raw::inner::alloc::inner::do_alloc::<_> |
86 | | } |