bytestr/
impls.rs

1use alloc::borrow::{Borrow, Cow};
2use alloc::string::String;
3use bytes::Bytes;
4use core::fmt;
5use core::ops::{Deref, Index, Range, RangeFrom, RangeFull, RangeTo, RangeToInclusive};
6use core::str::FromStr;
7
8use crate::ByteStr;
9
10impl fmt::Debug for ByteStr {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        fmt::Debug::fmt(self.as_str(), f)
13    }
14}
15
16impl fmt::Display for ByteStr {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        fmt::Display::fmt(self.as_str(), f)
19    }
20}
21
22impl Deref for ByteStr {
23    type Target = str;
24
25    fn deref(&self) -> &Self::Target {
26        self.as_str()
27    }
28}
29
30impl AsRef<str> for ByteStr {
31    fn as_ref(&self) -> &str {
32        self.as_str()
33    }
34}
35
36impl Borrow<str> for ByteStr {
37    fn borrow(&self) -> &str {
38        self.as_str()
39    }
40}
41
42impl AsRef<[u8]> for ByteStr {
43    fn as_ref(&self) -> &[u8] {
44        self.as_str().as_bytes()
45    }
46}
47
48impl FromStr for ByteStr {
49    type Err = ();
50
51    fn from_str(s: &str) -> Result<Self, Self::Err> {
52        Ok(Self::from(s))
53    }
54}
55
56impl<T: Into<String>> From<T> for ByteStr {
57    fn from(s: T) -> Self {
58        Self(s.into().into_bytes().into())
59    }
60}
61
62impl PartialEq<str> for ByteStr {
63    fn eq(&self, other: &str) -> bool {
64        &**self == other
65    }
66}
67
68impl PartialEq<String> for ByteStr {
69    fn eq(&self, other: &String) -> bool {
70        self.eq(&**other)
71    }
72}
73
74impl PartialEq<&str> for ByteStr {
75    fn eq(&self, other: &&str) -> bool {
76        self.eq(*other)
77    }
78}
79
80impl PartialEq<Cow<'_, str>> for ByteStr {
81    fn eq(&self, other: &Cow<str>) -> bool {
82        self.eq(&**other)
83    }
84}
85
86impl PartialEq<ByteStr> for String {
87    fn eq(&self, other: &ByteStr) -> bool {
88        other.eq(self)
89    }
90}
91
92impl PartialEq<ByteStr> for str {
93    fn eq(&self, other: &ByteStr) -> bool {
94        other.eq(self)
95    }
96}
97
98impl PartialEq<ByteStr> for &str {
99    fn eq(&self, other: &ByteStr) -> bool {
100        other.eq(self)
101    }
102}
103
104impl PartialEq<ByteStr> for Cow<'_, str> {
105    fn eq(&self, other: &ByteStr) -> bool {
106        other.eq(self)
107    }
108}
109
110impl From<ByteStr> for Bytes {
111    fn from(data: ByteStr) -> Self {
112        data.into_bytes()
113    }
114}
115
116// Index trait implementations for convenient slicing syntax
117
118impl Index<Range<usize>> for ByteStr {
119    type Output = str;
120
121    fn index(&self, index: Range<usize>) -> &Self::Output {
122        &self.as_str()[index]
123    }
124}
125
126impl Index<RangeFrom<usize>> for ByteStr {
127    type Output = str;
128
129    fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
130        &self.as_str()[index]
131    }
132}
133
134impl Index<RangeTo<usize>> for ByteStr {
135    type Output = str;
136
137    fn index(&self, index: RangeTo<usize>) -> &Self::Output {
138        &self.as_str()[index]
139    }
140}
141
142impl Index<RangeToInclusive<usize>> for ByteStr {
143    type Output = str;
144
145    fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output {
146        &self.as_str()[index]
147    }
148}
149
150impl Index<RangeFull> for ByteStr {
151    type Output = str;
152
153    fn index(&self, _index: RangeFull) -> &Self::Output {
154        self.as_str()
155    }
156}