Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,42 @@ impl Clone for Box<str> {
}
}

// This is the same impl as `Cow<'a, str>`, but with a newer stability attribute.
// It needs to be in this file to refer to the Box as a local struct rather than as a lang-item.
macro_rules! impl_eq {
($lhs:ty, $rhs: ty) => {
#[stable(feature = "box_str_partial_eq", since = "CURRENT_RUSTC_VERSION")]
#[allow(unused_lifetimes)]
impl<'a, 'b> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool {
PartialEq::eq(&self[..], &other[..])
}
#[inline]
fn ne(&self, other: &$rhs) -> bool {
PartialEq::ne(&self[..], &other[..])
}
}

#[stable(feature = "box_str_partial_eq", since = "CURRENT_RUSTC_VERSION")]
#[allow(unused_lifetimes)]
impl<'a, 'b> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool {
PartialEq::eq(&self[..], &other[..])
}
#[inline]
fn ne(&self, other: &$lhs) -> bool {
PartialEq::ne(&self[..], &other[..])
}
}
};
}

impl_eq! { Box<str>, str }
impl_eq! { Box<str>, &'a str }
impl_eq! { Box<str>, String }

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for Box<T, A> {
#[inline]
Expand All @@ -1564,6 +1600,7 @@ impl<T: ?Sized + PartialEq, A: Allocator> PartialEq for Box<T, A> {
PartialEq::ne(&**self, &**other)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialOrd, A: Allocator> PartialOrd for Box<T, A> {
#[inline]
Expand Down
23 changes: 23 additions & 0 deletions library/alloc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ fn test_le() {
assert_ne!("foo", "bar");
}

#[test]
fn test_box_str_eq() {
let boxed: Box<str> = Box::from("boxed");
assert!(*"boxed" == boxed);
assert!(*"other" != boxed);
assert!(boxed == *"boxed");
assert!(boxed != *"other");

assert!(&"boxed" == &boxed);
assert!(&boxed == &"boxed");
assert!(&"BOXED" != &boxed);
assert!(&boxed != &"BOXED");

assert_eq!(Box::from("foo"), "foo");
assert_eq!("bar", Box::from("bar"));

assert_ne!(Box::from("foo"), "bar");
assert_ne!("bar", Box::from("foo"));

assert!("" == Box::from(""));
assert!("本" == String::from("本").into_boxed_str());
}

#[test]
fn test_find() {
assert_eq!("hello".find('l'), Some(2));
Expand Down