11use crate::ascii::Char as AsciiChar;
22use crate::convert::TryFrom;
33use crate::mem;
4+ use crate::net::{Ipv4Addr, Ipv6Addr};
45use crate::num::NonZeroUsize;
56use crate::ops::{self, Try};
67
@@ -15,7 +16,7 @@ macro_rules! unsafe_impl_trusted_step {
1516 unsafe impl TrustedStep for $type {}
1617 )*};
1718}
18- unsafe_impl_trusted_step![AsciiChar char i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize];
19+ unsafe_impl_trusted_step![AsciiChar char i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize Ipv4Addr Ipv6Addr ];
1920
2021/// Objects that have a notion of *successor* and *predecessor* operations.
2122///
@@ -527,6 +528,70 @@ impl Step for AsciiChar {
527528 }
528529}
529530
531+ #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
532+ impl Step for Ipv4Addr {
533+ #[inline]
534+ fn steps_between(&start: &Ipv4Addr, &end: &Ipv4Addr) -> Option<usize> {
535+ u32::steps_between(&start.to_bits(), &end.to_bits())
536+ }
537+
538+ #[inline]
539+ fn forward_checked(start: Ipv4Addr, count: usize) -> Option<Ipv4Addr> {
540+ u32::forward_checked(start.to_bits(), count).map(Ipv4Addr::from_bits)
541+ }
542+
543+ #[inline]
544+ fn backward_checked(start: Ipv4Addr, count: usize) -> Option<Ipv4Addr> {
545+ u32::backward_checked(start.to_bits(), count).map(Ipv4Addr::from_bits)
546+ }
547+
548+ #[inline]
549+ unsafe fn forward_unchecked(start: Ipv4Addr, count: usize) -> Ipv4Addr {
550+ // SAFETY: Since u32 and Ipv4Addr are losslessly convertible,
551+ // this is as safe as the u32 version.
552+ Ipv4Addr::from_bits(unsafe { u32::forward_unchecked(start.to_bits(), count) })
553+ }
554+
555+ #[inline]
556+ unsafe fn backward_unchecked(start: Ipv4Addr, count: usize) -> Ipv4Addr {
557+ // SAFETY: Since u32 and Ipv4Addr are losslessly convertible,
558+ // this is as safe as the u32 version.
559+ Ipv4Addr::from_bits(unsafe { u32::backward_unchecked(start.to_bits(), count) })
560+ }
561+ }
562+
563+ #[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
564+ impl Step for Ipv6Addr {
565+ #[inline]
566+ fn steps_between(&start: &Ipv6Addr, &end: &Ipv6Addr) -> Option<usize> {
567+ u128::steps_between(&start.to_bits(), &end.to_bits())
568+ }
569+
570+ #[inline]
571+ fn forward_checked(start: Ipv6Addr, count: usize) -> Option<Ipv6Addr> {
572+ u128::forward_checked(start.to_bits(), count).map(Ipv6Addr::from_bits)
573+ }
574+
575+ #[inline]
576+ fn backward_checked(start: Ipv6Addr, count: usize) -> Option<Ipv6Addr> {
577+ u128::backward_checked(start.to_bits(), count).map(Ipv6Addr::from_bits)
578+ }
579+
580+ #[inline]
581+ unsafe fn forward_unchecked(start: Ipv6Addr, count: usize) -> Ipv6Addr {
582+ // SAFETY: Since u128 and Ipv6Addr are losslessly convertible,
583+ // this is as safe as the u128 version.
584+ Ipv6Addr::from_bits(unsafe { u128::forward_unchecked(start.to_bits(), count) })
585+ }
586+
587+ #[inline]
588+ unsafe fn backward_unchecked(start: Ipv6Addr, count: usize) -> Ipv6Addr {
589+ // SAFETY: Since u128 and Ipv6Addr are losslessly convertible,
590+ // this is as safe as the u128 version.
591+ Ipv6Addr::from_bits(unsafe { u128::backward_unchecked(start.to_bits(), count) })
592+ }
593+ }
594+
530595macro_rules! range_exact_iter_impl {
531596 ($($t:ty)*) => ($(
532597 #[stable(feature = "rust1", since = "1.0.0")]
0 commit comments