use crate::io::{Fd, RawFd};
use crate::system::FdSet as FdSetTrait;
use std::collections::BTreeSet;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct FdSet(BTreeSet<Fd>);
impl FdSetTrait for FdSet {
const MAX_FD: Fd = Fd(RawFd::MAX);
#[inline(always)]
fn insert(&mut self, fd: Fd) {
if fd.0 >= 0 {
self.0.insert(fd);
}
}
#[inline(always)]
fn remove(&mut self, fd: Fd) {
self.0.remove(&fd);
}
#[inline(always)]
fn contains(&self, fd: Fd) -> bool {
self.0.contains(&fd)
}
}
impl From<Fd> for FdSet {
#[inline(always)]
fn from(fd: Fd) -> Self {
let mut set = Self::new();
set.insert(fd);
set
}
}
impl FromIterator<Fd> for FdSet {
#[inline(always)]
fn from_iter<I: IntoIterator<Item = Fd>>(iter: I) -> Self {
Self(FromIterator::from_iter(iter))
}
}
#[derive(Debug)]
pub struct IntoIter(std::collections::btree_set::IntoIter<Fd>);
impl Iterator for IntoIter {
type Item = Fd;
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl IntoIterator for FdSet {
type Item = Fd;
type IntoIter = IntoIter;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.0.into_iter())
}
}
#[derive(Debug)]
pub struct Iter<'a>(std::collections::btree_set::Iter<'a, Fd>);
impl<'a> Iterator for Iter<'a> {
type Item = &'a Fd;
#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl<'a> IntoIterator for &'a FdSet {
type Item = &'a Fd;
type IntoIter = Iter<'a>;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
Iter(self.0.iter())
}
}
impl Extend<Fd> for FdSet {
fn extend<I: IntoIterator<Item = Fd>>(&mut self, iter: I) {
self.0.extend(iter);
}
}
impl<'a> Extend<&'a Fd> for FdSet {
fn extend<I: IntoIterator<Item = &'a Fd>>(&mut self, iter: I) {
self.0.extend(iter);
}
}
impl FdSet {
#[inline(always)]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline(always)]
pub fn iter(&self) -> Iter<'_> {
self.into_iter()
}
}