1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(not(feature = "alloc"))]
use musli_common::fixed_bytes::FixedBytes;

/// Provides the necessary scratch buffer used when decoding JSON.
#[doc(hidden)]
pub struct Scratch {
    #[cfg(feature = "alloc")]
    pub(crate) bytes: Vec<u8>,
    #[cfg(not(feature = "alloc"))]
    pub(crate) bytes: FixedBytes<128>,
}

impl Scratch {
    #[inline]
    pub(crate) fn new() -> Self {
        Self {
            bytes: Default::default(),
        }
    }

    #[inline]
    pub fn push(&mut self, value: u8) -> bool {
        #[cfg(feature = "alloc")]
        {
            self.bytes.push(value);
            true
        }

        #[cfg(not(feature = "alloc"))]
        {
            self.bytes.push(value)
        }
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }

    #[inline]
    pub(crate) fn extend_from_slice(&mut self, slice: &[u8]) -> bool {
        #[cfg(feature = "alloc")]
        {
            self.bytes.extend_from_slice(slice);
            true
        }

        #[cfg(not(feature = "alloc"))]
        {
            self.bytes.extend_from_slice(slice)
        }
    }

    #[inline]
    pub(crate) fn as_bytes(&self) -> &[u8] {
        self.bytes.as_slice()
    }
}