1use super::display_buffer::DisplayBuffer;
2use crate::cmp::Ordering;
3use crate::fmt::{self, Write};
4use crate::hash::{Hash, Hasher};
5use crate::mem::transmute;
6use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
7
8#[rustc_diagnostic_item = "IpAddr"]
28#[stable(feature = "ip_addr", since = "1.7.0")]
29#[derive(#[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::marker::Copy for IpAddr { }Copy, #[automatically_derived]#[doc(hidden)]#[stable(feature = "ip_addr", since =
"1.7.0")]
unsafe impl crate::clone::TrivialClone for IpAddr { }
#[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::clone::Clone for IpAddr {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: crate::clone::AssertParamIsClone<Ipv4Addr>;
let _: crate::clone::AssertParamIsClone<Ipv6Addr>;
*self
}
}Clone, #[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::cmp::Eq for IpAddr {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<Ipv4Addr>;
let _: crate::cmp::AssertParamIsEq<Ipv6Addr>;
}
}Eq, #[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::marker::StructuralPartialEq for IpAddr { }
#[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::cmp::PartialEq for IpAddr {
#[inline]
fn eq(&self, other: &Self) -> bool {
crate::intrinsics::discriminant_value(self) ==
crate::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::V4(__self_0), Self::V4(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::V6(__self_0), Self::V6(__arg1_0)) =>
__self_0 == __arg1_0,
_ => unsafe { crate::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::hash::Hash for IpAddr {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&crate::intrinsics::discriminant_value(self),
state);
match self {
Self::V4(__self_0) => crate::hash::Hash::hash(__self_0, state),
Self::V6(__self_0) => crate::hash::Hash::hash(__self_0, state),
}
}
}Hash, #[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::cmp::PartialOrd for IpAddr {
#[inline]
fn partial_cmp(&self, other: &Self)
-> crate::option::Option<crate::cmp::Ordering> {
crate::option::Option::Some(crate::cmp::Ord::cmp(self, other))
}
}PartialOrd, #[automatically_derived]#[stable(feature = "ip_addr", since = "1.7.0")]
impl crate::cmp::Ord for IpAddr {
#[inline]
fn cmp(&self, other: &Self) -> crate::cmp::Ordering {
match (self, other) {
(Self::V4(__self_0), Self::V4(__arg1_0)) =>
crate::cmp::Ord::cmp(__self_0, __arg1_0),
(Self::V6(__self_0), Self::V6(__arg1_0)) =>
crate::cmp::Ord::cmp(__self_0, __arg1_0),
_ =>
crate::cmp::Ord::cmp(&crate::intrinsics::discriminant_value(self),
&crate::intrinsics::discriminant_value(other)),
}
}
}Ord)]
30pub enum IpAddr {
31 #[stable(feature = "ip_addr", since = "1.7.0")]
33 V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
34 #[stable(feature = "ip_addr", since = "1.7.0")]
36 V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
37}
38
39#[rustc_diagnostic_item = "Ipv4Addr"]
71#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::marker::Copy for Ipv4Addr { }Copy)]
72#[derive_const(#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[doc(hidden)]#[stable(feature
= "rust1", since = "1.0.0")]
const unsafe impl crate::clone::TrivialClone for Ipv4Addr { }
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "rust1", since = "1.0.0")]
const impl crate::clone::Clone for Ipv4Addr {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: crate::clone::AssertParamIsClone<[u8; 4]>;
*self
}
}Clone, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::marker::StructuralPartialEq for Ipv4Addr { }
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "rust1", since = "1.0.0")]
const impl crate::cmp::PartialEq for Ipv4Addr {
#[inline]
fn eq(&self, other: &Self) -> bool { self.octets == other.octets }
}PartialEq, #[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "rust1", since = "1.0.0")]
const impl crate::cmp::Eq for Ipv4Addr {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<[u8; 4]>;
}
}Eq)]
73#[stable(feature = "rust1", since = "1.0.0")]
74pub struct Ipv4Addr {
75 octets: [u8; 4],
76}
77
78#[stable(feature = "rust1", since = "1.0.0")]
79impl Hash for Ipv4Addr {
80 fn hash<H: Hasher>(&self, state: &mut H) {
81 u32::from_ne_bytes(self.octets).hash(state);
85 }
86}
87
88#[rustc_diagnostic_item = "Ipv6Addr"]
167#[derive(#[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::marker::Copy for Ipv6Addr { }Copy)]
168#[derive_const(#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[doc(hidden)]#[stable(feature
= "rust1", since = "1.0.0")]
const unsafe impl crate::clone::TrivialClone for Ipv6Addr { }
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "rust1", since = "1.0.0")]
const impl crate::clone::Clone for Ipv6Addr {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: crate::clone::AssertParamIsClone<[u8; 16]>;
*self
}
}Clone, #[automatically_derived]#[stable(feature = "rust1", since = "1.0.0")]
impl crate::marker::StructuralPartialEq for Ipv6Addr { }
#[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "rust1", since = "1.0.0")]
const impl crate::cmp::PartialEq for Ipv6Addr {
#[inline]
fn eq(&self, other: &Self) -> bool { self.octets == other.octets }
}PartialEq, #[automatically_derived]#[rustc_const_unstable(feature = "derive_const", issue = "143874")]#[stable(feature
= "rust1", since = "1.0.0")]
const impl crate::cmp::Eq for Ipv6Addr {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: crate::cmp::AssertParamIsEq<[u8; 16]>;
}
}Eq)]
169#[stable(feature = "rust1", since = "1.0.0")]
170pub struct Ipv6Addr {
171 octets: [u8; 16],
172}
173
174#[stable(feature = "rust1", since = "1.0.0")]
175impl Hash for Ipv6Addr {
176 fn hash<H: Hasher>(&self, state: &mut H) {
177 u128::from_ne_bytes(self.octets).hash(state);
181 }
182}
183
184#[derive(#[automatically_derived]#[doc(hidden)]#[unstable(feature = "ip", issue =
"27709")]
unsafe impl crate::clone::TrivialClone for Ipv6MulticastScope { }
#[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::clone::Clone for Ipv6MulticastScope {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::marker::Copy for Ipv6MulticastScope { }Copy, #[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::fmt::Debug for Ipv6MulticastScope {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_c_like_enum_write_str(f,
"Reserved0 InterfaceLocal LinkLocal RealmLocal AdminLocal SiteLocal Unassigned6 Unassigned7 OrganizationLocal Unassigned9 UnassignedA UnassignedB UnassignedC UnassignedD Global ReservedF",
match self {
Ipv6MulticastScope::Reserved0 => 0usize,
Ipv6MulticastScope::InterfaceLocal => 10usize,
Ipv6MulticastScope::LinkLocal => 25usize,
Ipv6MulticastScope::RealmLocal => 35usize,
Ipv6MulticastScope::AdminLocal => 46usize,
Ipv6MulticastScope::SiteLocal => 57usize,
Ipv6MulticastScope::Unassigned6 => 67usize,
Ipv6MulticastScope::Unassigned7 => 79usize,
Ipv6MulticastScope::OrganizationLocal => 91usize,
Ipv6MulticastScope::Unassigned9 => 109usize,
Ipv6MulticastScope::UnassignedA => 121usize,
Ipv6MulticastScope::UnassignedB => 133usize,
Ipv6MulticastScope::UnassignedC => 145usize,
Ipv6MulticastScope::UnassignedD => 157usize,
Ipv6MulticastScope::Global => 169usize,
Ipv6MulticastScope::ReservedF => 176usize,
})
}
}Debug, #[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::marker::StructuralPartialEq for Ipv6MulticastScope { }
#[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::cmp::PartialEq for Ipv6MulticastScope {
#[inline]
fn eq(&self, other: &Self) -> bool {
crate::intrinsics::discriminant_value(self) ==
crate::intrinsics::discriminant_value(other)
}
}PartialEq, #[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::cmp::Eq for Ipv6MulticastScope { }Eq, #[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::cmp::PartialOrd for Ipv6MulticastScope {
#[inline]
fn partial_cmp(&self, other: &Self)
-> crate::option::Option<crate::cmp::Ordering> {
crate::option::Option::Some(crate::cmp::Ord::cmp(self, other))
}
}PartialOrd, #[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::cmp::Ord for Ipv6MulticastScope {
#[inline]
fn cmp(&self, other: &Self) -> crate::cmp::Ordering {
crate::cmp::Ord::cmp(&crate::intrinsics::discriminant_value(self),
&crate::intrinsics::discriminant_value(other))
}
}Ord, #[automatically_derived]#[unstable(feature = "ip", issue = "27709")]
impl crate::hash::Hash for Ipv6MulticastScope {
#[inline]
fn hash<__H: crate::hash::Hasher>(&self, state: &mut __H) {
crate::hash::Hash::hash(&crate::intrinsics::discriminant_value(self),
state)
}
}Hash)]
229#[unstable(feature = "ip", issue = "27709")]
230pub enum Ipv6MulticastScope {
231 #[doc(hidden)]
233 #[unstable(
234 feature = "ip_multicast_reserved",
235 reason = "not yet assigned by IETF",
236 issue = "none"
237 )]
238 Reserved0 = 0x0,
239 InterfaceLocal = 0x1,
241 LinkLocal = 0x2,
243 RealmLocal = 0x3,
245 AdminLocal = 0x4,
247 SiteLocal = 0x5,
249
250 Unassigned6 = 0x6,
253 Unassigned7 = 0x7,
256 OrganizationLocal = 0x8,
258 Unassigned9 = 0x9,
261 UnassignedA = 0xA,
264 UnassignedB = 0xB,
267 UnassignedC = 0xC,
270 UnassignedD = 0xD,
273 Global = 0xE,
275 #[doc(hidden)]
277 #[unstable(
278 feature = "ip_multicast_reserved",
279 reason = "not yet assigned by IETF",
280 issue = "none"
281 )]
282 ReservedF = 0xF,
283}
284
285impl IpAddr {
286 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
300 #[stable(feature = "ip_shared", since = "1.12.0")]
301 #[must_use]
302 #[inline]
303 pub const fn is_unspecified(&self) -> bool {
304 match self {
305 IpAddr::V4(ip) => ip.is_unspecified(),
306 IpAddr::V6(ip) => ip.is_unspecified(),
307 }
308 }
309
310 #[inline]
330 #[must_use]
331 #[unstable(feature = "addr_unspecified_from", issue = "158975")]
332 pub const fn unspecified_from(this: Self) -> Self {
333 match this {
334 Self::V4(_) => Self::V4(Ipv4Addr::UNSPECIFIED),
335 Self::V6(_) => Self::V6(Ipv6Addr::UNSPECIFIED),
336 }
337 }
338
339 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
353 #[stable(feature = "ip_shared", since = "1.12.0")]
354 #[must_use]
355 #[inline]
356 pub const fn is_loopback(&self) -> bool {
357 match self {
358 IpAddr::V4(ip) => ip.is_loopback(),
359 IpAddr::V6(ip) => ip.is_loopback(),
360 }
361 }
362
363 #[unstable(feature = "ip", issue = "27709")]
379 #[must_use]
380 #[inline]
381 pub const fn is_global(&self) -> bool {
382 match self {
383 IpAddr::V4(ip) => ip.is_global(),
384 IpAddr::V6(ip) => ip.is_global(),
385 }
386 }
387
388 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
402 #[stable(feature = "ip_shared", since = "1.12.0")]
403 #[must_use]
404 #[inline]
405 pub const fn is_multicast(&self) -> bool {
406 match self {
407 IpAddr::V4(ip) => ip.is_multicast(),
408 IpAddr::V6(ip) => ip.is_multicast(),
409 }
410 }
411
412 #[unstable(feature = "ip", issue = "27709")]
431 #[must_use]
432 #[inline]
433 pub const fn is_documentation(&self) -> bool {
434 match self {
435 IpAddr::V4(ip) => ip.is_documentation(),
436 IpAddr::V6(ip) => ip.is_documentation(),
437 }
438 }
439
440 #[unstable(feature = "ip", issue = "27709")]
456 #[must_use]
457 #[inline]
458 pub const fn is_benchmarking(&self) -> bool {
459 match self {
460 IpAddr::V4(ip) => ip.is_benchmarking(),
461 IpAddr::V6(ip) => ip.is_benchmarking(),
462 }
463 }
464
465 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
479 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
480 #[must_use]
481 #[inline]
482 pub const fn is_ipv4(&self) -> bool {
483 #[allow(non_exhaustive_omitted_patterns)] match self {
IpAddr::V4(_) => true,
_ => false,
}matches!(self, IpAddr::V4(_))
484 }
485
486 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
500 #[stable(feature = "ipaddr_checker", since = "1.16.0")]
501 #[must_use]
502 #[inline]
503 pub const fn is_ipv6(&self) -> bool {
504 #[allow(non_exhaustive_omitted_patterns)] match self {
IpAddr::V6(_) => true,
_ => false,
}matches!(self, IpAddr::V6(_))
505 }
506
507 #[inline]
524 #[must_use = "this returns the result of the operation, \
525 without modifying the original"]
526 #[stable(feature = "ip_to_canonical", since = "1.75.0")]
527 #[rustc_const_stable(feature = "ip_to_canonical", since = "1.75.0")]
528 pub const fn to_canonical(&self) -> IpAddr {
529 match self {
530 IpAddr::V4(_) => *self,
531 IpAddr::V6(v6) => v6.to_canonical(),
532 }
533 }
534
535 #[unstable(feature = "ip_as_octets", issue = "137259")]
549 #[must_use]
550 #[inline]
551 pub const fn as_octets(&self) -> &[u8] {
552 match self {
553 IpAddr::V4(ip) => ip.as_octets().as_slice(),
554 IpAddr::V6(ip) => ip.as_octets().as_slice(),
555 }
556 }
557}
558
559impl Ipv4Addr {
560 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
572 #[stable(feature = "rust1", since = "1.0.0")]
573 #[must_use]
574 #[inline]
575 pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
576 Ipv4Addr { octets: [a, b, c, d] }
577 }
578
579 #[stable(feature = "ip_bits", since = "1.80.0")]
589 pub const BITS: u32 = 32;
590
591 #[rustc_const_stable(feature = "ip_bits", since = "1.80.0")]
617 #[stable(feature = "ip_bits", since = "1.80.0")]
618 #[must_use]
619 #[inline]
620 pub const fn to_bits(self) -> u32 {
621 u32::from_be_bytes(self.octets)
622 }
623
624 #[rustc_const_stable(feature = "ip_bits", since = "1.80.0")]
637 #[stable(feature = "ip_bits", since = "1.80.0")]
638 #[must_use]
639 #[inline]
640 pub const fn from_bits(bits: u32) -> Ipv4Addr {
641 Ipv4Addr { octets: bits.to_be_bytes() }
642 }
643
644 #[stable(feature = "ip_constructors", since = "1.30.0")]
655 pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
656
657 #[doc(alias = "INADDR_ANY")]
670 #[stable(feature = "ip_constructors", since = "1.30.0")]
671 pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
672
673 #[stable(feature = "ip_constructors", since = "1.30.0")]
684 pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
685
686 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
697 #[stable(feature = "rust1", since = "1.0.0")]
698 #[must_use]
699 #[inline]
700 pub const fn octets(&self) -> [u8; 4] {
701 self.octets
702 }
703
704 #[stable(feature = "ip_from", since = "1.91.0")]
715 #[rustc_const_stable(feature = "ip_from", since = "1.91.0")]
716 #[must_use]
717 #[inline]
718 pub const fn from_octets(octets: [u8; 4]) -> Ipv4Addr {
719 Ipv4Addr { octets }
720 }
721
722 #[unstable(feature = "ip_as_octets", issue = "137259")]
736 #[must_use]
737 #[inline]
738 pub const fn as_octets(&self) -> &[u8; 4] {
739 &self.octets
740 }
741
742 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
758 #[stable(feature = "ip_shared", since = "1.12.0")]
759 #[must_use]
760 #[inline]
761 pub const fn is_unspecified(&self) -> bool {
762 u32::from_be_bytes(self.octets) == 0
763 }
764
765 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
780 #[stable(since = "1.7.0", feature = "ip_17")]
781 #[must_use]
782 #[inline]
783 pub const fn is_loopback(&self) -> bool {
784 self.octets()[0] == 127
785 }
786
787 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
811 #[stable(since = "1.7.0", feature = "ip_17")]
812 #[must_use]
813 #[inline]
814 pub const fn is_private(&self) -> bool {
815 match self.octets() {
816 [10, ..] => true,
817 [172, b, ..] if b >= 16 && b <= 31 => true,
818 [192, 168, ..] => true,
819 _ => false,
820 }
821 }
822
823 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
839 #[stable(since = "1.7.0", feature = "ip_17")]
840 #[must_use]
841 #[inline]
842 pub const fn is_link_local(&self) -> bool {
843 #[allow(non_exhaustive_omitted_patterns)] match self.octets() {
[169, 254, ..] => true,
_ => false,
}matches!(self.octets(), [169, 254, ..])
844 }
845
846 #[unstable(feature = "ip", issue = "27709")]
918 #[must_use]
919 #[inline]
920 pub const fn is_global(&self) -> bool {
921 !(self.octets()[0] == 0 || self.is_private()
923 || self.is_shared()
924 || self.is_loopback()
925 || self.is_link_local()
926 || (
929 self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0
930 && self.octets()[3] != 9 && self.octets()[3] != 10
931 )
932 || #[allow(non_exhaustive_omitted_patterns)] match self.octets() {
[192, 88, 99, 2] => true,
_ => false,
}matches!(self.octets(), [192, 88, 99, 2])
934 || self.is_documentation()
935 || self.is_benchmarking()
936 || self.is_reserved()
937 || self.is_broadcast())
938 }
939
940 #[unstable(feature = "ip", issue = "27709")]
956 #[must_use]
957 #[inline]
958 pub const fn is_shared(&self) -> bool {
959 self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
960 }
961
962 #[unstable(feature = "ip", issue = "27709")]
983 #[must_use]
984 #[inline]
985 pub const fn is_benchmarking(&self) -> bool {
986 self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
987 }
988
989 #[unstable(feature = "ip", issue = "27709")]
1019 #[must_use]
1020 #[inline]
1021 pub const fn is_reserved(&self) -> bool {
1022 self.octets()[0] & 240 == 240 && !self.is_broadcast()
1023 }
1024
1025 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1042 #[stable(since = "1.7.0", feature = "ip_17")]
1043 #[must_use]
1044 #[inline]
1045 pub const fn is_multicast(&self) -> bool {
1046 self.octets()[0] >= 224 && self.octets()[0] <= 239
1047 }
1048
1049 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1064 #[stable(since = "1.7.0", feature = "ip_17")]
1065 #[must_use]
1066 #[inline]
1067 pub const fn is_broadcast(&self) -> bool {
1068 u32::from_be_bytes(self.octets()) == u32::from_be_bytes(Self::BROADCAST.octets())
1069 }
1070
1071 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1092 #[stable(since = "1.7.0", feature = "ip_17")]
1093 #[must_use]
1094 #[inline]
1095 pub const fn is_documentation(&self) -> bool {
1096 #[allow(non_exhaustive_omitted_patterns)] match self.octets() {
[192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _] => true,
_ => false,
}matches!(self.octets(), [192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _])
1097 }
1098
1099 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1120 #[stable(feature = "rust1", since = "1.0.0")]
1121 #[must_use = "this returns the result of the operation, \
1122 without modifying the original"]
1123 #[inline]
1124 pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
1125 let [a, b, c, d] = self.octets();
1126 Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] }
1127 }
1128
1129 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1145 #[stable(feature = "rust1", since = "1.0.0")]
1146 #[must_use = "this returns the result of the operation, \
1147 without modifying the original"]
1148 #[inline]
1149 pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
1150 let [a, b, c, d] = self.octets();
1151 Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d] }
1152 }
1153}
1154
1155#[stable(feature = "ip_addr", since = "1.7.0")]
1156impl fmt::Display for IpAddr {
1157 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1158 match self {
1159 IpAddr::V4(ip) => ip.fmt(fmt),
1160 IpAddr::V6(ip) => ip.fmt(fmt),
1161 }
1162 }
1163}
1164
1165#[stable(feature = "ip_addr", since = "1.7.0")]
1166impl fmt::Debug for IpAddr {
1167 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1168 fmt::Display::fmt(self, fmt)
1169 }
1170}
1171
1172#[stable(feature = "ip_from_ip", since = "1.16.0")]
1173#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1174const impl From<Ipv4Addr> for IpAddr {
1175 #[inline]
1190 fn from(ipv4: Ipv4Addr) -> IpAddr {
1191 IpAddr::V4(ipv4)
1192 }
1193}
1194
1195#[stable(feature = "ip_from_ip", since = "1.16.0")]
1196#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1197const impl From<Ipv6Addr> for IpAddr {
1198 #[inline]
1213 fn from(ipv6: Ipv6Addr) -> IpAddr {
1214 IpAddr::V6(ipv6)
1215 }
1216}
1217
1218#[stable(feature = "rust1", since = "1.0.0")]
1219impl fmt::Display for Ipv4Addr {
1220 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1221 let octets = self.octets();
1222
1223 if fmt.precision().is_none() && fmt.width().is_none() {
1226 fmt.write_fmt(format_args!("{0}.{1}.{2}.{3}", octets[0], octets[1], octets[2],
octets[3]))write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
1227 } else {
1228 const LONGEST_IPV4_ADDR: &str = "255.255.255.255";
1229
1230 let mut buf = DisplayBuffer::buffer::<{ LONGEST_IPV4_ADDR.len() }>();
1231 let mut buf = DisplayBuffer::new(&mut buf);
1232 buf.write_fmt(format_args!("{0}.{1}.{2}.{3}", octets[0], octets[1], octets[2],
octets[3]))write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
1234
1235 fmt.pad(buf.as_str())
1236 }
1237 }
1238}
1239
1240#[stable(feature = "rust1", since = "1.0.0")]
1241impl fmt::Debug for Ipv4Addr {
1242 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1243 fmt::Display::fmt(self, fmt)
1244 }
1245}
1246
1247#[stable(feature = "ip_cmp", since = "1.16.0")]
1248impl PartialEq<Ipv4Addr> for IpAddr {
1249 #[inline]
1250 fn eq(&self, other: &Ipv4Addr) -> bool {
1251 match self {
1252 IpAddr::V4(v4) => v4 == other,
1253 IpAddr::V6(_) => false,
1254 }
1255 }
1256}
1257
1258#[stable(feature = "ip_cmp", since = "1.16.0")]
1259impl PartialEq<IpAddr> for Ipv4Addr {
1260 #[inline]
1261 fn eq(&self, other: &IpAddr) -> bool {
1262 match other {
1263 IpAddr::V4(v4) => self == v4,
1264 IpAddr::V6(_) => false,
1265 }
1266 }
1267}
1268
1269#[stable(feature = "rust1", since = "1.0.0")]
1270#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1271const impl PartialOrd for Ipv4Addr {
1272 #[inline]
1273 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1274 Some(self.cmp(other))
1275 }
1276}
1277
1278#[stable(feature = "ip_cmp", since = "1.16.0")]
1279impl PartialOrd<Ipv4Addr> for IpAddr {
1280 #[inline]
1281 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
1282 match self {
1283 IpAddr::V4(v4) => v4.partial_cmp(other),
1284 IpAddr::V6(_) => Some(Ordering::Greater),
1285 }
1286 }
1287}
1288
1289#[stable(feature = "ip_cmp", since = "1.16.0")]
1290impl PartialOrd<IpAddr> for Ipv4Addr {
1291 #[inline]
1292 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
1293 match other {
1294 IpAddr::V4(v4) => self.partial_cmp(v4),
1295 IpAddr::V6(_) => Some(Ordering::Less),
1296 }
1297 }
1298}
1299
1300#[stable(feature = "rust1", since = "1.0.0")]
1301#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
1302const impl Ord for Ipv4Addr {
1303 #[inline]
1304 fn cmp(&self, other: &Ipv4Addr) -> Ordering {
1305 self.octets.cmp(&other.octets)
1306 }
1307}
1308
1309#[stable(feature = "ip_u32", since = "1.1.0")]
1310#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1311const impl From<Ipv4Addr> for u32 {
1312 #[inline]
1314 fn from(ip: Ipv4Addr) -> u32 {
1315 ip.to_bits()
1316 }
1317}
1318
1319#[stable(feature = "ip_u32", since = "1.1.0")]
1320#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1321const impl From<u32> for Ipv4Addr {
1322 #[inline]
1324 fn from(ip: u32) -> Ipv4Addr {
1325 Ipv4Addr::from_bits(ip)
1326 }
1327}
1328
1329#[stable(feature = "from_slice_v4", since = "1.9.0")]
1330#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1331const impl From<[u8; 4]> for Ipv4Addr {
1332 #[inline]
1343 fn from(octets: [u8; 4]) -> Ipv4Addr {
1344 Ipv4Addr { octets }
1345 }
1346}
1347
1348#[stable(feature = "ip_from_slice", since = "1.17.0")]
1349#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1350const impl From<[u8; 4]> for IpAddr {
1351 #[inline]
1362 fn from(octets: [u8; 4]) -> IpAddr {
1363 IpAddr::V4(Ipv4Addr::from(octets))
1364 }
1365}
1366
1367impl Ipv6Addr {
1368 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
1380 #[stable(feature = "rust1", since = "1.0.0")]
1381 #[must_use]
1382 #[inline]
1383 pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
1384 let addr16 = [
1385 a.to_be(),
1386 b.to_be(),
1387 c.to_be(),
1388 d.to_be(),
1389 e.to_be(),
1390 f.to_be(),
1391 g.to_be(),
1392 h.to_be(),
1393 ];
1394 Ipv6Addr {
1395 octets: unsafe { transmute::<_, [u8; 16]>(addr16) },
1398 }
1399 }
1400
1401 #[stable(feature = "ip_bits", since = "1.80.0")]
1411 pub const BITS: u32 = 128;
1412
1413 #[rustc_const_stable(feature = "ip_bits", since = "1.80.0")]
1450 #[stable(feature = "ip_bits", since = "1.80.0")]
1451 #[must_use]
1452 #[inline]
1453 pub const fn to_bits(self) -> u128 {
1454 u128::from_be_bytes(self.octets)
1455 }
1456
1457 #[rustc_const_stable(feature = "ip_bits", since = "1.80.0")]
1475 #[stable(feature = "ip_bits", since = "1.80.0")]
1476 #[must_use]
1477 #[inline]
1478 pub const fn from_bits(bits: u128) -> Ipv6Addr {
1479 Ipv6Addr { octets: bits.to_be_bytes() }
1480 }
1481
1482 #[doc(alias = "IN6ADDR_LOOPBACK_INIT")]
1496 #[doc(alias = "in6addr_loopback")]
1497 #[stable(feature = "ip_constructors", since = "1.30.0")]
1498 pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
1499
1500 #[doc(alias = "IN6ADDR_ANY_INIT")]
1513 #[doc(alias = "in6addr_any")]
1514 #[stable(feature = "ip_constructors", since = "1.30.0")]
1515 pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
1516
1517 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1528 #[stable(feature = "rust1", since = "1.0.0")]
1529 #[must_use]
1530 #[inline]
1531 pub const fn segments(&self) -> [u16; 8] {
1532 let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(self.octets) };
1535 [
1537 u16::from_be(a),
1538 u16::from_be(b),
1539 u16::from_be(c),
1540 u16::from_be(d),
1541 u16::from_be(e),
1542 u16::from_be(f),
1543 u16::from_be(g),
1544 u16::from_be(h),
1545 ]
1546 }
1547
1548 #[stable(feature = "ip_from", since = "1.91.0")]
1568 #[rustc_const_stable(feature = "ip_from", since = "1.91.0")]
1569 #[must_use]
1570 #[inline]
1571 pub const fn from_segments(segments: [u16; 8]) -> Ipv6Addr {
1572 let [a, b, c, d, e, f, g, h] = segments;
1573 Ipv6Addr::new(a, b, c, d, e, f, g, h)
1574 }
1575
1576 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1591 #[stable(since = "1.7.0", feature = "ip_17")]
1592 #[must_use]
1593 #[inline]
1594 pub const fn is_unspecified(&self) -> bool {
1595 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
1596 }
1597
1598 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1615 #[stable(since = "1.7.0", feature = "ip_17")]
1616 #[must_use]
1617 #[inline]
1618 pub const fn is_loopback(&self) -> bool {
1619 u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
1620 }
1621
1622 #[unstable(feature = "ip", issue = "27709")]
1688 #[must_use]
1689 #[inline]
1690 pub const fn is_global(&self) -> bool {
1691 !(self.is_unspecified()
1692 || self.is_loopback()
1693 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0, 0, 0, 0, 0, 0xffff, _, _] => true,
_ => false,
}matches!(self.segments(), [0, 0, 0, 0, 0, 0xffff, _, _])
1695 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x64, 0xff9b, 1, _, _, _, _, _] => true,
_ => false,
}matches!(self.segments(), [0x64, 0xff9b, 1, _, _, _, _, _])
1697 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x100, 0, 0, 0, _, _, _, _] => true,
_ => false,
}matches!(self.segments(), [0x100, 0, 0, 0, _, _, _, _])
1699 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x100, 0, 0, 1, _, _, _, _] => true,
_ => false,
}matches!(self.segments(), [0x100, 0, 0, 1, _, _, _, _])
1701 || (#[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x2001, b, _, _, _, _, _, _] if b < 0x200 => true,
_ => false,
}matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200)
1703 && !(
1704 u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001
1706 || u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002
1708 || u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0003
1710 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x2001, 3, _, _, _, _, _, _] => true,
_ => false,
}matches!(self.segments(), [0x2001, 3, _, _, _, _, _, _])
1712 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x2001, 4, 0x112, _, _, _, _, _] => true,
_ => false,
}matches!(self.segments(), [0x2001, 4, 0x112, _, _, _, _, _])
1714 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x3F => true,
_ => false,
}matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x3F)
1717 ))
1718 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x2002, _, _, _, _, _, _, _] => true,
_ => false,
}matches!(self.segments(), [0x2002, _, _, _, _, _, _, _])
1721 || self.is_documentation()
1722 || #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x5f00, ..] => true,
_ => false,
}matches!(self.segments(), [0x5f00, ..])
1724 || self.is_unique_local()
1725 || self.is_unicast_link_local())
1726 }
1727
1728 #[must_use]
1743 #[inline]
1744 #[stable(feature = "ipv6_is_unique_local", since = "1.84.0")]
1745 #[rustc_const_stable(feature = "ipv6_is_unique_local", since = "1.84.0")]
1746 pub const fn is_unique_local(&self) -> bool {
1747 (self.segments()[0] & 0xfe00) == 0xfc00
1748 }
1749
1750 #[unstable(feature = "ip", issue = "27709")]
1772 #[must_use]
1773 #[inline]
1774 pub const fn is_unicast(&self) -> bool {
1775 !self.is_multicast()
1776 }
1777
1778 #[must_use]
1821 #[inline]
1822 #[stable(feature = "ipv6_is_unique_local", since = "1.84.0")]
1823 #[rustc_const_stable(feature = "ipv6_is_unique_local", since = "1.84.0")]
1824 pub const fn is_unicast_link_local(&self) -> bool {
1825 (self.segments()[0] & 0xffc0) == 0xfe80
1826 }
1827
1828 #[unstable(feature = "ip", issue = "27709")]
1848 #[must_use]
1849 #[inline]
1850 pub const fn is_documentation(&self) -> bool {
1851 #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0x2001, 0xdb8, ..] | [0x3fff, 0..=0x0fff, ..] => true,
_ => false,
}matches!(self.segments(), [0x2001, 0xdb8, ..] | [0x3fff, 0..=0x0fff, ..])
1852 }
1853
1854 #[unstable(feature = "ip", issue = "27709")]
1871 #[must_use]
1872 #[inline]
1873 pub const fn is_benchmarking(&self) -> bool {
1874 (self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) && (self.segments()[2] == 0)
1875 }
1876
1877 #[unstable(feature = "ip", issue = "27709")]
1912 #[must_use]
1913 #[inline]
1914 pub const fn is_unicast_global(&self) -> bool {
1915 self.is_unicast()
1916 && !self.is_loopback()
1917 && !self.is_unicast_link_local()
1918 && !self.is_unique_local()
1919 && !self.is_unspecified()
1920 && !self.is_documentation()
1921 && !self.is_benchmarking()
1922 }
1923
1924 #[unstable(feature = "ip", issue = "27709")]
1940 #[must_use]
1941 #[inline]
1942 pub const fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
1943 if self.is_multicast() {
1944 match self.segments()[0] & 0x000f {
1945 0x0 => Some(Ipv6MulticastScope::Reserved0),
1946 0x1 => Some(Ipv6MulticastScope::InterfaceLocal),
1947 0x2 => Some(Ipv6MulticastScope::LinkLocal),
1948 0x3 => Some(Ipv6MulticastScope::RealmLocal),
1949 0x4 => Some(Ipv6MulticastScope::AdminLocal),
1950 0x5 => Some(Ipv6MulticastScope::SiteLocal),
1951 0x6 => Some(Ipv6MulticastScope::Unassigned6),
1952 0x7 => Some(Ipv6MulticastScope::Unassigned7),
1953 0x8 => Some(Ipv6MulticastScope::OrganizationLocal),
1954 0x9 => Some(Ipv6MulticastScope::Unassigned9),
1955 0xA => Some(Ipv6MulticastScope::UnassignedA),
1956 0xB => Some(Ipv6MulticastScope::UnassignedB),
1957 0xC => Some(Ipv6MulticastScope::UnassignedC),
1958 0xD => Some(Ipv6MulticastScope::UnassignedD),
1959 0xE => Some(Ipv6MulticastScope::Global),
1960 0xF => Some(Ipv6MulticastScope::ReservedF),
1961 _ => crate::panicking::unreachable_default_msg()unreachable!(),
1962 }
1963 } else {
1964 None
1965 }
1966 }
1967
1968 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
1983 #[stable(since = "1.7.0", feature = "ip_17")]
1984 #[must_use]
1985 #[inline]
1986 pub const fn is_multicast(&self) -> bool {
1987 (self.segments()[0] & 0xff00) == 0xff00
1988 }
1989
1990 #[unstable(feature = "ip", issue = "27709")]
2008 #[must_use]
2009 #[inline]
2010 pub const fn is_ipv4_mapped(&self) -> bool {
2011 #[allow(non_exhaustive_omitted_patterns)] match self.segments() {
[0, 0, 0, 0, 0, 0xffff, _, _] => true,
_ => false,
}matches!(self.segments(), [0, 0, 0, 0, 0, 0xffff, _, _])
2012 }
2013
2014 #[inline]
2035 #[must_use = "this returns the result of the operation, \
2036 without modifying the original"]
2037 #[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
2038 #[rustc_const_stable(feature = "const_ipv6_to_ipv4_mapped", since = "1.75.0")]
2039 pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
2040 match self.octets() {
2041 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
2042 Some(Ipv4Addr::new(a, b, c, d))
2043 }
2044 _ => None,
2045 }
2046 }
2047
2048 #[rustc_const_stable(feature = "const_ip_50", since = "1.50.0")]
2077 #[stable(feature = "rust1", since = "1.0.0")]
2078 #[must_use = "this returns the result of the operation, \
2079 without modifying the original"]
2080 #[inline]
2081 pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
2082 if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
2083 let [a, b] = ab.to_be_bytes();
2084 let [c, d] = cd.to_be_bytes();
2085 Some(Ipv4Addr::new(a, b, c, d))
2086 } else {
2087 None
2088 }
2089 }
2090
2091 #[inline]
2103 #[must_use = "this returns the result of the operation, \
2104 without modifying the original"]
2105 #[stable(feature = "ip_to_canonical", since = "1.75.0")]
2106 #[rustc_const_stable(feature = "ip_to_canonical", since = "1.75.0")]
2107 pub const fn to_canonical(&self) -> IpAddr {
2108 if let Some(mapped) = self.to_ipv4_mapped() {
2109 return IpAddr::V4(mapped);
2110 }
2111 IpAddr::V6(*self)
2112 }
2113
2114 #[rustc_const_stable(feature = "const_ip_32", since = "1.32.0")]
2123 #[stable(feature = "ipv6_to_octets", since = "1.12.0")]
2124 #[must_use]
2125 #[inline]
2126 pub const fn octets(&self) -> [u8; 16] {
2127 self.octets
2128 }
2129
2130 #[stable(feature = "ip_from", since = "1.91.0")]
2150 #[rustc_const_stable(feature = "ip_from", since = "1.91.0")]
2151 #[must_use]
2152 #[inline]
2153 pub const fn from_octets(octets: [u8; 16]) -> Ipv6Addr {
2154 Ipv6Addr { octets }
2155 }
2156
2157 #[unstable(feature = "ip_as_octets", issue = "137259")]
2171 #[must_use]
2172 #[inline]
2173 pub const fn as_octets(&self) -> &[u8; 16] {
2174 &self.octets
2175 }
2176}
2177
2178#[stable(feature = "rust1", since = "1.0.0")]
2182impl fmt::Display for Ipv6Addr {
2183 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2184 if f.precision().is_none() && f.width().is_none() {
2187 let segments = self.segments();
2188
2189 if let Some(ipv4) = self.to_ipv4_mapped() {
2190 f.write_fmt(format_args!("::ffff:{0}", ipv4))write!(f, "::ffff:{}", ipv4)
2191 } else {
2192 #[derive(#[automatically_derived]impl crate::marker::Copy for Span { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl crate::clone::TrivialClone
for Span {
}
#[automatically_derived]impl crate::clone::Clone for Span {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: crate::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]impl crate::default::Default for Span {
#[inline]
fn default() -> Self {
Span {
start: crate::default::Default::default(),
len: crate::default::Default::default(),
}
}
}Default)]
2193 struct Span {
2194 start: usize,
2195 len: usize,
2196 }
2197
2198 let zeroes = {
2200 let mut longest = Span::default();
2201 let mut current = Span::default();
2202
2203 for (i, &segment) in segments.iter().enumerate() {
2204 if segment == 0 {
2205 if current.len == 0 {
2206 current.start = i;
2207 }
2208
2209 current.len += 1;
2210
2211 if current.len > longest.len {
2212 longest = current;
2213 }
2214 } else {
2215 current = Span::default();
2216 }
2217 }
2218
2219 longest
2220 };
2221
2222 #[inline]
2224 fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result {
2225 if let Some((first, tail)) = chunk.split_first() {
2226 f.write_fmt(format_args!("{0:x}", first))write!(f, "{:x}", first)?;
2227 for segment in tail {
2228 f.write_char(':')?;
2229 f.write_fmt(format_args!("{0:x}", segment))write!(f, "{:x}", segment)?;
2230 }
2231 }
2232 Ok(())
2233 }
2234
2235 if zeroes.len > 1 {
2236 fmt_subslice(f, &segments[..zeroes.start])?;
2237 f.write_str("::")?;
2238 fmt_subslice(f, &segments[zeroes.start + zeroes.len..])
2239 } else {
2240 fmt_subslice(f, &segments)
2241 }
2242 }
2243 } else {
2244 const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";
2245
2246 let mut buf = DisplayBuffer::buffer::<{ LONGEST_IPV6_ADDR.len() }>();
2247 let mut buf = DisplayBuffer::new(&mut buf);
2248 buf.write_fmt(format_args!("{0}", self))write!(buf, "{}", self).unwrap();
2250
2251 f.pad(buf.as_str())
2252 }
2253 }
2254}
2255
2256#[stable(feature = "rust1", since = "1.0.0")]
2257impl fmt::Debug for Ipv6Addr {
2258 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2259 fmt::Display::fmt(self, fmt)
2260 }
2261}
2262
2263#[stable(feature = "ip_cmp", since = "1.16.0")]
2264impl PartialEq<IpAddr> for Ipv6Addr {
2265 #[inline]
2266 fn eq(&self, other: &IpAddr) -> bool {
2267 match other {
2268 IpAddr::V4(_) => false,
2269 IpAddr::V6(v6) => self == v6,
2270 }
2271 }
2272}
2273
2274#[stable(feature = "ip_cmp", since = "1.16.0")]
2275impl PartialEq<Ipv6Addr> for IpAddr {
2276 #[inline]
2277 fn eq(&self, other: &Ipv6Addr) -> bool {
2278 match self {
2279 IpAddr::V4(_) => false,
2280 IpAddr::V6(v6) => v6 == other,
2281 }
2282 }
2283}
2284
2285#[stable(feature = "rust1", since = "1.0.0")]
2286#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2287const impl PartialOrd for Ipv6Addr {
2288 #[inline]
2289 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
2290 Some(self.cmp(other))
2291 }
2292}
2293
2294#[stable(feature = "ip_cmp", since = "1.16.0")]
2295impl PartialOrd<Ipv6Addr> for IpAddr {
2296 #[inline]
2297 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
2298 match self {
2299 IpAddr::V4(_) => Some(Ordering::Less),
2300 IpAddr::V6(v6) => v6.partial_cmp(other),
2301 }
2302 }
2303}
2304
2305#[stable(feature = "ip_cmp", since = "1.16.0")]
2306impl PartialOrd<IpAddr> for Ipv6Addr {
2307 #[inline]
2308 fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
2309 match other {
2310 IpAddr::V4(_) => Some(Ordering::Greater),
2311 IpAddr::V6(v6) => self.partial_cmp(v6),
2312 }
2313 }
2314}
2315
2316#[stable(feature = "rust1", since = "1.0.0")]
2317#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2318const impl Ord for Ipv6Addr {
2319 #[inline]
2320 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
2321 self.segments().cmp(&other.segments())
2322 }
2323}
2324
2325#[stable(feature = "i128", since = "1.26.0")]
2326#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2327const impl From<Ipv6Addr> for u128 {
2328 #[inline]
2330 fn from(ip: Ipv6Addr) -> u128 {
2331 ip.to_bits()
2332 }
2333}
2334#[stable(feature = "i128", since = "1.26.0")]
2335#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2336const impl From<u128> for Ipv6Addr {
2337 #[inline]
2339 fn from(ip: u128) -> Ipv6Addr {
2340 Ipv6Addr::from_bits(ip)
2341 }
2342}
2343
2344#[stable(feature = "ipv6_from_octets", since = "1.9.0")]
2345#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2346const impl From<[u8; 16]> for Ipv6Addr {
2347 #[inline]
2367 fn from(octets: [u8; 16]) -> Ipv6Addr {
2368 Ipv6Addr { octets }
2369 }
2370}
2371
2372#[stable(feature = "ipv6_from_segments", since = "1.16.0")]
2373#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2374const impl From<[u16; 8]> for Ipv6Addr {
2375 #[inline]
2395 fn from(segments: [u16; 8]) -> Ipv6Addr {
2396 let [a, b, c, d, e, f, g, h] = segments;
2397 Ipv6Addr::new(a, b, c, d, e, f, g, h)
2398 }
2399}
2400
2401#[stable(feature = "ip_from_slice", since = "1.17.0")]
2402#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2403const impl From<[u8; 16]> for IpAddr {
2404 #[inline]
2424 fn from(octets: [u8; 16]) -> IpAddr {
2425 IpAddr::V6(Ipv6Addr::from(octets))
2426 }
2427}
2428
2429#[stable(feature = "ip_from_slice", since = "1.17.0")]
2430#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
2431const impl From<[u16; 8]> for IpAddr {
2432 #[inline]
2452 fn from(segments: [u16; 8]) -> IpAddr {
2453 IpAddr::V6(Ipv6Addr::from(segments))
2454 }
2455}
2456
2457#[stable(feature = "ip_bitops", since = "1.75.0")]
2458#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2459const impl Not for Ipv4Addr {
2460 type Output = Ipv4Addr;
2461
2462 #[inline]
2463 fn not(mut self) -> Ipv4Addr {
2464 let mut idx = 0;
2465 while idx < 4 {
2466 self.octets[idx] = !self.octets[idx];
2467 idx += 1;
2468 }
2469 self
2470 }
2471}
2472
2473#[stable(feature = "ip_bitops", since = "1.75.0")]
2474#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2475const impl Not for &'_ Ipv4Addr {
2476 type Output = Ipv4Addr;
2477
2478 #[inline]
2479 fn not(self) -> Ipv4Addr {
2480 !*self
2481 }
2482}
2483
2484#[stable(feature = "ip_bitops", since = "1.75.0")]
2485#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2486const impl Not for Ipv6Addr {
2487 type Output = Ipv6Addr;
2488
2489 #[inline]
2490 fn not(mut self) -> Ipv6Addr {
2491 let mut idx = 0;
2492 while idx < 16 {
2493 self.octets[idx] = !self.octets[idx];
2494 idx += 1;
2495 }
2496 self
2497 }
2498}
2499
2500#[stable(feature = "ip_bitops", since = "1.75.0")]
2501#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2502const impl Not for &'_ Ipv6Addr {
2503 type Output = Ipv6Addr;
2504
2505 #[inline]
2506 fn not(self) -> Ipv6Addr {
2507 !*self
2508 }
2509}
2510
2511macro_rules! bitop_impls {
2512 ($(
2513 $(#[$attr:meta])*
2514 impl ($BitOp:ident, $BitOpAssign:ident) for $ty:ty = ($bitop:ident, $bitop_assign:ident);
2515 )*) => {
2516 $(
2517 $(#[$attr])*
2518 const impl $BitOpAssign for $ty {
2519 fn $bitop_assign(&mut self, rhs: $ty) {
2520 let mut idx = 0;
2521 while idx < self.octets.len() {
2522 self.octets[idx].$bitop_assign(rhs.octets[idx]);
2523 idx += 1;
2524 }
2525 }
2526 }
2527
2528 $(#[$attr])*
2529 const impl $BitOpAssign<&'_ $ty> for $ty {
2530 fn $bitop_assign(&mut self, rhs: &'_ $ty) {
2531 self.$bitop_assign(*rhs);
2532 }
2533 }
2534
2535 $(#[$attr])*
2536 const impl $BitOp for $ty {
2537 type Output = $ty;
2538
2539 #[inline]
2540 fn $bitop(mut self, rhs: $ty) -> $ty {
2541 self.$bitop_assign(rhs);
2542 self
2543 }
2544 }
2545
2546 $(#[$attr])*
2547 const impl $BitOp<&'_ $ty> for $ty {
2548 type Output = $ty;
2549
2550 #[inline]
2551 fn $bitop(mut self, rhs: &'_ $ty) -> $ty {
2552 self.$bitop_assign(*rhs);
2553 self
2554 }
2555 }
2556
2557 $(#[$attr])*
2558 const impl $BitOp<$ty> for &'_ $ty {
2559 type Output = $ty;
2560
2561 #[inline]
2562 fn $bitop(self, rhs: $ty) -> $ty {
2563 let mut lhs = *self;
2564 lhs.$bitop_assign(rhs);
2565 lhs
2566 }
2567 }
2568
2569 $(#[$attr])*
2570 const impl $BitOp<&'_ $ty> for &'_ $ty {
2571 type Output = $ty;
2572
2573 #[inline]
2574 fn $bitop(self, rhs: &'_ $ty) -> $ty {
2575 let mut lhs = *self;
2576 lhs.$bitop_assign(*rhs);
2577 lhs
2578 }
2579 }
2580 )*
2581 };
2582}
2583
2584#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAndAssign for Ipv4Addr {
fn bitand_assign(&mut self, rhs: Ipv4Addr) {
let mut idx = 0;
while idx < self.octets.len() {
self.octets[idx].bitand_assign(rhs.octets[idx]);
idx += 1;
}
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAndAssign<&'_ Ipv4Addr> for Ipv4Addr {
fn bitand_assign(&mut self, rhs: &'_ Ipv4Addr) {
self.bitand_assign(*rhs);
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd for Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitand(mut self, rhs: Ipv4Addr) -> Ipv4Addr {
self.bitand_assign(rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd<&'_ Ipv4Addr> for Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitand(mut self, rhs: &'_ Ipv4Addr) -> Ipv4Addr {
self.bitand_assign(*rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd<Ipv4Addr> for &'_ Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitand(self, rhs: Ipv4Addr) -> Ipv4Addr {
let mut lhs = *self;
lhs.bitand_assign(rhs);
lhs
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd<&'_ Ipv4Addr> for &'_ Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitand(self, rhs: &'_ Ipv4Addr) -> Ipv4Addr {
let mut lhs = *self;
lhs.bitand_assign(*rhs);
lhs
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOrAssign for Ipv4Addr {
fn bitor_assign(&mut self, rhs: Ipv4Addr) {
let mut idx = 0;
while idx < self.octets.len() {
self.octets[idx].bitor_assign(rhs.octets[idx]);
idx += 1;
}
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOrAssign<&'_ Ipv4Addr> for Ipv4Addr {
fn bitor_assign(&mut self, rhs: &'_ Ipv4Addr) { self.bitor_assign(*rhs); }
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr for Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitor(mut self, rhs: Ipv4Addr) -> Ipv4Addr {
self.bitor_assign(rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr<&'_ Ipv4Addr> for Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitor(mut self, rhs: &'_ Ipv4Addr) -> Ipv4Addr {
self.bitor_assign(*rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr<Ipv4Addr> for &'_ Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitor(self, rhs: Ipv4Addr) -> Ipv4Addr {
let mut lhs = *self;
lhs.bitor_assign(rhs);
lhs
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr<&'_ Ipv4Addr> for &'_ Ipv4Addr {
type Output = Ipv4Addr;
#[inline]
fn bitor(self, rhs: &'_ Ipv4Addr) -> Ipv4Addr {
let mut lhs = *self;
lhs.bitor_assign(*rhs);
lhs
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAndAssign for Ipv6Addr {
fn bitand_assign(&mut self, rhs: Ipv6Addr) {
let mut idx = 0;
while idx < self.octets.len() {
self.octets[idx].bitand_assign(rhs.octets[idx]);
idx += 1;
}
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAndAssign<&'_ Ipv6Addr> for Ipv6Addr {
fn bitand_assign(&mut self, rhs: &'_ Ipv6Addr) {
self.bitand_assign(*rhs);
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd for Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitand(mut self, rhs: Ipv6Addr) -> Ipv6Addr {
self.bitand_assign(rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd<&'_ Ipv6Addr> for Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitand(mut self, rhs: &'_ Ipv6Addr) -> Ipv6Addr {
self.bitand_assign(*rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd<Ipv6Addr> for &'_ Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitand(self, rhs: Ipv6Addr) -> Ipv6Addr {
let mut lhs = *self;
lhs.bitand_assign(rhs);
lhs
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitAnd<&'_ Ipv6Addr> for &'_ Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitand(self, rhs: &'_ Ipv6Addr) -> Ipv6Addr {
let mut lhs = *self;
lhs.bitand_assign(*rhs);
lhs
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOrAssign for Ipv6Addr {
fn bitor_assign(&mut self, rhs: Ipv6Addr) {
let mut idx = 0;
while idx < self.octets.len() {
self.octets[idx].bitor_assign(rhs.octets[idx]);
idx += 1;
}
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOrAssign<&'_ Ipv6Addr> for Ipv6Addr {
fn bitor_assign(&mut self, rhs: &'_ Ipv6Addr) { self.bitor_assign(*rhs); }
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr for Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitor(mut self, rhs: Ipv6Addr) -> Ipv6Addr {
self.bitor_assign(rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr<&'_ Ipv6Addr> for Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitor(mut self, rhs: &'_ Ipv6Addr) -> Ipv6Addr {
self.bitor_assign(*rhs);
self
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr<Ipv6Addr> for &'_ Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitor(self, rhs: Ipv6Addr) -> Ipv6Addr {
let mut lhs = *self;
lhs.bitor_assign(rhs);
lhs
}
}
#[stable(feature = "ip_bitops", since = "1.75.0")]
#[rustc_const_unstable(feature = "const_ops", issue = "143802")]
const impl BitOr<&'_ Ipv6Addr> for &'_ Ipv6Addr {
type Output = Ipv6Addr;
#[inline]
fn bitor(self, rhs: &'_ Ipv6Addr) -> Ipv6Addr {
let mut lhs = *self;
lhs.bitor_assign(*rhs);
lhs
}
}bitop_impls! {
2585 #[stable(feature = "ip_bitops", since = "1.75.0")]
2586 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2587 impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
2588 #[stable(feature = "ip_bitops", since = "1.75.0")]
2589 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2590 impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);
2591
2592 #[stable(feature = "ip_bitops", since = "1.75.0")]
2593 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2594 impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
2595 #[stable(feature = "ip_bitops", since = "1.75.0")]
2596 #[rustc_const_unstable(feature = "const_ops", issue = "143802")]
2597 impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
2598}