1// SPDX-License-Identifier: MIT OR Apache-2.0
23use polling::{Event, Poller};
45use std::fmt;
6use std::io::Result;
7use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd};
89/// The raw registration into the reactor.
10#[doc(hidden)]
11pub struct Registration {
12/// Raw file descriptor on Unix.
13 ///
14 /// # Invariant
15 ///
16 /// This describes a valid file descriptor that has not been `close`d. It will not be
17 /// closed while this object is alive.
18raw: RawFd,
19}
2021impl fmt::Debug for Registration {
22fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 fmt::Debug::fmt(&self.raw, f)
24 }
25}
2627impl Registration {
28/// Add this file descriptor into the reactor.
29 ///
30 /// # Safety
31 ///
32 /// The provided file descriptor must be valid and not be closed while this object is alive.
33pub(crate) unsafe fn new(f: BorrowedFd<'_>) -> Self {
34Self { raw: f.as_raw_fd() }
35 }
3637/// Registers the object into the reactor.
38#[inline]
39pub(crate) fn add(&self, poller: &Poller, token: usize) -> Result<()> {
40// SAFETY: This object's existence validates the invariants of Poller::add
41unsafe { poller.add(self.raw, Event::none(token)) }
42 }
4344/// Re-registers the object into the reactor.
45#[inline]
46pub(crate) fn modify(&self, poller: &Poller, interest: Event) -> Result<()> {
47// SAFETY: self.raw is a valid file descriptor
48let fd = unsafe { BorrowedFd::borrow_raw(self.raw) };
49 poller.modify(fd, interest)
50 }
5152/// Deregisters the object from the reactor.
53#[inline]
54pub(crate) fn delete(&self, poller: &Poller) -> Result<()> {
55// SAFETY: self.raw is a valid file descriptor
56let fd = unsafe { BorrowedFd::borrow_raw(self.raw) };
57 poller.delete(fd)
58 }
59}