Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) add mutable indexing, pop #1

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/nibbles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ where
}
}

impl<Idx> core::ops::IndexMut<Idx> for Nibbles
where
Repr: core::ops::IndexMut<Idx>,
{
#[inline]
fn index_mut(&mut self, index: Idx) -> &mut Self::Output {
self.0.index_mut(index)
}
}

#[cfg(feature = "rlp")]
impl alloy_rlp::Encodable for Nibbles {
#[inline]
Expand Down Expand Up @@ -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<u8> {
self.0.pop()
}

/// Extend the current nibbles with another nibbles.
#[inline]
pub fn extend_from_slice(&mut self, b: impl AsRef<[u8]>) {
Expand Down Expand Up @@ -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[0], 0x0A);
nibbles[0] = 0x0B;
assert_eq!(nibbles[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::<Vec<u8>>()) {
Expand Down