Auto merge of #113748 - clarfonthey:ip-step, r=dtolnay · rust-lang/rust@c8b5754 (original) (raw)
1
1
`use crate::ascii::Char as AsciiChar;
`
2
2
`use crate::convert::TryFrom;
`
3
3
`use crate::mem;
`
``
4
`+
use crate:🥅:{Ipv4Addr, Ipv6Addr};
`
4
5
`use crate::num::NonZeroUsize;
`
5
6
`use crate::ops::{self, Try};
`
6
7
``
`@@ -15,7 +16,7 @@ macro_rules! unsafe_impl_trusted_step {
`
15
16
`unsafe impl TrustedStep for $type {}
`
16
17
`)*};
`
17
18
`}
`
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];
`
19
20
``
20
21
`/// Objects that have a notion of successor and predecessor operations.
`
21
22
`///
`
`@@ -527,6 +528,70 @@ impl Step for AsciiChar {
`
527
528
`}
`
528
529
`}
`
529
530
``
``
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 {
`
``
535
`+
u32::steps_between(&start.to_bits(), &end.to_bits())
`
``
536
`+
}
`
``
537
+
``
538
`+
#[inline]
`
``
539
`+
fn forward_checked(start: Ipv4Addr, count: usize) -> Option {
`
``
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 {
`
``
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 {
`
``
567
`+
u128::steps_between(&start.to_bits(), &end.to_bits())
`
``
568
`+
}
`
``
569
+
``
570
`+
#[inline]
`
``
571
`+
fn forward_checked(start: Ipv6Addr, count: usize) -> Option {
`
``
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 {
`
``
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
+
530
595
`macro_rules! range_exact_iter_impl {
`
531
596
`($($t:ty)*) => ($(
`
532
597
` #[stable(feature = "rust1", since = "1.0.0")]
`