Skip to main content

dope_sys/io/file/
open.rs

1use std::ops;
2
3/// One exact `open(2)` flag set.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5#[repr(transparent)]
6pub struct Open(libc::c_int);
7
8impl Open {
9    pub const NONE: Self = Self(0);
10    pub const READ: Self = Self(libc::O_RDONLY);
11    pub const WRITE: Self = Self(libc::O_WRONLY);
12    pub const READ_WRITE: Self = Self(libc::O_RDWR);
13    pub const DIRECTORY: Self = Self(libc::O_RDONLY | libc::O_DIRECTORY);
14    pub const CREATE: Self = Self(libc::O_CREAT);
15    pub const EXCLUSIVE: Self = Self(libc::O_EXCL);
16    pub const APPEND: Self = Self(libc::O_APPEND);
17    pub const TRUNCATE: Self = Self(libc::O_TRUNC);
18    pub const NO_FOLLOW: Self = Self(libc::O_NOFOLLOW);
19    pub const NONBLOCK: Self = Self(libc::O_NONBLOCK);
20    pub const CLOSE_ON_EXEC: Self = Self(libc::O_CLOEXEC);
21
22    pub const fn from_raw(bits: libc::c_int) -> Self {
23        Self(bits)
24    }
25
26    pub const fn union(self, other: Self) -> Self {
27        Self(self.0 | other.0)
28    }
29
30    pub const fn difference(self, other: Self) -> Self {
31        Self(self.0 & !other.0)
32    }
33
34    pub const fn contains(self, other: Self) -> bool {
35        self.0 & other.0 == other.0
36    }
37
38    pub const fn raw(self) -> libc::c_int {
39        self.0
40    }
41}
42
43impl ops::BitOr for Open {
44    type Output = Self;
45
46    fn bitor(self, other: Self) -> Self {
47        Self(self.0 | other.0)
48    }
49}