Skip to content

Commit f207ecb

Browse files
committed
Auto merge of #23972 - gkoz:partial_eq_str_string, r=alexcrichton
Right now comparing a `&String` (or a `&Cow`) to a `&str` requires redundant borrowing of the latter. Implementing `PartialEq<str>` tries to avoid this limitation. ```rust struct Foo (String); fn main () { let s = Foo("foo".to_string()); match s { Foo(ref x) if x == &"foo" => println!("foo!"), // avoid this -----^ _ => {} } } ``` I was hoping that #23521 would solve this but it didn't work out.
2 parents c1b8eb5 + 5234063 commit f207ecb

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/libcollections/string.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -796,49 +796,51 @@ impl<'a, 'b> Pattern<'a> for &'b String {
796796
#[stable(feature = "rust1", since = "1.0.0")]
797797
impl PartialEq for String {
798798
#[inline]
799-
fn eq(&self, other: &String) -> bool { PartialEq::eq(&**self, &**other) }
799+
fn eq(&self, other: &String) -> bool { PartialEq::eq(&self[..], &other[..]) }
800800
#[inline]
801-
fn ne(&self, other: &String) -> bool { PartialEq::ne(&**self, &**other) }
801+
fn ne(&self, other: &String) -> bool { PartialEq::ne(&self[..], &other[..]) }
802802
}
803803

804804
macro_rules! impl_eq {
805805
($lhs:ty, $rhs: ty) => {
806806
#[stable(feature = "rust1", since = "1.0.0")]
807807
impl<'a> PartialEq<$rhs> for $lhs {
808808
#[inline]
809-
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
809+
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
810810
#[inline]
811-
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
811+
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
812812
}
813813

814814
#[stable(feature = "rust1", since = "1.0.0")]
815815
impl<'a> PartialEq<$lhs> for $rhs {
816816
#[inline]
817-
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
817+
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) }
818818
#[inline]
819-
fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
819+
fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&self[..], &other[..]) }
820820
}
821821

822822
}
823823
}
824824

825+
impl_eq! { String, str }
825826
impl_eq! { String, &'a str }
827+
impl_eq! { Cow<'a, str>, str }
826828
impl_eq! { Cow<'a, str>, String }
827829

828830
#[stable(feature = "rust1", since = "1.0.0")]
829831
impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str> {
830832
#[inline]
831-
fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&**self, &**other) }
833+
fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&self[..], &other[..]) }
832834
#[inline]
833-
fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&**self, &**other) }
835+
fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&self[..], &other[..]) }
834836
}
835837

836838
#[stable(feature = "rust1", since = "1.0.0")]
837839
impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str {
838840
#[inline]
839-
fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&**self, &**other) }
841+
fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&self[..], &other[..]) }
840842
#[inline]
841-
fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&**self, &**other) }
843+
fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&self[..], &other[..]) }
842844
}
843845

844846
#[unstable(feature = "collections", reason = "waiting on Str stabilization")]

0 commit comments

Comments
 (0)