Debian Codegraph @

debian/rust-vendor/prettyplease/examples/output.rustc.rs

raw
1 use crate::cmp::Ordering;use crate::fmt::{self, Write as FmtWrite};
2 use crate::hash;
3 use crate::io::Write as IoWrite;
4 use crate::mem::transmute;
5 use crate::sys::net::netc as c;
6 use crate::sys_common::{AsInner, FromInner, IntoInner};
7 #[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
8 pub enum IpAddr { V4(Ipv4Addr), V6(Ipv6Addr), }
9 #[derive(Copy)]
10 pub struct Ipv4Addr {
11 inner: c::in_addr,
12 }
13 #[derive(Copy)]
14 pub struct Ipv6Addr {
15 inner: c::in6_addr,
16 }
17 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
18 #[non_exhaustive]
19 pub enum Ipv6MulticastScope {
20 InterfaceLocal,
21 LinkLocal,
22 RealmLocal,
23 AdminLocal,
24 SiteLocal,
25 OrganizationLocal,
26 Global,
27 }
28 impl IpAddr {
29 pub const fn is_unspecified(&self) -> bool {
30 match self {
31 IpAddr::V4(ip) => ip.is_unspecified(),
32 IpAddr::V6(ip) => ip.is_unspecified(),
33 }
34 }
35 pub const fn is_loopback(&self) -> bool {
36 match self {
37 IpAddr::V4(ip) => ip.is_loopback(),
38 IpAddr::V6(ip) => ip.is_loopback(),
39 }
40 }
41 pub const fn is_global(&self) -> bool {
42 match self {
43 IpAddr::V4(ip) => ip.is_global(),
44 IpAddr::V6(ip) => ip.is_global(),
45 }
46 }
47 pub const fn is_multicast(&self) -> bool {
48 match self {
49 IpAddr::V4(ip) => ip.is_multicast(),
50 IpAddr::V6(ip) => ip.is_multicast(),
51 }
52 }
53 pub const fn is_documentation(&self) -> bool {
54 match self {
55 IpAddr::V4(ip) => ip.is_documentation(),
56 IpAddr::V6(ip) => ip.is_documentation(),
57 }
58 }
59 pub const fn is_benchmarking(&self) -> bool {
60 match self {
61 IpAddr::V4(ip) => ip.is_benchmarking(),
62 IpAddr::V6(ip) => ip.is_benchmarking(),
63 }
64 }
65 pub const fn is_ipv4(&self) -> bool { matches!(self, IpAddr :: V4(_)) }
66 pub const fn is_ipv6(&self) -> bool { matches!(self, IpAddr :: V6(_)) }
67 pub const fn to_canonical(&self) -> IpAddr {
68 match self {
69 &v4 @ IpAddr::V4(_) => v4,
70 IpAddr::V6(v6) => v6.to_canonical(),
71 }
72 }
73 }
74 impl Ipv4Addr {
75 pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
76 Ipv4Addr {
77 inner: c::in_addr { s_addr: u32::from_ne_bytes([a, b, c, d]) },
78 }
79 }
80 pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
81 #[doc(alias = "INADDR_ANY")]
82 pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
83 pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
84 pub const fn octets(&self) -> [u8; 4] { self.inner.s_addr.to_ne_bytes() }
85 pub const fn is_unspecified(&self) -> bool { self.inner.s_addr == 0 }
86 pub const fn is_loopback(&self) -> bool { self.octets()[0] == 127 }
87 pub const fn is_private(&self) -> bool {
88 match self.octets() {
89 [10, ..] => true,
90 [172, b, ..] if b >= 16 && b <= 31 => true,
91 [192, 168, ..] => true,
92 _ => false,
93 }
94 }
95 pub const fn is_link_local(&self) -> bool {
96 matches!(self.octets(), [169, 254, ..])
97 }
98 pub const fn is_global(&self) -> bool {
99 if u32::from_be_bytes(self.octets()) == 0xc0000009 ||
100 u32::from_be_bytes(self.octets()) == 0xc000000a {
101 return true;
102 }
103 !self.is_private() && !self.is_loopback() && !self.is_link_local() &&
104 !self.is_broadcast() && !self.is_documentation() &&
105 !self.is_shared() &&
106 !(self.octets()[0] == 192 && self.octets()[1] == 0 &&
107 self.octets()[2] == 0) && !self.is_reserved() &&
108 !self.is_benchmarking() && self.octets()[0] != 0
109 }
110 pub const fn is_shared(&self) -> bool {
111 self.octets()[0] == 100 &&
112 (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
113 }
114 pub const fn is_benchmarking(&self) -> bool {
115 self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
116 }
117 pub const fn is_reserved(&self) -> bool {
118 self.octets()[0] & 240 == 240 && !self.is_broadcast()
119 }
120 pub const fn is_multicast(&self) -> bool {
121 self.octets()[0] >= 224 && self.octets()[0] <= 239
122 }
123 pub const fn is_broadcast(&self) -> bool {
124 u32::from_be_bytes(self.octets()) ==
125 u32::from_be_bytes(Self::BROADCAST.octets())
126 }
127 pub const fn is_documentation(&self) -> bool {
128 matches!(self.octets(), [192, 0, 2, _] | [198, 51, 100, _] |
129 [203, 0, 113, _])
130 }
131 pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
132 let [a, b, c, d] = self.octets();
133 Ipv6Addr {
134 inner: c::in6_addr {
135 s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d],
136 },
137 }
138 }
139 pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
140 let [a, b, c, d] = self.octets();
141 Ipv6Addr {
142 inner: c::in6_addr {
143 s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c,
144 d],
145 },
146 }
147 }
148 }
149 impl fmt::Display for IpAddr {
150 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
151 match self {
152 IpAddr::V4(ip) => ip.fmt(fmt),
153 IpAddr::V6(ip) => ip.fmt(fmt),
154 }
155 }
156 }
157 impl fmt::Debug for IpAddr {
158 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
159 fmt::Display::fmt(self, fmt)
160 }
161 }
162 impl From<Ipv4Addr> for IpAddr {
163 fn from(ipv4: Ipv4Addr) -> IpAddr { IpAddr::V4(ipv4) }
164 }
165 impl From<Ipv6Addr> for IpAddr {
166 fn from(ipv6: Ipv6Addr) -> IpAddr { IpAddr::V6(ipv6) }
167 }
168 impl fmt::Display for Ipv4Addr {
169 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
170 let octets = self.octets();
171 if fmt.precision().is_none() && fmt.width().is_none() {
172 write!(fmt, "{}.{}.{}.{}", octets [0], octets [1], octets [2],
173 octets [3])
174 } else {
175 const IPV4_BUF_LEN: usize = 15;
176 let mut buf = [0u8; IPV4_BUF_LEN];
177 let mut buf_slice = &mut buf[..];
178 write!(buf_slice, "{}.{}.{}.{}", octets [0], octets [1], octets
179 [2], octets [3]).unwrap();
180 let len = IPV4_BUF_LEN - buf_slice.len();
181 let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
182 fmt.pad(buf)
183 }
184 }
185 }
186 impl fmt::Debug for Ipv4Addr {
187 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
188 fmt::Display::fmt(self, fmt)
189 }
190 }
191 impl Clone for Ipv4Addr {
192 fn clone(&self) -> Ipv4Addr { *self }
193 }
194 impl PartialEq for Ipv4Addr {
195 fn eq(&self, other: &Ipv4Addr) -> bool {
196 self.inner.s_addr == other.inner.s_addr
197 }
198 }
199 impl PartialEq<Ipv4Addr> for IpAddr {
200 fn eq(&self, other: &Ipv4Addr) -> bool {
201 match self { IpAddr::V4(v4) => v4 == other, IpAddr::V6(_) => false, }
202 }
203 }
204 impl PartialEq<IpAddr> for Ipv4Addr {
205 fn eq(&self, other: &IpAddr) -> bool {
206 match other { IpAddr::V4(v4) => self == v4, IpAddr::V6(_) => false, }
207 }
208 }
209 impl Eq for Ipv4Addr {}
210 impl hash::Hash for Ipv4Addr {
211 fn hash<H: hash::Hasher>(&self, s: &mut H) {
212 { self.inner.s_addr }.hash(s)
213 }
214 }
215 impl PartialOrd for Ipv4Addr {
216 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
217 Some(self.cmp(other))
218 }
219 }
220 impl PartialOrd<Ipv4Addr> for IpAddr {
221 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
222 match self {
223 IpAddr::V4(v4) => v4.partial_cmp(other),
224 IpAddr::V6(_) => Some(Ordering::Greater),
225 }
226 }
227 }
228 impl PartialOrd<IpAddr> for Ipv4Addr {
229 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
230 match other {
231 IpAddr::V4(v4) => self.partial_cmp(v4),
232 IpAddr::V6(_) => Some(Ordering::Less),
233 }
234 }
235 }
236 impl Ord for Ipv4Addr {
237 fn cmp(&self, other: &Ipv4Addr) -> Ordering {
238 u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
239 }
240 }
241 impl IntoInner<c::in_addr> for Ipv4Addr {
242 fn into_inner(self) -> c::in_addr { self.inner }
243 }
244 impl From<Ipv4Addr> for u32 {
245 fn from(ip: Ipv4Addr) -> u32 {
246 let ip = ip.octets();
247 u32::from_be_bytes(ip)
248 }
249 }
250 impl From<u32> for Ipv4Addr {
251 fn from(ip: u32) -> Ipv4Addr { Ipv4Addr::from(ip.to_be_bytes()) }
252 }
253 impl From<[u8; 4]> for Ipv4Addr {
254 fn from(octets: [u8; 4]) -> Ipv4Addr {
255 Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
256 }
257 }
258 impl From<[u8; 4]> for IpAddr {
259 fn from(octets: [u8; 4]) -> IpAddr { IpAddr::V4(Ipv4Addr::from(octets)) }
260 }
261 impl Ipv6Addr {
262 pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
263 h: u16) -> Ipv6Addr {
264 let addr16 =
265 [a.to_be(), b.to_be(), c.to_be(), d.to_be(), e.to_be(), f.to_be(),
266 g.to_be(), h.to_be()];
267 Ipv6Addr {
268 inner: c::in6_addr {
269 s6_addr: unsafe { transmute::<_, [u8; 16]>(addr16) },
270 },
271 }
272 }
273 pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
274 pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
275 pub const fn segments(&self) -> [u16; 8] {
276 let [a, b, c, d, e, f, g, h] =
277 unsafe { transmute::<_, [u16; 8]>(self.inner.s6_addr) };
278 [u16::from_be(a), u16::from_be(b), u16::from_be(c), u16::from_be(d),
279 u16::from_be(e), u16::from_be(f), u16::from_be(g),
280 u16::from_be(h)]
281 }
282 pub const fn is_unspecified(&self) -> bool {
283 u128::from_be_bytes(self.octets()) ==
284 u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
285 }
286 pub const fn is_loopback(&self) -> bool {
287 u128::from_be_bytes(self.octets()) ==
288 u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
289 }
290 pub const fn is_global(&self) -> bool {
291 match self.multicast_scope() {
292 Some(Ipv6MulticastScope::Global) => true,
293 None => self.is_unicast_global(),
294 _ => false,
295 }
296 }
297 pub const fn is_unique_local(&self) -> bool {
298 (self.segments()[0] & 0xfe00) == 0xfc00
299 }
300 pub const fn is_unicast(&self) -> bool { !self.is_multicast() }
301 pub const fn is_unicast_link_local(&self) -> bool {
302 (self.segments()[0] & 0xffc0) == 0xfe80
303 }
304 pub const fn is_documentation(&self) -> bool {
305 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
306 }
307 pub const fn is_benchmarking(&self) -> bool {
308 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) &&
309 (self.segments()[2] == 0)
310 }
311 pub const fn is_unicast_global(&self) -> bool {
312 self.is_unicast() && !self.is_loopback() &&
313 !self.is_unicast_link_local() && !self.is_unique_local() &&
314 !self.is_unspecified() && !self.is_documentation()
315 }
316 pub const fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
317 if self.is_multicast() {
318 match self.segments()[0] & 0x000f {
319 1 => Some(Ipv6MulticastScope::InterfaceLocal),
320 2 => Some(Ipv6MulticastScope::LinkLocal),
321 3 => Some(Ipv6MulticastScope::RealmLocal),
322 4 => Some(Ipv6MulticastScope::AdminLocal),
323 5 => Some(Ipv6MulticastScope::SiteLocal),
324 8 => Some(Ipv6MulticastScope::OrganizationLocal),
325 14 => Some(Ipv6MulticastScope::Global),
326 _ => None,
327 }
328 } else { None }
329 }
330 pub const fn is_multicast(&self) -> bool {
331 (self.segments()[0] & 0xff00) == 0xff00
332 }
333 pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
334 match self.octets() {
335 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
336 Some(Ipv4Addr::new(a, b, c, d))
337 }
338 _ => None,
339 }
340 }
341 pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
342 if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
343 let [a, b] = ab.to_be_bytes();
344 let [c, d] = cd.to_be_bytes();
345 Some(Ipv4Addr::new(a, b, c, d))
346 } else { None }
347 }
348 pub const fn to_canonical(&self) -> IpAddr {
349 if let Some(mapped) = self.to_ipv4_mapped() {
350 return IpAddr::V4(mapped);
351 }
352 IpAddr::V6(*self)
353 }
354 pub const fn octets(&self) -> [u8; 16] { self.inner.s6_addr }
355 }
356 impl fmt::Display for Ipv6Addr {
357 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
358 if f.precision().is_none() && f.width().is_none() {
359 let segments = self.segments();
360 if self.is_unspecified() {
361 f.write_str("::")
362 } else if self.is_loopback() {
363 f.write_str("::1")
364 } else if let Some(ipv4) = self.to_ipv4() {
365 match segments[5] {
366 0 => write!(f, "::{}", ipv4),
367 0xffff => write!(f, "::ffff:{}", ipv4),
368 _ => unreachable!(),
369 }
370 } else {
371 #[derive(Copy, Clone, Default)]
372 struct Span {
373 start: usize,
374 len: usize,
375 }
376 let zeroes =
377 {
378 let mut longest = Span::default();
379 let mut current = Span::default();
380 for (i, &segment) in segments.iter().enumerate() {
381 if segment == 0 {
382 if current.len == 0 { current.start = i; }
383 current.len += 1;
384 if current.len > longest.len { longest = current; }
385 } else { current = Span::default(); }
386 }
387 longest
388 };
389 #[doc = " Write a colon-separated part of the address"]
390 #[inline]
391 fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16])
392 -> fmt::Result {
393 if let Some((first, tail)) = chunk.split_first() {
394 write!(f, "{:x}", first)?;
395 for segment in tail {
396 f.write_char(':')?;
397 write!(f, "{:x}", segment)?;
398 }
399 }
400 Ok(())
401 }
402 if zeroes.len > 1 {
403 fmt_subslice(f, &segments[..zeroes.start])?;
404 f.write_str("::")?;
405 fmt_subslice(f, &segments[zeroes.start + zeroes.len..])
406 } else { fmt_subslice(f, &segments) }
407 }
408 } else {
409 const IPV6_BUF_LEN: usize = (4 * 8) + 7;
410 let mut buf = [0u8; IPV6_BUF_LEN];
411 let mut buf_slice = &mut buf[..];
412 write!(buf_slice, "{}", self).unwrap();
413 let len = IPV6_BUF_LEN - buf_slice.len();
414 let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
415 f.pad(buf)
416 }
417 }
418 }
419 impl fmt::Debug for Ipv6Addr {
420 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
421 fmt::Display::fmt(self, fmt)
422 }
423 }
424 impl Clone for Ipv6Addr {
425 fn clone(&self) -> Ipv6Addr { *self }
426 }
427 impl PartialEq for Ipv6Addr {
428 fn eq(&self, other: &Ipv6Addr) -> bool {
429 self.inner.s6_addr == other.inner.s6_addr
430 }
431 }
432 impl PartialEq<IpAddr> for Ipv6Addr {
433 fn eq(&self, other: &IpAddr) -> bool {
434 match other { IpAddr::V4(_) => false, IpAddr::V6(v6) => self == v6, }
435 }
436 }
437 impl PartialEq<Ipv6Addr> for IpAddr {
438 fn eq(&self, other: &Ipv6Addr) -> bool {
439 match self { IpAddr::V4(_) => false, IpAddr::V6(v6) => v6 == other, }
440 }
441 }
442 impl Eq for Ipv6Addr {}
443 impl hash::Hash for Ipv6Addr {
444 fn hash<H: hash::Hasher>(&self, s: &mut H) { self.inner.s6_addr.hash(s) }
445 }
446 impl PartialOrd for Ipv6Addr {
447 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
448 Some(self.cmp(other))
449 }
450 }
451 impl PartialOrd<Ipv6Addr> for IpAddr {
452 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
453 match self {
454 IpAddr::V4(_) => Some(Ordering::Less),
455 IpAddr::V6(v6) => v6.partial_cmp(other),
456 }
457 }
458 }
459 impl PartialOrd<IpAddr> for Ipv6Addr {
460 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
461 match other {
462 IpAddr::V4(_) => Some(Ordering::Greater),
463 IpAddr::V6(v6) => self.partial_cmp(v6),
464 }
465 }
466 }
467 impl Ord for Ipv6Addr {
468 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
469 self.segments().cmp(&other.segments())
470 }
471 }
472 impl AsInner<c::in6_addr> for Ipv6Addr {
473 fn as_inner(&self) -> &c::in6_addr { &self.inner }
474 }
475 impl FromInner<c::in6_addr> for Ipv6Addr {
476 fn from_inner(addr: c::in6_addr) -> Ipv6Addr { Ipv6Addr { inner: addr } }
477 }
478 impl From<Ipv6Addr> for u128 {
479 fn from(ip: Ipv6Addr) -> u128 {
480 let ip = ip.octets();
481 u128::from_be_bytes(ip)
482 }
483 }
484 impl From<u128> for Ipv6Addr {
485 fn from(ip: u128) -> Ipv6Addr { Ipv6Addr::from(ip.to_be_bytes()) }
486 }
487 impl From<[u8; 16]> for Ipv6Addr {
488 fn from(octets: [u8; 16]) -> Ipv6Addr {
489 let inner = c::in6_addr { s6_addr: octets };
490 Ipv6Addr::from_inner(inner)
491 }
492 }
493 impl From<[u16; 8]> for Ipv6Addr {
494 fn from(segments: [u16; 8]) -> Ipv6Addr {
495 let [a, b, c, d, e, f, g, h] = segments;
496 Ipv6Addr::new(a, b, c, d, e, f, g, h)
497 }
498 }
499 impl From<[u8; 16]> for IpAddr {
500 fn from(octets: [u8; 16]) -> IpAddr { IpAddr::V6(Ipv6Addr::from(octets)) }
501 }
502 impl From<[u16; 8]> for IpAddr {
503 fn from(segments: [u16; 8]) -> IpAddr {
504 IpAddr::V6(Ipv6Addr::from(segments))
505 }
506 }