ps_buffer/methods/
push.rs

1use crate::{Buffer, BufferError};
2
3impl Buffer {
4    /// Appends a single byte to the buffer, automatically resizing if necessary.
5    ///
6    /// This method adds a `u8` value to the end of the buffer.
7    /// If the buffer is at capacity, it attempts to reserve additional space
8    /// before inserting the value.
9    ///
10    /// # Arguments
11    ///
12    /// * `value` - A value that can be converted into a `u8`
13    ///
14    /// # Errors
15    ///
16    /// * `BufferError::Overflow` - If incrementing the length would overflow `usize`
17    /// * `BufferError` - If memory allocation fails during resize
18    ///
19    /// # Examples
20    ///
21    /// ```
22    /// # use ps_buffer::{Buffer, BufferError};
23    /// let mut buffer = Buffer::new();
24    /// buffer.push(42)?;
25    /// assert_eq!(buffer.slice(..), &[42]);
26    /// buffer.push(65)?;
27    /// assert_eq!(buffer.slice(..), &[42, 65]);
28    /// # Ok::<(), BufferError>(())
29    /// ```
30    pub fn push(&mut self, value: impl Into<u8>) -> Result<&mut Self, BufferError> {
31        if self.len() >= self.capacity() {
32            self.reserve(1)?;
33        }
34
35        let index = self.len();
36        let value = value.into();
37
38        self.length += 1;
39        self[index] = value;
40
41        Ok(self)
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use ps_alloc::AllocationError;
48
49    use crate::{Buffer, BufferError};
50
51    #[test]
52    fn push_digits() -> Result<(), BufferError> {
53        let mut buffer = Buffer::new();
54
55        for i in 0..10 {
56            buffer.push(b'0' + i)?;
57        }
58
59        assert_eq!(buffer.slice(..), b"0123456789");
60        assert_eq!(buffer.len(), 10);
61        assert!(buffer.capacity() >= 10);
62
63        Ok(())
64    }
65
66    #[test]
67    fn push_values() -> Result<(), BufferError> {
68        let mut buffer = Buffer::new();
69        buffer.push(65)?;
70        buffer.push(66)?;
71        assert_eq!(buffer.slice(..), &[65, 66]);
72        assert_eq!(buffer.len(), 2);
73        Ok(())
74    }
75
76    #[test]
77    fn push_at_capacity() -> Result<(), BufferError> {
78        // Create a buffer with exact capacity
79        let mut buffer = Buffer::with_capacity(2)?;
80        buffer.push(1)?;
81        buffer.push(2)?;
82        // Push one more to trigger resize
83        buffer.push(3)?;
84        assert_eq!(buffer.slice(..), &[1, 2, 3]);
85        assert_eq!(buffer.len(), 3);
86        assert!(buffer.capacity() >= 3);
87        Ok(())
88    }
89
90    #[test]
91    fn push_empty() -> Result<(), BufferError> {
92        let mut buffer = Buffer::new();
93        buffer.push(0)?;
94        assert_eq!(buffer.slice(..), &[0]);
95        assert_eq!(buffer.len(), 1);
96        Ok(())
97    }
98
99    #[test]
100    fn push_overflow() {
101        let mut buffer = Buffer::new();
102
103        buffer.length = usize::MAX;
104
105        let result = buffer.push(0);
106
107        assert!(
108            matches!(
109                result,
110                Err(BufferError::AllocationError(AllocationError::OutOfMemory))
111            ),
112            "Overflowing the Buffer did not return AllocationError::OutOfMemory."
113        );
114    }
115}