• 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::num::NonZeroU8;
15 
16 /// The interested events, such as readable, writeable.
17 #[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord)]
18 pub struct Interest(NonZeroU8);
19 
20 const READABLE: u8 = 0b0001;
21 const WRITABLE: u8 = 0b0010;
22 
23 /// A wrapper that wraps around fd events
24 impl Interest {
25     /// An interest for readable events
26     pub const READABLE: Interest = Interest(unsafe { NonZeroU8::new_unchecked(READABLE) });
27     /// An interest for writeable events
28     pub const WRITABLE: Interest = Interest(unsafe { NonZeroU8::new_unchecked(WRITABLE) });
29 
30     /// Combines two Interest into one.
add(self, other: Interest) -> Interest31     pub const fn add(self, other: Interest) -> Interest {
32         Interest(unsafe { NonZeroU8::new_unchecked(self.0.get() | other.0.get()) })
33     }
34 
35     /// Checks if the interest is for readable events.
is_readable(self) -> bool36     pub const fn is_readable(self) -> bool {
37         (self.0.get() & READABLE) != 0
38     }
39 
40     /// Checks if the interest is for writeable events.
is_writable(self) -> bool41     pub const fn is_writable(self) -> bool {
42         (self.0.get() & WRITABLE) != 0
43     }
44 }
45