• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::io;
15 use std::time::Duration;
16 
17 use crate::{Events, Interest, Selector, Source, Token};
18 
19 /// Registers and deregisters an io's fd
20 pub struct Poll {
21     selector: Selector,
22 }
23 
24 impl Poll {
25     /// Creates a new Poll.
new() -> io::Result<Poll>26     pub fn new() -> io::Result<Poll> {
27         Selector::new().map(|selector| Poll {
28             selector: { selector },
29         })
30     }
31 
32     /// Gets all interested io events within a time limit and writes them into
33     /// `event`.
34     ///
35     /// If `timeout` is none, then it will block the current thread until an
36     /// event arrives.
poll(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()>37     pub fn poll(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()> {
38         self.selector.select(events, timeout)
39     }
40 
41     /// Registers the I/O source's fd in order to monitor its io events.
register<S>(&self, source: &mut S, token: Token, interests: Interest) -> io::Result<()> where S: Source + ?Sized,42     pub fn register<S>(&self, source: &mut S, token: Token, interests: Interest) -> io::Result<()>
43     where
44         S: Source + ?Sized,
45     {
46         source.register(&self.selector, token, interests)
47     }
48 
49     /// Re-registers the I/O source's fd in order to monitor its io events.
reregister<S>(&self, source: &mut S, token: Token, interests: Interest) -> io::Result<()> where S: Source + ?Sized,50     pub fn reregister<S>(&self, source: &mut S, token: Token, interests: Interest) -> io::Result<()>
51     where
52         S: Source + ?Sized,
53     {
54         source.reregister(&self.selector, token, interests)
55     }
56 
57     /// De-registers the I/O source's fd so the Poll will no longer monitor its
58     /// io events.
deregister<S>(&self, source: &mut S) -> io::Result<()> where S: Source + ?Sized,59     pub fn deregister<S>(&self, source: &mut S) -> io::Result<()>
60     where
61         S: Source + ?Sized,
62     {
63         source.deregister(&self.selector)
64     }
65 
66     /// Returns the selector of the `Poll`
selector(&self) -> &Selector67     pub fn selector(&self) -> &Selector {
68         &self.selector
69     }
70 }
71 
72 impl std::fmt::Debug for Poll {
fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result73     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74         write!(fmt, "({:?})", self.selector)
75     }
76 }
77