Skip to content

Commit 33c7dc1

Browse files
committed
Use itertools to reduce manual iterator parsing
Signed-off-by: Colin Walters <[email protected]>
1 parent dac7bcb commit 33c7dc1

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tokio = { features = ["fs", "io-util", "macros", "process", "rt", "sync"], versi
2323
tracing = "0.1"
2424
# We support versions 2, 3 and 4
2525
cap-std-ext = ">= 2.0, <= 4.0"
26+
itertools = "0.14.0"
2627

2728
[dev-dependencies]
2829
anyhow = "1.0"

src/imageproxy.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use cap_std_ext::prelude::CapStdExtCommandExt;
88
use cap_std_ext::{cap_std, cap_tempfile};
99
use futures_util::{Future, FutureExt};
10+
use itertools::Itertools;
1011
use oci_spec::image::{Descriptor, Digest};
1112
use serde::{Deserialize, Serialize};
1213
use std::fs::File;
@@ -382,39 +383,27 @@ impl FromReplyFds for () {
382383
/// A FinishPipe instance
383384
impl FromReplyFds for FinishPipe {
384385
fn from_reply(fds: impl IntoIterator<Item = OwnedFd>, pipeid: u32) -> Result<Self> {
385-
let mut fds = fds.into_iter();
386-
let Some(first_fd) = fds.next() else {
387-
return Err(Error::Other("Expected fd for FinishPipe".into()));
388-
};
389-
if fds.next().is_some() {
390-
return Err(Error::Other("More than one fd for FinishPipe".into()));
391-
}
392386
let Some(pipeid) = PipeId::try_new(pipeid) else {
393387
return Err(Error::Other("Expected pipeid for FinishPipe".into()));
394388
};
395-
Ok(Self {
396-
pipeid,
397-
datafd: first_fd,
398-
})
389+
let datafd = fds
390+
.into_iter()
391+
.exactly_one()
392+
.map_err(|_| Error::Other("Expected exactly one fd for FinishPipe".into()))?;
393+
Ok(Self { pipeid, datafd })
399394
}
400395
}
401396

402397
/// A DualFds instance
403398
impl FromReplyFds for DualFds {
404399
fn from_reply(fds: impl IntoIterator<Item = OwnedFd>, pipeid: u32) -> Result<Self> {
405-
let mut fds = fds.into_iter();
406-
let Some(datafd) = fds.next() else {
407-
return Err(Error::Other("Expected data fd for DualFds".into()));
408-
};
409-
let Some(errfd) = fds.next() else {
410-
return Err(Error::Other("Expected err fd for DualFds".into()));
411-
};
412-
if fds.next().is_some() {
413-
return Err(Error::Other("More than two fds for DualFds".into()));
414-
}
415400
if pipeid != 0 {
416401
return Err(Error::Other("Unexpected pipeid with DualFds".into()));
417402
}
403+
let [datafd, errfd] = fds
404+
.into_iter()
405+
.collect_array()
406+
.ok_or_else(|| Error::Other("Expected two fds for DualFds".into()))?;
418407
Ok(Self { datafd, errfd })
419408
}
420409
}
@@ -961,7 +950,7 @@ mod tests {
961950
match DualFds::from_reply(fds, 0) {
962951
Ok(v) => unreachable!("{v:?}"),
963952
Err(Error::Other(msg)) => {
964-
assert_eq!(msg.as_ref(), "More than two fds for DualFds")
953+
assert_eq!(msg.as_ref(), "Expected two fds for DualFds")
965954
}
966955
Err(other) => unreachable!("{other}"),
967956
}
@@ -997,7 +986,7 @@ mod tests {
997986
match FinishPipe::from_reply(fds, 1) {
998987
Ok(v) => unreachable!("{v:?}"),
999988
Err(Error::Other(msg)) => {
1000-
assert_eq!(msg.as_ref(), "Expected fd for FinishPipe")
989+
assert_eq!(msg.as_ref(), "Expected exactly one fd for FinishPipe")
1001990
}
1002991
Err(other) => unreachable!("{other}"),
1003992
}

0 commit comments

Comments
 (0)