Debian Codegraph @

src/reactor/windows.rs

raw symbols SCIP
1 // SPDX-License-Identifier: MIT OR Apache-2.0
2
3 use polling::os::iocp::PollerIocpExt;
4 use polling::{Event, PollMode, Poller};
5 use std::fmt;
6 use std::io::Result;
7 use std::os::windows::io::{
8 AsRawHandle, AsRawSocket, BorrowedHandle, BorrowedSocket, RawHandle, RawSocket,
9 };
10
11 /// The raw registration into the reactor.
12 #[doc(hidden)]
13 pub enum Registration {
14 /// Raw socket handle on Windows.
15 ///
16 /// # Invariant
17 ///
18 /// This describes a valid socket that has not been `close`d. It will not be
19 /// closed while this object is alive.
20 Socket(RawSocket),
21
22 /// Waitable handle for Windows.
23 ///
24 /// # Invariant
25 ///
26 /// This describes a valid waitable handle that has not been `close`d. It will not be
27 /// closed while this object is alive.
28 Handle(RawHandle),
29 }
30
31 unsafe impl Send for Registration {}
32 unsafe impl Sync for Registration {}
33
34 impl fmt::Debug for Registration {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match self {
37 Self::Socket(raw) => fmt::Debug::fmt(raw, f),
38 Self::Handle(handle) => fmt::Debug::fmt(handle, f),
39 }
40 }
41 }
42
43 impl Registration {
44 /// Add this file descriptor into the reactor.
45 ///
46 /// # Safety
47 ///
48 /// The provided file descriptor must be valid and not be closed while this object is alive.
49 pub(crate) unsafe fn new(f: BorrowedSocket<'_>) -> Self {
50 Self::Socket(f.as_raw_socket())
51 }
52
53 /// Create a new [`Registration`] around a waitable handle.
54 ///
55 /// # Safety
56 ///
57 /// The provided handle must be valid and not be closed while this object is alive.
58 pub(crate) unsafe fn new_waitable(f: BorrowedHandle<'_>) -> Self {
59 Self::Handle(f.as_raw_handle())
60 }
61
62 /// Registers the object into the reactor.
63 #[inline]
64 pub(crate) fn add(&self, poller: &Poller, token: usize) -> Result<()> {
65 // SAFETY: This object's existence validates the invariants of Poller::add
66 unsafe {
67 match self {
68 Self::Socket(raw) => poller.add(*raw, Event::none(token)),
69 Self::Handle(handle) => {
70 poller.add_waitable(*handle, Event::none(token), PollMode::Oneshot)
71 }
72 }
73 }
74 }
75
76 /// Re-registers the object into the reactor.
77 #[inline]
78 pub(crate) fn modify(&self, poller: &Poller, interest: Event) -> Result<()> {
79 // SAFETY: self.raw is a valid file descriptor
80 match self {
81 Self::Socket(raw) => {
82 poller.modify(unsafe { BorrowedSocket::borrow_raw(*raw) }, interest)
83 }
84 Self::Handle(handle) => poller.modify_waitable(
85 unsafe { BorrowedHandle::borrow_raw(*handle) },
86 interest,
87 PollMode::Oneshot,
88 ),
89 }
90 }
91
92 /// Deregisters the object from the reactor.
93 #[inline]
94 pub(crate) fn delete(&self, poller: &Poller) -> Result<()> {
95 // SAFETY: self.raw is a valid file descriptor
96 match self {
97 Self::Socket(raw) => poller.delete(unsafe { BorrowedSocket::borrow_raw(*raw) }),
98 Self::Handle(handle) => {
99 poller.remove_waitable(unsafe { BorrowedHandle::borrow_raw(*handle) })
100 }
101 }
102 }
103 }