Add comparison operators to ImmutableString.

This commit is contained in:
Stephen Chung
2020-07-30 13:28:25 +08:00
parent 691e04292f
commit e505a06839
4 changed files with 51 additions and 9 deletions

View File

@@ -6,6 +6,7 @@ use crate::stdlib::{
any::TypeId,
borrow::Borrow,
boxed::Box,
cmp::Ordering,
fmt,
hash::{BuildHasher, Hash, Hasher},
iter::FromIterator,
@@ -141,6 +142,12 @@ impl AsRef<String> for ImmutableString {
}
}
impl Borrow<String> for ImmutableString {
fn borrow(&self) -> &String {
&self.0
}
}
impl Borrow<str> for ImmutableString {
fn borrow(&self) -> &str {
self.0.as_str()
@@ -348,6 +355,42 @@ impl AddAssign<char> for ImmutableString {
}
}
impl<S: AsRef<str>> PartialEq<S> for ImmutableString {
fn eq(&self, other: &S) -> bool {
self.as_str().eq(other.as_ref())
}
}
impl PartialEq<ImmutableString> for str {
fn eq(&self, other: &ImmutableString) -> bool {
self.eq(other.as_str())
}
}
impl PartialEq<ImmutableString> for String {
fn eq(&self, other: &ImmutableString) -> bool {
self.eq(other.as_str())
}
}
impl<S: AsRef<str>> PartialOrd<S> for ImmutableString {
fn partial_cmp(&self, other: &S) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_ref())
}
}
impl PartialOrd<ImmutableString> for str {
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
self.partial_cmp(other.as_str())
}
}
impl PartialOrd<ImmutableString> for String {
fn partial_cmp(&self, other: &ImmutableString) -> Option<Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
impl ImmutableString {
/// Consume the `ImmutableString` and convert it into a `String`.
/// If there are other references to the same string, a cloned copy is returned.