Skip to content

Commit

Permalink
Fix recursive PartialEq impl in BSTR (#2116)
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev authored Oct 24, 2022
1 parent 1e14dc4 commit 9279ef5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
26 changes: 12 additions & 14 deletions crates/libs/windows/src/core/strings/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,25 @@ impl std::cmp::PartialEq for BSTR {
}
}
impl std::cmp::Eq for BSTR {}
impl std::cmp::PartialEq<windows::core::alloc::string::String> for BSTR {
fn eq(&self, other: &windows::core::alloc::string::String) -> bool {
self == other.as_str()
}
}
impl std::cmp::PartialEq<str> for BSTR {
fn eq(&self, other: &str) -> bool {
self == other
}
}
impl std::cmp::PartialEq<&str> for BSTR {
fn eq(&self, other: &&str) -> bool {
self.as_wide().iter().copied().eq(other.encode_utf16())

impl std::cmp::PartialEq<BSTR> for &str {
fn eq(&self, other: &BSTR) -> bool {
other == self
}
}

impl std::cmp::PartialEq<BSTR> for &str {
impl std::cmp::PartialEq<BSTR> for String {
fn eq(&self, other: &BSTR) -> bool {
other == self
}
}

impl<T: AsRef<str> + ?Sized> std::cmp::PartialEq<T> for BSTR {
fn eq(&self, other: &T) -> bool {
self.as_wide().iter().copied().eq(other.as_ref().encode_utf16())
}
}

impl std::ops::Drop for BSTR {
fn drop(&mut self) {
if !self.0.is_null() {
Expand Down
5 changes: 5 additions & 0 deletions crates/tests/bstr/tests/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn clone() {
let b = a.clone();
assert_eq!(a, "hello");
assert_eq!(b, "hello");
assert_eq!("hello", a);

let a = BSTR::default();
assert!(a.is_empty());
Expand Down Expand Up @@ -44,6 +45,10 @@ fn clone() {
let a: BSTR = unsafe { SysAllocStringLen(None) };
assert!(a.is_empty());
assert!(a.len() == 0);

let a = BSTR::from("a");
assert_eq!(a, String::from("a"));
assert_eq!(String::from("a"), a);
}

#[test]
Expand Down

0 comments on commit 9279ef5

Please sign in to comment.