diff --git a/src/nibbles.rs b/src/nibbles.rs index 2bf581a..31633de 100644 --- a/src/nibbles.rs +++ b/src/nibbles.rs @@ -552,6 +552,16 @@ impl Nibbles { self[i] as usize } + /// Sets the nibble at the given index + /// + /// # Panics + /// + /// Panics if the index is out of bounds. + #[inline] + pub fn set_at(&mut self, i: usize, value: u8) { + self.0[i] = value; + } + /// Returns the first nibble of the current nibble sequence. #[inline] pub fn first(&self) -> Option { @@ -618,6 +628,12 @@ impl Nibbles { self.0.push(nibble); } + /// Pops a nibble from the end of the current nibbles. + #[inline] + pub fn pop(&mut self) -> Option { + self.0.pop() + } + /// Extend the current nibbles with another nibbles. #[inline] pub fn extend_from_slice(&mut self, b: impl AsRef<[u8]>) { @@ -701,6 +717,25 @@ mod tests { test_slice(0..RAW.len(), RAW); } + #[test] + fn indexing() { + let mut nibbles = Nibbles::from_nibbles_unchecked(&[0x0A]); + assert_eq!(nibbles.at(0), 0x0A); + nibbles.set_at(0, 0x0B); + assert_eq!(nibbles.at(0), 0x0B); + } + + #[test] + fn push_pop() { + let mut nibbles = Nibbles::new(); + nibbles.push(0x0A); + assert_eq!(nibbles[0], 0x0A); + assert_eq!(nibbles.len(), 1); + + assert_eq!(nibbles.pop(), Some(0x0A)); + assert_eq!(nibbles.len(), 0); + } + proptest! { #[test] fn pack_unpack_roundtrip(input in any::>()) {