Skip to content

Commit 2ef7655

Browse files
Use macro for traits
1 parent a39a627 commit 2ef7655

File tree

1 file changed

+134
-199
lines changed

1 file changed

+134
-199
lines changed

src/string.rs

+134-199
Original file line numberDiff line numberDiff line change
@@ -887,226 +887,161 @@ impl<const N: usize> Clone for String<N> {
887887
}
888888
}
889889

890-
impl<const N: usize> fmt::Debug for String<N> {
891-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
892-
self.as_view().fmt(f)
893-
}
894-
}
895-
896-
impl fmt::Debug for StringView {
897-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
898-
<str as fmt::Debug>::fmt(self, f)
899-
}
900-
}
901-
902-
impl<const N: usize> fmt::Display for String<N> {
903-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
904-
self.as_view().fmt(f)
905-
}
906-
}
907-
908-
impl fmt::Display for StringView {
909-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
910-
<str as fmt::Display>::fmt(self, f)
911-
}
912-
}
913-
914-
impl<const N: usize> hash::Hash for String<N> {
915-
#[inline]
916-
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
917-
self.as_view().hash(hasher)
918-
}
919-
}
920-
921-
impl hash::Hash for StringView {
922-
#[inline]
923-
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
924-
<str as hash::Hash>::hash(self, hasher)
925-
}
926-
}
927-
928-
impl<const N: usize> fmt::Write for String<N> {
929-
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
930-
self.as_mut_view().write_str(s)
931-
}
932-
933-
fn write_char(&mut self, c: char) -> Result<(), fmt::Error> {
934-
self.as_mut_view().write_char(c)
935-
}
936-
}
937-
938-
impl fmt::Write for StringView {
939-
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
940-
self.push_str(s).map_err(|_| fmt::Error)
941-
}
942-
943-
fn write_char(&mut self, c: char) -> Result<(), fmt::Error> {
944-
self.push(c).map_err(|_| fmt::Error)
945-
}
946-
}
947-
948-
impl<const N: usize> ops::Deref for String<N> {
949-
type Target = str;
950-
951-
fn deref(&self) -> &str {
952-
self.as_view().deref()
953-
}
954-
}
955-
956-
impl ops::Deref for StringView {
957-
type Target = str;
958-
959-
fn deref(&self) -> &str {
960-
self.as_str()
961-
}
962-
}
963-
964-
impl<const N: usize> ops::DerefMut for String<N> {
965-
fn deref_mut(&mut self) -> &mut str {
966-
self.as_mut_view().deref_mut()
967-
}
968-
}
890+
macro_rules! imp_traits {
891+
($Ty:ident$(<const $N:ident : usize, const $M:ident : usize>)?) => {
892+
// String<N>/StringView == String<N>
893+
impl<const N: usize, $(const $M: usize)*> PartialEq<String<N>> for $Ty<$($M)*>
894+
{
895+
#[inline]
896+
fn eq(&self, other: &String<N>) -> bool {
897+
self.as_str().eq(other.as_str())
898+
}
899+
}
969900

970-
impl ops::DerefMut for StringView {
971-
fn deref_mut(&mut self) -> &mut str {
972-
self.as_mut_str()
973-
}
974-
}
901+
// String<N>/StringView == StringView
902+
impl<$(const $M: usize)*> PartialEq<StringView> for $Ty<$($M)*>
903+
{
904+
#[inline]
905+
fn eq(&self, other: &StringView) -> bool {
906+
self.as_str().eq(other.as_str())
907+
}
908+
}
975909

976-
impl<const N: usize> AsRef<str> for String<N> {
977-
#[inline]
978-
fn as_ref(&self) -> &str {
979-
self
980-
}
981-
}
910+
// String<N>/StringView == str
911+
impl<$(const $M: usize)*> PartialEq<str> for $Ty<$($M)*>
912+
{
913+
#[inline]
914+
fn eq(&self, other: &str) -> bool {
915+
self.as_str().eq(other)
916+
}
917+
}
982918

983-
impl AsRef<str> for StringView {
984-
#[inline]
985-
fn as_ref(&self) -> &str {
986-
self
987-
}
988-
}
919+
// str == String<N>/StringView
920+
impl<$(const $M: usize)*> PartialEq<$Ty<$($M)*>> for str
921+
{
922+
#[inline]
923+
fn eq(&self, other: &$Ty<$($M)*>) -> bool {
924+
self.eq(other.as_str())
925+
}
926+
}
989927

990-
impl<const N: usize> AsRef<[u8]> for String<N> {
991-
#[inline]
992-
fn as_ref(&self) -> &[u8] {
993-
self.as_view().as_bytes()
994-
}
995-
}
928+
// &str == String<N>/StringView
929+
impl<$(const $M: usize)*> PartialEq<&str> for $Ty<$($M)*>
930+
{
931+
#[inline]
932+
fn eq(&self, other: &&str) -> bool {
933+
(*self).as_str().eq(*other)
934+
}
935+
}
996936

997-
impl AsRef<[u8]> for StringView {
998-
#[inline]
999-
fn as_ref(&self) -> &[u8] {
1000-
self.as_bytes()
1001-
}
1002-
}
937+
// String<N>/StringView == str
938+
impl<$(const $M: usize)*> PartialEq<$Ty<$($M)*>> for &str
939+
{
940+
#[inline]
941+
fn eq(&self, other: &$Ty<$($M)*>) -> bool {
942+
(*self).eq(other.as_str())
943+
}
944+
}
1003945

1004-
impl<const N1: usize, const N2: usize> PartialEq<String<N2>> for String<N1> {
1005-
fn eq(&self, rhs: &String<N2>) -> bool {
1006-
str::eq(&**self, &**rhs)
1007-
}
1008-
}
946+
impl<$(const $M: usize)*> Eq for $Ty<$($M)*> {}
1009947

1010-
impl PartialEq<StringView> for StringView {
1011-
fn eq(&self, rhs: &StringView) -> bool {
1012-
str::eq(&**self, &**rhs)
1013-
}
1014-
}
948+
impl<const N: usize, $(const $M: usize)*> PartialOrd<String<N>> for $Ty<$($M)*>
949+
{
950+
#[inline]
951+
fn partial_cmp(&self, other: &String<N>) -> Option<Ordering> {
952+
Some(<str as Ord>::cmp(self, other))
953+
}
954+
}
1015955

1016-
// String<N> == str
1017-
impl<const N: usize> PartialEq<str> for String<N> {
1018-
#[inline]
1019-
fn eq(&self, other: &str) -> bool {
1020-
str::eq(self, other)
1021-
}
1022-
}
956+
impl<$(const $M: usize)*> PartialOrd<StringView> for $Ty<$($M)*>
957+
{
958+
#[inline]
959+
fn partial_cmp(&self, other: &StringView) -> Option<Ordering> {
960+
Some(<str as Ord>::cmp(self, other))
961+
}
962+
}
1023963

1024-
// StringView == str
1025-
impl PartialEq<str> for StringView {
1026-
#[inline]
1027-
fn eq(&self, other: &str) -> bool {
1028-
str::eq(self, other)
1029-
}
1030-
}
964+
impl<$(const $M: usize)*> Ord for $Ty<$($M)*> {
965+
fn cmp(&self, other: &$Ty<$($M)*>) -> Ordering {
966+
<str as Ord>::cmp(self, other)
967+
}
968+
}
1031969

1032-
// String<N> == &'str
1033-
impl<const N: usize> PartialEq<&str> for String<N> {
1034-
#[inline]
1035-
fn eq(&self, other: &&str) -> bool {
1036-
str::eq(self, &other[..])
1037-
}
1038-
}
970+
impl<$(const $M: usize)*> fmt::Debug for $Ty<$($M)*>
971+
{
972+
#[inline]
973+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
974+
<str as fmt::Debug>::fmt(self, f)
975+
}
976+
}
1039977

1040-
// StringView == &'str
1041-
impl PartialEq<&str> for StringView {
1042-
#[inline]
1043-
fn eq(&self, other: &&str) -> bool {
1044-
str::eq(self, &other[..])
1045-
}
1046-
}
978+
impl<$(const $M: usize)*> fmt::Display for $Ty<$($M)*>
979+
{
980+
#[inline]
981+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
982+
<str as fmt::Display>::fmt(self, f)
983+
}
984+
}
1047985

