diff --git a/src/string.rs b/src/string.rs index cead240d1c..3c5e1d9c18 100644 --- a/src/string.rs +++ b/src/string.rs @@ -887,226 +887,161 @@ impl Clone for String { } } -impl fmt::Debug for String { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.as_view().fmt(f) - } -} - -impl fmt::Debug for StringView { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - ::fmt(self, f) - } -} - -impl fmt::Display for String { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.as_view().fmt(f) - } -} - -impl fmt::Display for StringView { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - ::fmt(self, f) - } -} - -impl hash::Hash for String { - #[inline] - fn hash(&self, hasher: &mut H) { - self.as_view().hash(hasher) - } -} - -impl hash::Hash for StringView { - #[inline] - fn hash(&self, hasher: &mut H) { - ::hash(self, hasher) - } -} - -impl fmt::Write for String { - fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { - self.as_mut_view().write_str(s) - } - - fn write_char(&mut self, c: char) -> Result<(), fmt::Error> { - self.as_mut_view().write_char(c) - } -} - -impl fmt::Write for StringView { - fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { - self.push_str(s).map_err(|_| fmt::Error) - } - - fn write_char(&mut self, c: char) -> Result<(), fmt::Error> { - self.push(c).map_err(|_| fmt::Error) - } -} - -impl ops::Deref for String { - type Target = str; - - fn deref(&self) -> &str { - self.as_view().deref() - } -} - -impl ops::Deref for StringView { - type Target = str; - - fn deref(&self) -> &str { - self.as_str() - } -} - -impl ops::DerefMut for String { - fn deref_mut(&mut self) -> &mut str { - self.as_mut_view().deref_mut() - } -} +macro_rules! imp_traits { + ($Ty:ident$()?) => { + // String/StringView == String + impl PartialEq> for $Ty<$($M)*> + { + #[inline] + fn eq(&self, other: &String) -> bool { + self.as_str().eq(other.as_str()) + } + } -impl ops::DerefMut for StringView { - fn deref_mut(&mut self) -> &mut str { - self.as_mut_str() - } -} + // String/StringView == StringView + impl<$(const $M: usize)*> PartialEq for $Ty<$($M)*> + { + #[inline] + fn eq(&self, other: &StringView) -> bool { + self.as_str().eq(other.as_str()) + } + } -impl AsRef for String { - #[inline] - fn as_ref(&self) -> &str { - self - } -} + // String/StringView == str + impl<$(const $M: usize)*> PartialEq for $Ty<$($M)*> + { + #[inline] + fn eq(&self, other: &str) -> bool { + self.as_str().eq(other) + } + } -impl AsRef for StringView { - #[inline] - fn as_ref(&self) -> &str { - self - } -} + // str == String/StringView + impl<$(const $M: usize)*> PartialEq<$Ty<$($M)*>> for str + { + #[inline] + fn eq(&self, other: &$Ty<$($M)*>) -> bool { + self.eq(other.as_str()) + } + } -impl AsRef<[u8]> for String { - #[inline] - fn as_ref(&self) -> &[u8] { - self.as_view().as_bytes() - } -} + // &str == String/StringView + impl<$(const $M: usize)*> PartialEq<&str> for $Ty<$($M)*> + { + #[inline] + fn eq(&self, other: &&str) -> bool { + (*self).as_str().eq(*other) + } + } -impl AsRef<[u8]> for StringView { - #[inline] - fn as_ref(&self) -> &[u8] { - self.as_bytes() - } -} + // String/StringView == str + impl<$(const $M: usize)*> PartialEq<$Ty<$($M)*>> for &str + { + #[inline] + fn eq(&self, other: &$Ty<$($M)*>) -> bool { + (*self).eq(other.as_str()) + } + } -impl PartialEq> for String { - fn eq(&self, rhs: &String) -> bool { - str::eq(&**self, &**rhs) - } -} + impl<$(const $M: usize)*> Eq for $Ty<$($M)*> {} -impl PartialEq for StringView { - fn eq(&self, rhs: &StringView) -> bool { - str::eq(&**self, &**rhs) - } -} + impl PartialOrd> for $Ty<$($M)*> + { + #[inline] + fn partial_cmp(&self, other: &String) -> Option { + Some(::cmp(self, other)) + } + } -// String == str -impl PartialEq for String { - #[inline] - fn eq(&self, other: &str) -> bool { - str::eq(self, other) - } -} + impl<$(const $M: usize)*> PartialOrd for $Ty<$($M)*> + { + #[inline] + fn partial_cmp(&self, other: &StringView) -> Option { + Some(::cmp(self, other)) + } + } -// StringView == str -impl PartialEq for StringView { - #[inline] - fn eq(&self, other: &str) -> bool { - str::eq(self, other) - } -} + impl<$(const $M: usize)*> Ord for $Ty<$($M)*> { + fn cmp(&self, other: &$Ty<$($M)*>) -> Ordering { + ::cmp(self, other) + } + } -// String == &'str -impl PartialEq<&str> for String { - #[inline] - fn eq(&self, other: &&str) -> bool { - str::eq(self, &other[..]) - } -} + impl<$(const $M: usize)*> fmt::Debug for $Ty<$($M)*> + { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + ::fmt(self, f) + } + } -// StringView == &'str -impl PartialEq<&str> for StringView { - #[inline] - fn eq(&self, other: &&str) -> bool { - str::eq(self, &other[..]) - } -} + impl<$(const $M: usize)*> fmt::Display for $Ty<$($M)*> + { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + ::fmt(self, f) + } + } -// str == String -impl PartialEq> for str { - #[inline] - fn eq(&self, other: &String) -> bool { - str::eq(self, &other[..]) - } -} + impl<$(const $M: usize)*> hash::Hash for $Ty<$($M)*> + { + #[inline] + fn hash(&self, hasher: &mut H) { + ::hash(self, hasher) + } + } -// str == StringView -impl PartialEq for str { - #[inline] - fn eq(&self, other: &StringView) -> bool { - str::eq(self, &other[..]) - } -} + impl<$(const $M: usize)*> fmt::Write for $Ty<$($M)*> + { + #[inline] + fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { + self.push_str(s).map_err(|_| fmt::Error) + } -// &'str == String -impl PartialEq> for &str { - #[inline] - fn eq(&self, other: &String) -> bool { - str::eq(self, &other[..]) - } -} + #[inline] + fn write_char(&mut self, c: char) -> Result<(), fmt::Error> { + self.push(c).map_err(|_| fmt::Error) + } + } -// &'str == StringView -impl PartialEq for &str { - #[inline] - fn eq(&self, other: &StringView) -> bool { - str::eq(self, &other[..]) - } -} + impl<$(const $M: usize)*> ops::Deref for $Ty<$($M)*> + { + type Target = str; -impl Eq for String {} -impl Eq for StringView {} + #[inline] + fn deref(&self) -> &str { + self.as_str() + } + } -impl PartialOrd> for String { - #[inline] - fn partial_cmp(&self, other: &String) -> Option { - Some(Ord::cmp(&**self, &**other)) - } -} + impl<$(const $M: usize)*> ops::DerefMut for $Ty<$($M)*> + { + #[inline] + fn deref_mut(&mut self) -> &mut str { + self.as_mut_str() + } + } -impl PartialOrd for StringView { - #[inline] - fn partial_cmp(&self, other: &StringView) -> Option { - Some(Ord::cmp(&**self, &**other)) - } -} + impl<$(const $M: usize)*> AsRef for $Ty<$($M)*> + { + #[inline] + fn as_ref(&self) -> &str { + self + } + } -impl Ord for String { - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - Ord::cmp(&**self, &**other) - } + impl<$(const $M: usize)*> AsRef<[u8]> for $Ty<$($M)*> + { + #[inline] + fn as_ref(&self) -> &[u8] { + self.as_bytes() + } + } + }; } -impl Ord for StringView { - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - Ord::cmp(&**self, &**other) - } -} +imp_traits!(String); +imp_traits!(StringView); /// Equivalent to [`format`](https://doc.rust-lang.org/std/fmt/fn.format.html). ///