Skip to content

Commit c677f39

Browse files
committed
Implement Eq
1 parent 2bf5f22 commit c677f39

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/c_string.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ impl<const N: usize> Deref for CString<N> {
292292
}
293293
}
294294

295+
impl<const N: usize, const M: usize> PartialEq<CString<M>> for CString<N> {
296+
fn eq(&self, rhs: &CString<M>) -> bool {
297+
self.as_c_str() == rhs.as_c_str()
298+
}
299+
}
300+
301+
impl<const N: usize> Eq for CString<N> {}
302+
295303
/// An error to extend [`CString`] with bytes.
296304
#[derive(Debug)]
297305
pub enum ExtendError {
@@ -331,6 +339,11 @@ mod tests {
331339
assert_eq!(empty.to_str(), Ok(""));
332340
}
333341

342+
#[test]
343+
fn create_with_capacity_error() {
344+
assert!(CString::<1>::from_bytes_with_nul(b"a\0").is_err())
345+
}
346+
334347
#[test]
335348
fn push_no_bytes() {
336349
let mut c_string = CString::<1>::new();
@@ -429,4 +442,36 @@ mod tests {
429442
string.extend_from_bytes(b"foo").unwrap();
430443
assert_eq!(Borrow::<CStr>::borrow(&string), c"foo");
431444
}
445+
446+
#[test]
447+
fn equal() {
448+
// Empty strings
449+
assert!(CString::<1>::new() == CString::<1>::new());
450+
assert!(CString::<1>::new() == CString::<2>::new());
451+
assert!(CString::<1>::from_bytes_with_nul(b"\0").unwrap() == CString::<3>::new());
452+
453+
// Single character
454+
assert!(
455+
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
456+
== CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
457+
);
458+
assert!(
459+
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
460+
== CString::<3>::from_bytes_with_nul(b"a\0").unwrap()
461+
);
462+
assert!(
463+
CString::<2>::from_bytes_with_nul(b"a\0").unwrap()
464+
!= CString::<2>::from_bytes_with_nul(b"b\0").unwrap()
465+
);
466+
467+
// Multiple characters
468+
assert!(
469+
CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
470+
== CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
471+
);
472+
assert!(
473+
CString::<3>::from_bytes_with_nul(b"ab\0").unwrap()
474+
!= CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
475+
);
476+
}
432477
}

0 commit comments

Comments
 (0)