use std::collections::HashMap;
use std::rc::Rc;
use crate::fs::File;
pub enum FdOwnership {
TableOwned(Rc<dyn File>),
UvOwned,
}
pub struct FdTable {
entries: HashMap<i32, FdOwnership>,
}
impl FdTable {
pub fn new() -> Self {
Self {
entries: HashMap::new(),
}
}
pub fn register(&mut self, fd: i32, file: Rc<dyn File>) -> bool {
if self.entries.contains_key(&fd) {
return false;
}
self.entries.insert(fd, FdOwnership::TableOwned(file));
true
}
pub fn register_uv_owned(&mut self, fd: i32) -> bool {
if self.entries.contains_key(&fd) {
return false;
}
self.entries.insert(fd, FdOwnership::UvOwned);
true
}
pub fn get(&self, fd: i32) -> Option<&Rc<dyn File>> {
match self.entries.get(&fd) {
Some(FdOwnership::TableOwned(file)) => Some(file),
_ => None,
}
}
pub fn remove(&mut self, fd: i32) -> Option<Rc<dyn File>> {
match self.entries.remove(&fd) {
Some(FdOwnership::TableOwned(file)) => Some(file),
Some(FdOwnership::UvOwned) => None,
None => None,
}
}
pub fn contains(&self, fd: i32) -> bool {
self.entries.contains_key(&fd)
}
}
impl Default for FdTable {
fn default() -> Self {
Self::new()
}
}