Skip to content

Commit

Permalink
Add PartialEq for Bytes in nom-embedded-storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandudey committed Dec 12, 2024
1 parent 879aa9a commit 098b726
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions nom-embedded-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use core::{
cell::RefCell,
iter::Enumerate,
ops::Deref,
ops::{Range, RangeFrom, RangeFull, RangeTo},
};
use embedded_storage::nor_flash::ReadNorFlash;
Expand Down Expand Up @@ -142,6 +143,66 @@ impl<S, const N: usize> Clone for Bytes<S, N> {
}
}

impl<S, const N: usize> PartialEq for Bytes<S, N>
where
S: ReadNorFlash,
{
fn eq(&self, other: &Self) -> bool {
if other.len() != self.len() {
return false;
}

if other.is_empty() != self.is_empty() {
return false;
}

let mut pos = 0;
while pos < self.len() {
let offset0 = match u32::try_from(self.offset + pos) {
Ok(v) => v,
Err(_) => return false,
};

let offset1 = match u32::try_from(other.offset + pos) {
Ok(v) => v,
Err(_) => return false,
};

let len = self.len().min(N);

let mut buffer0 = self.buffer.borrow_mut();
buffer0.clear();
buffer0
.resize(len, 0)
.expect("chunk size should be less than or equal to N");

// NOTE: We can't use other.buffer here because the user can
// pass &self as other, and we already borrowed that one.
let mut buffer1 = Vec::<u8, N>::new();
buffer1.clear();
buffer1
.resize(len, 0)
.expect("chunk size should be less than or equal to N");

if let Err(_) = self.storage.borrow_mut().read(offset0, &mut buffer0) {
return false;
}

if let Err(_) = self.storage.borrow_mut().read(offset1, &mut buffer1) {
return false;
}

pos += self.len().min(N);

if buffer0.deref() != buffer1.deref() {
return false;
}
}

true
}
}

/// An iterator over [`Bytes`].
#[derive(Debug)]
pub struct BytesIter<S, const N: usize> {
Expand Down Expand Up @@ -670,4 +731,31 @@ mod tests {
"case-insensitive comparison should succeed"
);
}

#[test]
fn test_partial_eq() {
let original = b"abcd123";
let storage = NonNull::from(Box::leak(Box::new(RcInner::new(RefCell::new(Storage(
original,
))))));
let storage = unsafe { Rc::from_inner(storage) };
let s0 = Bytes::<_, 16>::new(0, original.len(), storage).unwrap();

let original = b"abcd123";
let storage = NonNull::from(Box::leak(Box::new(RcInner::new(RefCell::new(Storage(
original,
))))));
let storage = unsafe { Rc::from_inner(storage) };
let s1 = Bytes::<_, 16>::new(0, original.len(), storage).unwrap();

assert_eq!(s0, s0, "same bytes should be equal");
assert_eq!(s1, s1, "same bytes should be equal");
assert_eq!(s0, s1);
assert_eq!(s0.slice(2..), s1.slice(2..));
assert_ne!(s0.slice(3..), s1.slice(2..));
assert_ne!(s0.slice(2..), s1.slice(3..));
assert_eq!(s0.slice(2..4).len(), 2);
assert_eq!(s1.slice(2..4).len(), 2);
assert_ne!(s0.slice(2..4), s1.slice(4..6));
}
}

0 comments on commit 098b726

Please sign in to comment.