Skip to content

Commit 111b9e5

Browse files
committed
iter: Replace some map(..).flatten() with and_then(..)
Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
1 parent dfd3999 commit 111b9e5

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

src/iter/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where
3434
/// may produce `None`.
3535
fn next(&mut self) -> Option<Self::Item> {
3636
let cur: Option<Self::Item> = self.cur;
37-
let next: Option<Self::Item> = cur.map(|cur| cur.offset(1_u32)).flatten();
37+
let next: Option<Self::Item> = cur.and_then(|cur| cur.offset(1_u32));
3838

3939
match (cur, next) {
4040
(Some(cur), Some(next)) => {

src/iter/offset.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ impl Offset<i32> for Ipv6Addr {
2424

2525
// This is a simple "if positive, use checked_add, if negative, use checked_sub" conditional
2626
offset_abs
27-
.map(|abs: u128| match offset {
27+
.and_then(|abs: u128| match offset {
2828
0..=MAX => u128::from(*self).checked_add(abs),
2929
MIN..=-1 => u128::from(*self).checked_sub(abs),
3030
})
31-
.flatten()
3231
.map(Ipv6Addr::from)
3332
}
3433
}
@@ -53,8 +52,7 @@ impl Offset<i32> for Ipv4Addr {
5352
let offset: Option<u32> = addr
5453
.checked_add(i64::from(offset))
5554
.map(TryInto::try_into)
56-
.map(Result::ok)
57-
.flatten();
55+
.and_then(Result::ok);
5856
offset.map(Ipv4Addr::from)
5957
}
6058
}
@@ -63,8 +61,7 @@ impl Offset<u128> for Ipv4Addr {
6361
fn offset(&self, offset: u128) -> Option<Self> {
6462
let offset: Option<u32> = offset.try_into().ok();
6563
offset
66-
.map(|offset: u32| u32::from(*self).checked_add(offset))
67-
.flatten()
64+
.and_then(|offset: u32| u32::from(*self).checked_add(offset))
6865
.map(Ipv4Addr::from)
6966
}
7067
}

src/iter/subnet.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ impl Iterator for SubnetIterator<Netv4Addr, Netv4Addr> {
1919
Some(cur) if self.net.contains(&cur) => {
2020
let device_count: Option<u32> = cur.len();
2121

22-
let next: Option<Netv4Addr> = device_count
23-
.map(|device_count: u32| cur.offset(device_count))
24-
.flatten();
22+
let next: Option<Netv4Addr> =
23+
device_count.and_then(|device_count: u32| cur.offset(device_count));
2524

2625
match next {
2726
Some(next) => {
@@ -56,9 +55,8 @@ impl Iterator for SubnetIterator<Netv6Addr, Netv6Addr> {
5655
Some(cur) if self.net.contains(&cur) => {
5756
let device_count: Option<u128> = cur.len();
5857

59-
let next: Option<Netv6Addr> = device_count
60-
.map(|device_count: u128| cur.offset(device_count))
61-
.flatten();
58+
let next: Option<Netv6Addr> =
59+
device_count.and_then(|device_count: u128| cur.offset(device_count));
6260

6361
match next {
6462
Some(next) => {

0 commit comments

Comments
 (0)