Skip to content

Commit

Permalink
select/fdset: use BorrowedFd as argument for FromIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJonny committed Apr 23, 2024
1 parent bf2f844 commit 7def327
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/sys/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::convert::TryFrom;
use std::iter::FusedIterator;
use std::mem;
use std::ops::Range;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd};
use std::ptr::{null, null_mut};

pub use libc::FD_SETSIZE;
Expand Down Expand Up @@ -163,19 +163,17 @@ impl<'a, 'fd> DoubleEndedIterator for Fds<'a, 'fd> {

impl<'a, 'fd> FusedIterator for Fds<'a, 'fd> {}

impl<'fd, F> FromIterator<F> for FdSet<'fd> where F: 'fd + AsFd {
fn from_iter<T>(iter: T) -> Self where T: IntoIterator<Item = F> {
impl<'fd> FromIterator<BorrowedFd<'fd>> for FdSet<'fd> {
fn from_iter<T>(iter: T) -> Self where T: IntoIterator<Item=BorrowedFd<'fd>> {
let mut result = FdSet::new();
for fd in iter.into_iter() {
let fd = fd.as_fd();
assert_fd_valid(fd.as_raw_fd());
unsafe { libc::FD_SET(fd.as_raw_fd(), &mut result.set) };
result.insert(fd)
}
result
}
}

impl<'fd, T, F> From<T> for FdSet<'fd> where T: IntoIterator<Item = F>, F: 'fd + AsFd {
impl<'fd, T> From<T> for FdSet<'fd> where T: IntoIterator<Item=BorrowedFd<'fd>> {
fn from(value: T) -> Self {
value.into_iter().collect()
}
Expand Down
4 changes: 2 additions & 2 deletions test/sys/test_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ fn test_fdset_from_iterable() {
.map(|fd| (fd.as_raw_fd(), fd))
.collect::<std::collections::HashMap<_, _>>();
let writes = [w1, w2];
let reads_fdset: FdSet = reads.values().into();
let writes_fdset: FdSet = writes.iter().into();
let reads_fdset: FdSet = reads.values().map(|x| x.as_fd()).into();
let writes_fdset: FdSet = writes.iter().map(|x| x.as_fd()).into();
assert_eq!(
reads_fdset
.fds(None)
Expand Down

0 comments on commit 7def327

Please sign in to comment.