musli/alloc/
disabled.rs

1use core::marker::PhantomData;
2use core::ptr;
3
4use super::{Allocator, RawVec};
5
6/// An empty buffer.
7pub struct EmptyBuf<T> {
8    _marker: PhantomData<T>,
9}
10
11impl<T> RawVec<T> for EmptyBuf<T> {
12    #[inline]
13    fn resize(&mut self, _: usize, _: usize) -> bool {
14        false
15    }
16
17    #[inline]
18    fn as_ptr(&self) -> *const T {
19        ptr::NonNull::dangling().as_ptr()
20    }
21
22    #[inline]
23    fn as_mut_ptr(&mut self) -> *mut T {
24        ptr::NonNull::dangling().as_ptr()
25    }
26
27    #[inline]
28    fn try_merge<B>(&mut self, _: usize, other: B, _: usize) -> Result<(), B>
29    where
30        B: RawVec<T>,
31    {
32        Err(other)
33    }
34}
35
36/// An allocator which cannot allocate anything.
37///
38/// If any operation requires allocations this will error.
39#[non_exhaustive]
40pub struct Disabled;
41
42impl Disabled {
43    /// Construct a new disabled allocator.
44    #[inline]
45    pub const fn new() -> Self {
46        Self
47    }
48}
49
50impl Default for Disabled {
51    #[inline]
52    fn default() -> Self {
53        Self::new()
54    }
55}
56
57impl Allocator for Disabled {
58    type RawVec<'this, T> = EmptyBuf<T> where T: 'this;
59
60    #[inline(always)]
61    fn new_raw_vec<'a, T>(&'a self) -> Self::RawVec<'a, T>
62    where
63        T: 'a,
64    {
65        EmptyBuf {
66            _marker: PhantomData,
67        }
68    }
69}