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 TRUNCATE: Self = Self(libc::O_TRUNC);
17    pub const NO_FOLLOW: Self = Self(libc::O_NOFOLLOW);
18    pub const NONBLOCK: Self = Self(libc::O_NONBLOCK);
19    pub const CLOSE_ON_EXEC: Self = Self(libc::O_CLOEXEC);
20
21    pub const fn union(self, other: Self) -> Self {
22        Self(self.0 | other.0)
23    }
24
25    pub const fn difference(self, other: Self) -> Self {
26        Self(self.0 & !other.0)
27    }
28
29    pub const fn contains(self, other: Self) -> bool {
30        self.0 & other.0 == other.0
31    }
32
33    pub const fn raw(self) -> libc::c_int {
34        self.0
35    }
36}
37
38impl ops::BitOr for Open {
39    type Output = Self;
40
41    fn bitor(self, other: Self) -> Self {
42        Self(self.0 | other.0)
43    }
44}