musli_allocator/disabled.rs
1use core::fmt::Arguments;
2
3use musli::buf::Error;
4use musli::{Allocator, Buf};
5
6/// An empty buffer.
7pub struct EmptyBuf;
8
9impl Buf for EmptyBuf {
10 #[inline(always)]
11 fn write(&mut self, _: &[u8]) -> bool {
12 false
13 }
14
15 #[inline(always)]
16 fn len(&self) -> usize {
17 0
18 }
19
20 #[inline(always)]
21 fn as_slice(&self) -> &[u8] {
22 &[]
23 }
24
25 #[inline(always)]
26 fn write_fmt(&mut self, _: Arguments<'_>) -> Result<(), Error> {
27 Err(Error)
28 }
29}
30
31/// An allocator which cannot allocate anything.
32///
33/// If any operation requires allocations this will error.
34#[non_exhaustive]
35pub struct Disabled;
36
37impl Disabled {
38 /// Construct a new disabled allocator.
39 #[inline]
40 pub const fn new() -> Self {
41 Self
42 }
43}
44
45impl Default for Disabled {
46 #[inline]
47 fn default() -> Self {
48 Self::new()
49 }
50}
51
52impl Allocator for Disabled {
53 type Buf<'this> = EmptyBuf;
54
55 #[inline(always)]
56 fn alloc(&self) -> Option<Self::Buf<'_>> {
57 Some(EmptyBuf)
58 }
59}