pub struct BedrockPathBuffer { buffer: [u8; 256], pointer: u8, } impl BedrockPathBuffer { pub fn new() -> Self { Self { buffer: [0; 256] , pointer: 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]) } /// 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 the start of the path or 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 as u8).saturating_add(1), 0x00 => break, _ => continue, } } } } /// Read a single byte from the buffer. pub fn read(&mut self) -> u8 { let pointer = self.pointer as usize; self.pointer = self.pointer.wrapping_add(1); self.buffer[pointer] } /// Write a single byte to the buffer. /// /// If a null-byte is written, the buffer will be cleared and returned. pub fn write(&mut self, byte: u8) -> Option<[u8; 256]> { if byte == 0x00 { Some(self.clear()) } else { self.buffer[self.pointer as usize] = byte; self.pointer = self.pointer.saturating_add(1); None } } }