Skip to content

Commit 4f1bbe9

Browse files
committed
Implement equality with CStr
1 parent 08f1794 commit 4f1bbe9

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/c_string.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@ impl<const N: usize, const M: usize> PartialEq<CString<M>> for CString<N> {
298298
}
299299
}
300300

301+
impl<const N: usize> PartialEq<CStr> for CString<N> {
302+
fn eq(&self, rhs: &CStr) -> bool {
303+
self.as_c_str() == rhs
304+
}
305+
}
306+
307+
impl<const N: usize> PartialEq<&CStr> for CString<N> {
308+
fn eq(&self, rhs: &&CStr) -> bool {
309+
self.as_c_str() == *rhs
310+
}
311+
}
312+
313+
impl<const N: usize> PartialEq<CString<N>> for CStr {
314+
fn eq(&self, rhs: &CString<N>) -> bool {
315+
self == rhs.as_c_str()
316+
}
317+
}
318+
319+
impl<const N: usize> PartialEq<CString<N>> for &CStr {
320+
fn eq(&self, rhs: &CString<N>) -> bool {
321+
*self == rhs.as_c_str()
322+
}
323+
}
324+
301325
impl<const N: usize> Eq for CString<N> {}
302326

303327
/// An error to extend [`CString`] with bytes.
@@ -474,4 +498,19 @@ mod tests {
474498
!= CString::<4>::from_bytes_with_nul(b"abc\0").unwrap()
475499
);
476500
}
501+
502+
#[test]
503+
fn equal_with_c_str() {
504+
// Empty strings
505+
assert!(CString::<1>::new() == c"");
506+
assert!(c"" == CString::<1>::new());
507+
508+
// Single character
509+
assert!(CString::<2>::from_bytes_with_nul(b"a\0").unwrap() == c"a");
510+
assert!(c"a" == CString::<2>::from_bytes_with_nul(b"a\0").unwrap());
511+
512+
// Multiple characters
513+
assert!(CString::<4>::from_bytes_with_nul(b"abc\0").unwrap() == c"abc");
514+
assert!(c"abc" == CString::<4>::from_bytes_with_nul(b"abc\0").unwrap());
515+
}
477516
}

0 commit comments

Comments
 (0)