1048-
// str == String<N>
1049-
impl<const N: usize> PartialEq<String<N>> for str {
1050-
#[inline]
1051-
fn eq(&self, other: &String<N>) -> bool {
1052-
str::eq(self, &other[..])
1053-
}
1054-
}
986+
impl<$(const $M: usize)*> hash::Hash for $Ty<$($M)*>
987+
{
988+
#[inline]
989+
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
990+
<str as hash::Hash>::hash(self, hasher)
991+
}
992+
}
1055993

1056-
// str == StringView
1057-
impl PartialEq<StringView> for str {
1058-
#[inline]
1059-
fn eq(&self, other: &StringView) -> bool {
1060-
str::eq(self, &other[..])
1061-
}
1062-
}
994+
impl<$(const $M: usize)*> fmt::Write for $Ty<$($M)*>
995+
{
996+
#[inline]
997+
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
998+
self.push_str(s).map_err(|_| fmt::Error)
999+
}
10631000

1064-
// &'str == String<N>
1065-
impl<const N: usize> PartialEq<String<N>> for &str {
1066-
#[inline]
1067-
fn eq(&self, other: &String<N>) -> bool {
1068-
str::eq(self, &other[..])
1069-
}
1070-
}
1001+
#[inline]
1002+
fn write_char(&mut self, c: char) -> Result<(), fmt::Error> {
1003+
self.push(c).map_err(|_| fmt::Error)
1004+
}
1005+
}
10711006

