Skip to content

Commit

Permalink
make ResourceId newtype instead of typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Dec 20, 2023
1 parent dda46f2 commit 8244a41
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/poller/popol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Poller {
Self {
poll: popol::Sources::new(),
events: empty!(),
id_top: 0,
id_top: ResourceId::ZERO,
}
}

Expand All @@ -60,7 +60,7 @@ impl Poller {
Self {
poll: popol::Sources::with_capacity(capacity),
events: VecDeque::with_capacity(capacity),
id_top: 0,
id_top: ResourceId::ZERO,
}
}
}
Expand All @@ -70,7 +70,7 @@ impl Poll for Poller {

fn register(&mut self, fd: &impl AsRawFd, interest: IoType) -> ResourceId {
let id = self.id_top;
self.id_top += 1;
self.id_top.inc();

#[cfg(feature = "log")]
log::trace!(target: "popol", "Registering file descriptor {} as resource with id {}", fd.as_raw_fd(), id);
Expand Down
10 changes: 9 additions & 1 deletion src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ pub enum Io {

/// The resource identifier must be globally unique and non-reusable object. Because of this,
/// things like [`RawFd`] and socket addresses can't operate like resource identifiers.
pub type ResourceId = u64;
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[display(inner)]
pub struct ResourceId(u64);

impl ResourceId {
pub(crate) const ZERO: ResourceId = ResourceId(0);

pub(crate) fn inc(&mut self) { self.0 += 1 }
}

/// A resource which can be managed by the reactor.
pub trait Resource: AsRawFd + WriteAtomic + Send {
Expand Down

0 comments on commit 8244a41

Please sign in to comment.