summaryrefslogtreecommitdiff
path: root/src/devices/file/circular_path_buffer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/devices/file/circular_path_buffer.rs')
-rw-r--r--src/devices/file/circular_path_buffer.rs45
1 files changed, 21 insertions, 24 deletions
diff --git a/src/devices/file/circular_path_buffer.rs b/src/devices/file/circular_path_buffer.rs
index e5d903b..9d1dea6 100644
--- a/src/devices/file/circular_path_buffer.rs
+++ b/src/devices/file/circular_path_buffer.rs
@@ -1,5 +1,3 @@
-use super::*;
-
pub struct CircularPathBuffer {
buffer: [u8; 256],
pointer: u8,
@@ -10,29 +8,29 @@ impl CircularPathBuffer {
Self { buffer: [0; 256] , pointer: 0 }
}
- pub fn clear(&mut self) {
- self.buffer.fill(0);
+ /// Clear the buffer, returning the previous buffer contents.
+ pub fn clear(&mut self) -> [u8; 256] {
self.pointer = 0;
+ std::mem::replace(&mut self.buffer, [0; 256])
}
- pub fn populate(&mut self, bytes: &[u8]) {
- self.clear();
- if bytes.len() > 255 {
- unreachable!(
- "Attempted to populate CircularPathBuffer with {} bytes: {:?}",
- bytes.len(), String::from_utf8_lossy(bytes)
- )
- }
- let self_slice = &mut self.buffer[..bytes.len()];
- self_slice.copy_from_slice(&bytes);
+ /// Reset the pointer and hot-swap the byte buffer.
+ pub fn populate(&mut self, buffer: [u8; 256]) {
+ self.pointer = 0;
+ self.buffer = buffer;
}
+ /// Move internal pointer to either the start of the path or the file name.
+ ///
+ /// If value is non-zero, the pointer will be moved to the byte
+ /// directly following the final forward-slash.
pub fn set_pointer(&mut self, value: u8) {
self.pointer = 0;
+ // Set the pointer to the start of the filename if value is truthy.
if value != 0x00 {
for (i, c) in self.buffer.iter().enumerate() {
match c {
- b'/' => self.pointer = (i + 1) as u8,
+ b'/' => self.pointer = (i as u8).saturating_add(1),
0x00 => break,
_ => continue,
}
@@ -40,23 +38,22 @@ impl CircularPathBuffer {
}
}
+ /// Read a single byte from the buffer.
pub fn read_byte(&mut self) -> u8 {
let pointer = self.pointer as usize;
self.pointer = self.pointer.wrapping_add(1);
self.buffer[pointer]
}
- // Returns an unsanitized relative path.
- pub fn push_byte(&mut self, value: u8) -> Option<PathBuf> {
- if value == 0x00 {
- let pointer = self.pointer as usize;
- let path = bytes_to_path(&self.buffer[..pointer]);
- self.clear();
- Some(path)
+ /// Write a single byte to the buffer.
+ ///
+ /// If a null-byte is written, the buffer will be cleared and returned.
+ pub fn push_byte(&mut self, byte: u8) -> Option<[u8; 256]> {
+ if byte == 0x00 {
+ Some(self.clear())
} else {
- let pointer = self.pointer as usize;
+ self.buffer[self.pointer as usize] = byte;
self.pointer = self.pointer.wrapping_add(1);
- self.buffer[pointer] = value;
None
}
}