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 use std::ops; 20 21 const READABLE: u8 = 0b0001; 22 const WRITABLE: u8 = 0b0010; 23 24 /// A wrapper that wraps around fd events 25 impl Interest { 26 /// An interest for readable events 27 pub const READABLE: Interest = Interest(unsafe { NonZeroU8::new_unchecked(READABLE) }); 28 /// An interest for writeable events 29 pub const WRITABLE: Interest = Interest(unsafe { NonZeroU8::new_unchecked(WRITABLE) }); 30 31 /// Combines two Interest into one. add(self, other: Interest) -> Interest32 pub const fn add(self, other: Interest) -> Interest { 33 Interest(unsafe { NonZeroU8::new_unchecked(self.0.get() | other.0.get()) }) 34 } 35 36 /// Checks if the interest is for readable events. is_readable(self) -> bool37 pub const fn is_readable(self) -> bool { 38 (self.0.get() & READABLE) != 0 39 } 40 41 /// Checks if the interest is for writeable events. is_writable(self) -> bool42 pub const fn is_writable(self) -> bool { 43 (self.0.get() & WRITABLE) != 0 44 } 45 } 46 47 impl ops::BitOr for Interest { 48 type Output = Self; 49 50 #[inline] bitor(self, other: Self) -> Self51 fn bitor(self, other: Self) -> Self { 52 self.add(other) 53 } 54 } 55