]>
Commit | Line | Data |
---|---|---|
781aab86 FG |
1 | use std::borrow::Borrow; |
2 | use std::fmt; | |
3 | use std::str::Utf8Error; | |
4 | ||
5 | use crate::YarnBox; | |
6 | use crate::YarnRef; | |
7 | ||
8 | #[derive(Clone, Debug)] | |
9 | pub struct NonCopy(()); | |
10 | ||
11 | impl fmt::Display for NonCopy { | |
12 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
13 | f.write_str("cannot convert yarn to non-owning yarn") | |
14 | } | |
15 | } | |
16 | ||
17 | impl<'a, Buf> TryFrom<YarnBox<'a, Buf>> for YarnRef<'a, Buf> | |
18 | where | |
19 | Buf: crate::Buf + ?Sized, | |
20 | { | |
21 | type Error = NonCopy; | |
22 | ||
23 | fn try_from(y: YarnBox<'a, Buf>) -> Result<Self, NonCopy> { | |
24 | y.to_ref().ok_or(NonCopy(())) | |
25 | } | |
26 | } | |
27 | ||
28 | impl<'a> TryFrom<YarnBox<'a, [u8]>> for YarnBox<'a, str> { | |
29 | type Error = Utf8Error; | |
30 | ||
31 | fn try_from(y: YarnBox<'a, [u8]>) -> Result<Self, Utf8Error> { | |
32 | y.to_utf8() | |
33 | } | |
34 | } | |
35 | ||
36 | impl<'a> TryFrom<YarnRef<'a, [u8]>> for YarnRef<'a, str> { | |
37 | type Error = Utf8Error; | |
38 | ||
39 | fn try_from(y: YarnRef<'a, [u8]>) -> Result<Self, Utf8Error> { | |
40 | y.to_utf8() | |
41 | } | |
42 | } | |
43 | ||
44 | impl<'a> From<YarnBox<'a, str>> for YarnBox<'a, [u8]> { | |
45 | fn from(y: YarnBox<'a, str>) -> Self { | |
46 | y.into_bytes() | |
47 | } | |
48 | } | |
49 | ||
50 | impl<'a> From<YarnRef<'a, str>> for YarnRef<'a, [u8]> { | |
51 | fn from(y: YarnRef<'a, str>) -> Self { | |
52 | y.into_bytes() | |
53 | } | |
54 | } | |
55 | ||
56 | impl From<u8> for YarnBox<'_, [u8]> { | |
57 | fn from(c: u8) -> Self { | |
58 | Self::from_byte(c) | |
59 | } | |
60 | } | |
61 | ||
62 | impl From<u8> for YarnRef<'_, [u8]> { | |
63 | fn from(c: u8) -> Self { | |
64 | Self::from_byte(c) | |
65 | } | |
66 | } | |
67 | ||
68 | impl<Buf> From<char> for YarnBox<'_, Buf> | |
69 | where | |
70 | Buf: crate::Buf + ?Sized, | |
71 | { | |
72 | fn from(c: char) -> Self { | |
73 | Self::from_char(c) | |
74 | } | |
75 | } | |
76 | ||
77 | impl<Buf> From<char> for YarnRef<'_, Buf> | |
78 | where | |
79 | Buf: crate::Buf + ?Sized, | |
80 | { | |
81 | fn from(c: char) -> Self { | |
82 | Self::from_char(c) | |
83 | } | |
84 | } | |
85 | ||
86 | impl<'a, Buf> From<&'a Buf> for YarnBox<'a, Buf> | |
87 | where | |
88 | Buf: crate::Buf + ?Sized, | |
89 | { | |
90 | fn from(s: &'a Buf) -> Self { | |
91 | Self::new(s) | |
92 | } | |
93 | } | |
94 | ||
95 | impl<'a, Buf> From<&'a YarnBox<'_, Buf>> for YarnBox<'a, Buf> | |
96 | where | |
97 | Buf: crate::Buf + ?Sized, | |
98 | { | |
99 | fn from(s: &'a YarnBox<'a, Buf>) -> Self { | |
100 | s.aliased() | |
101 | } | |
102 | } | |
103 | ||
104 | impl<'a, Buf> From<&'a YarnBox<'_, Buf>> for YarnRef<'a, Buf> | |
105 | where | |
106 | Buf: crate::Buf + ?Sized, | |
107 | { | |
108 | fn from(s: &'a YarnBox<'a, Buf>) -> Self { | |
109 | s.as_ref() | |
110 | } | |
111 | } | |
112 | ||
113 | impl<'a, Buf> From<&'a Buf> for YarnRef<'a, Buf> | |
114 | where | |
115 | Buf: crate::Buf + ?Sized, | |
116 | { | |
117 | fn from(s: &'a Buf) -> Self { | |
118 | Self::new(s) | |
119 | } | |
120 | } | |
121 | ||
122 | impl From<Box<[u8]>> for YarnBox<'_, [u8]> { | |
123 | fn from(s: Box<[u8]>) -> Self { | |
124 | Self::from_boxed_bytes(s) | |
125 | } | |
126 | } | |
127 | ||
128 | impl From<Vec<u8>> for YarnBox<'_, [u8]> { | |
129 | fn from(s: Vec<u8>) -> Self { | |
130 | Self::from_vec(s) | |
131 | } | |
132 | } | |
133 | ||
134 | impl<Buf> From<Box<str>> for YarnBox<'_, Buf> | |
135 | where | |
136 | Buf: crate::Buf + ?Sized, | |
137 | { | |
138 | fn from(s: Box<str>) -> Self { | |
139 | Self::from_boxed_str(s) | |
140 | } | |
141 | } | |
142 | ||
143 | impl<Buf> From<String> for YarnBox<'_, Buf> | |
144 | where | |
145 | Buf: crate::Buf + ?Sized, | |
146 | { | |
147 | fn from(s: String) -> Self { | |
148 | Self::from_string(s) | |
149 | } | |
150 | } | |
151 | ||
152 | impl<Buf> From<YarnBox<'_, Buf>> for Box<[u8]> | |
153 | where | |
154 | Buf: crate::Buf + ?Sized, | |
155 | { | |
156 | fn from(y: YarnBox<Buf>) -> Self { | |
157 | y.into_boxed_bytes() | |
158 | } | |
159 | } | |
160 | ||
161 | impl<Buf> From<YarnRef<'_, Buf>> for Box<[u8]> | |
162 | where | |
163 | Buf: crate::Buf + ?Sized, | |
164 | { | |
165 | fn from(y: YarnRef<Buf>) -> Self { | |
166 | y.to_boxed_bytes() | |
167 | } | |
168 | } | |
169 | ||
170 | impl<Buf> From<YarnBox<'_, Buf>> for Vec<u8> | |
171 | where | |
172 | Buf: crate::Buf + ?Sized, | |
173 | { | |
174 | fn from(y: YarnBox<Buf>) -> Self { | |
175 | y.into_vec() | |
176 | } | |
177 | } | |
178 | ||
179 | impl<Buf> From<YarnRef<'_, Buf>> for Vec<u8> | |
180 | where | |
181 | Buf: crate::Buf + ?Sized, | |
182 | { | |
183 | fn from(y: YarnRef<Buf>) -> Self { | |
184 | y.to_vec() | |
185 | } | |
186 | } | |
187 | ||
188 | impl From<YarnBox<'_, str>> for Box<str> { | |
189 | fn from(y: YarnBox<str>) -> Self { | |
190 | y.into_boxed_str() | |
191 | } | |
192 | } | |
193 | ||
194 | impl From<YarnRef<'_, str>> for Box<str> { | |
195 | fn from(y: YarnRef<str>) -> Self { | |
196 | y.to_boxed_str() | |
197 | } | |
198 | } | |
199 | ||
200 | impl From<YarnBox<'_, str>> for String { | |
201 | fn from(y: YarnBox<str>) -> Self { | |
202 | y.into_string() | |
203 | } | |
204 | } | |
205 | ||
206 | impl From<YarnRef<'_, str>> for String { | |
207 | fn from(y: YarnRef<str>) -> Self { | |
208 | y.to_string() | |
209 | } | |
210 | } | |
211 | ||
212 | // AsRef / Borrow | |
213 | ||
214 | impl<Buf> AsRef<Buf> for YarnBox<'_, Buf> | |
215 | where | |
216 | Buf: crate::Buf + ?Sized, | |
217 | { | |
218 | fn as_ref(&self) -> &Buf { | |
219 | self.as_slice() | |
220 | } | |
221 | } | |
222 | ||
223 | impl<Buf> AsRef<Buf> for YarnRef<'_, Buf> | |
224 | where | |
225 | Buf: crate::Buf + ?Sized, | |
226 | { | |
227 | fn as_ref(&self) -> &Buf { | |
228 | self.as_slice() | |
229 | } | |
230 | } | |
231 | ||
232 | impl<Buf> Borrow<Buf> for YarnBox<'_, Buf> | |
233 | where | |
234 | Buf: crate::Buf + ?Sized, | |
235 | { | |
236 | fn borrow(&self) -> &Buf { | |
237 | self.as_slice() | |
238 | } | |
239 | } | |
240 | ||
241 | impl<Buf> Borrow<Buf> for YarnRef<'_, Buf> | |
242 | where | |
243 | Buf: crate::Buf + ?Sized, | |
244 | { | |
245 | fn borrow(&self) -> &Buf { | |
246 | self.as_slice() | |
247 | } | |
248 | } |