diff options
author | Ross Light <ross@zombiezen.com> | 2020-04-13 09:26:22 -0700 |
---|---|---|
committer | Ross Light <ross@zombiezen.com> | 2020-04-13 09:26:22 -0700 |
commit | 8b4a43386f8d0c6f68c21f113cb7b52909457f04 (patch) | |
tree | 659c4f47c99cde1a7af8c498e492e32f8c26f527 /src/sys/select.rs | |
parent | 41db1bf213cf5a15423380d031ad6009004443ba (diff) | |
download | nix-8b4a43386f8d0c6f68c21f113cb7b52909457f04.zip |
Operate on &mut
Diffstat (limited to 'src/sys/select.rs')
-rw-r--r-- | src/sys/select.rs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/sys/select.rs b/src/sys/select.rs index b914ffae..8591dd85 100644 --- a/src/sys/select.rs +++ b/src/sys/select.rs @@ -32,9 +32,8 @@ impl FdSet { unsafe { libc::FD_CLR(fd, &mut self.0) }; } - pub fn contains(&self, fd: RawFd) -> bool { - let mut copy = self.0; - unsafe { libc::FD_ISSET(fd, &mut copy) } + pub fn contains(&mut self, fd: RawFd) -> bool { + unsafe { libc::FD_ISSET(fd, &mut self.0) } } pub fn clear(&mut self) { @@ -61,11 +60,13 @@ impl FdSet { /// ``` /// /// [`select`]: fn.select.html - pub fn highest(&self) -> Option<RawFd> { - self.fds().next_back() + pub fn highest(&mut self) -> Option<RawFd> { + self.fds(None).next_back() } - /// Returns an iterator over the file descriptors in the set. + /// Returns an iterator over the file descriptors in the set. For + /// performance, it takes an optional higher bound: the iterator will not + /// return any elements of the set greater than the given file descriptor. /// /// # Examples /// @@ -76,14 +77,14 @@ impl FdSet { /// let mut set = FdSet::new(); /// set.insert(4); /// set.insert(9); - /// let fds: Vec<RawFd> = set.fds().collect(); + /// let fds: Vec<RawFd> = set.fds(None).collect(); /// assert_eq!(fds, vec![4, 9]); /// ``` #[inline] - pub fn fds(&self) -> Fds { + pub fn fds(&mut self, highest: Option<RawFd>) -> Fds { Fds { set: self, - range: 0..FD_SETSIZE, + range: 0..highest.map(|h| h as usize + 1).unwrap_or(FD_SETSIZE), } } } @@ -95,9 +96,9 @@ impl Default for FdSet { } /// Iterator over `FdSet`. -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct Fds<'a> { - set: &'a FdSet, + set: &'a mut FdSet, range: Range<usize>, } |