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