Skip to content

Commit

Permalink
impl: fix cow partialeq impl
Browse files Browse the repository at this point in the history
Twas a silly mistake. One of the impls was comparing against itself,
which is obviously wrong. We fix it here and add a regression test.
We should probably add tests for other impls as well.

Fixes #82
  • Loading branch information
BurntSushi committed Feb 7, 2021
1 parent e9d337d commit b2111b6
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ macro_rules! impl_partial_eq_cow {
#[inline]
fn eq(&self, other: &$lhs) -> bool {
let this: &[u8] = (&**other).as_ref();
PartialEq::eq(this, other.as_bytes())
PartialEq::eq(this, self.as_bytes())
}
}
};
Expand Down Expand Up @@ -967,3 +967,18 @@ fn test_debug() {
B(&format!("{:?}", b"\xFF\xEF\xBF\xBD\xFF".as_bstr())).as_bstr(),
);
}

// See: https://github.com/BurntSushi/bstr/issues/82
#[test]
fn test_cows_regression() {
use crate::ByteSlice;
use std::borrow::Cow;

let c1 = Cow::from(b"hello bstr".as_bstr());
let c2 = b"goodbye bstr".as_bstr();
assert_ne!(c1, c2);

let c3 = Cow::from("hello str");
let c4 = "goodbye str";
assert_ne!(c3, c4);
}

0 comments on commit b2111b6

Please sign in to comment.