1072-
// &'str == StringView
1073-
impl PartialEq<StringView> for &str {
1074-
#[inline]
1075-
fn eq(&self, other: &StringView) -> bool {
1076-
str::eq(self, &other[..])
1077-
}
1078-
}
1007+
impl<$(const $M: usize)*> ops::Deref for $Ty<$($M)*>
1008+
{
1009+
type Target = str;
10791010

1080-
impl<const N: usize> Eq for String<N> {}
1081-
impl Eq for StringView {}
1011+
#[inline]
1012+
fn deref(&self) -> &str {
1013+
self.as_str()
1014+
}
1015+
}
10821016

1083-
impl<const N1: usize, const N2: usize> PartialOrd<String<N2>> for String<N1> {
1084-
#[inline]
1085-
fn partial_cmp(&self, other: &String<N2>) -> Option<Ordering> {
1086-
Some(Ord::cmp(&**self, &**other))
1087-
}
1088-
}
1017+
impl<$(const $M: usize)*> ops::DerefMut for $Ty<$($M)*>
1018+
{
1019+
#[inline]
1020+
fn deref_mut(&mut self) -> &mut str {
1021+
self.as_mut_str()
1022+
}
1023+
}
10891024

1090-
impl PartialOrd<StringView> for StringView {
1091-
#[inline]
1092-
fn partial_cmp(&self, other: &StringView) -> Option<Ordering> {
1093-
Some(Ord::cmp(&**self, &**other))
1094-
}
1095-
}
1025+
impl<$(const $M: usize)*> AsRef<str> for $Ty<$($M)*>
1026+
{
1027+
#[inline]
1028+
fn as_ref(&self) -> &str {
1029+
self
1030+
}
1031+
}
10961032

1097-
impl<const N: usize> Ord for String<N> {
1098-
#[inline]
1099-
fn cmp(&self, other: &Self) -> Ordering {
1100-
Ord::cmp(&**self, &**other)
1101-
}
1033+
impl<$(const $M: usize)*> AsRef<[u8]> for $Ty<$($M)*>
1034+
{
1035+
#[inline]
1036+
fn as_ref(&self) -> &[u8] {
1037+
self.as_bytes()
1038+
}
1039+
}
1040+
};
11021041
}
11031042

1104-
impl Ord for StringView {
1105-
#[inline]
1106-
fn cmp(&self, other: &Self) -> Ordering {
1107-
Ord::cmp(&**self, &**other)
1108-
}
1109-
}
1043+
imp_traits!(String<const N: usize, const M: usize>);
1044+
imp_traits!(StringView);
11101045

11111046
/// Equivalent to [`format`](https://doc.rust-lang.org/std/fmt/fn.format.html).
11121047
///

0 commit comments

Comments
 (0)