Write bytes into the buffer. Returns the number of bytes written. If the buffer doesn't have enough remaining capacity, writes as much as possible and returns the count.
(&mut self, data: &[u8])
| 91 | /// If the buffer doesn't have enough remaining capacity, writes as much |
| 92 | /// as possible and returns the count. |
| 93 | pub fn write(&mut self, data: &[u8]) -> usize { |
| 94 | let available = self.capacity - self.len; |
| 95 | let to_write = data.len().min(available); |
| 96 | if to_write > 0 { |
| 97 | // SAFETY: ptr + len is within the allocation, and to_write <= available. |
| 98 | unsafe { |
| 99 | std::ptr::copy_nonoverlapping(data.as_ptr(), self.ptr.add(self.len), to_write); |
| 100 | } |
| 101 | self.len += to_write; |
| 102 | } |
| 103 | to_write |
| 104 | } |
| 105 | |
| 106 | /// Get the written portion of the buffer as a byte slice. |
| 107 | /// |