runa_wayland_types/
lib.rs

1use std::os::unix::{
2    io::OwnedFd,
3    prelude::{AsRawFd, FromRawFd, RawFd},
4};
5
6use fixed::{traits::ToFixed, types::extra::U8};
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct NewId(pub u32);
9
10impl From<u32> for NewId {
11    fn from(id: u32) -> Self {
12        NewId(id)
13    }
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct Fixed(pub fixed::FixedI32<U8>);
18#[derive(Debug)]
19pub enum Fd {
20    Raw(RawFd),
21    Owned(OwnedFd),
22}
23impl From<RawFd> for Fd {
24    fn from(fd: RawFd) -> Self {
25        Fd::Raw(fd)
26    }
27}
28
29impl From<OwnedFd> for Fd {
30    fn from(fd: OwnedFd) -> Self {
31        Fd::Owned(fd)
32    }
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct Object(pub u32);
37
38impl From<u32> for Object {
39    fn from(id: u32) -> Self {
40        Object(id)
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Copy)]
45pub struct Str<'a>(pub &'a [u8]);
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct String(pub Vec<u8>);
48
49impl From<std::string::String> for String {
50    fn from(s: std::string::String) -> Self {
51        String(s.into_bytes())
52    }
53}
54
55impl<'a> From<&'a [u8]> for Str<'a> {
56    fn from(value: &'a [u8]) -> Self {
57        Str(value)
58    }
59}
60
61impl<'a, const N: usize> From<&'a [u8; N]> for Str<'a> {
62    fn from(value: &'a [u8; N]) -> Self {
63        Str(value)
64    }
65}
66
67impl String {
68    pub fn as_str(&self) -> Str {
69        Str(&self.0[..])
70    }
71}
72
73impl ::std::fmt::Display for NewId {
74    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
75        write!(f, "NewId({})", self.0)
76    }
77}
78
79impl AsRawFd for Fd {
80    fn as_raw_fd(&self) -> RawFd {
81        match self {
82            Fd::Raw(fd) => *fd,
83            Fd::Owned(fd) => fd.as_raw_fd(),
84        }
85    }
86}
87
88impl ::std::cmp::PartialEq for Fd {
89    fn eq(&self, other: &Self) -> bool {
90        self.as_raw_fd() == other.as_raw_fd()
91    }
92}
93
94impl ::std::cmp::Eq for Fd {}
95
96impl Fd {
97    /// Convert a `Fd` into an `OwnedFd`.
98    ///
99    /// # Safety
100    ///
101    /// The `Fd` must own the contained file descriptor, i.e. this file
102    /// descriptor must not be used elsewhere. This is because `OwnedFd`
103    /// will close the file descriptor when it is dropped, and invalidate
104    /// the file descriptor if it's incorrectly used elsewhere.
105    pub unsafe fn assume_owned(&mut self) -> &mut OwnedFd {
106        match self {
107            Fd::Raw(fd) => {
108                *self = Fd::Owned(OwnedFd::from_raw_fd(*fd));
109                match self {
110                    Fd::Owned(fd) => fd,
111                    // Safety: we just assigned OwnedFd to self
112                    Fd::Raw(_) => std::hint::unreachable_unchecked(),
113                }
114            },
115            Fd::Owned(fd) => fd,
116        }
117    }
118
119    /// Take ownership of the file descriptor.
120    ///
121    /// This will return `None` if the `Fd` does not own the file descriptor.
122    pub fn take(&mut self) -> Option<OwnedFd> {
123        match self {
124            Fd::Raw(_) => None,
125            Fd::Owned(fd) => {
126                let mut fd2 = Fd::Raw(fd.as_raw_fd());
127                std::mem::swap(self, &mut fd2);
128                match fd2 {
129                    Fd::Owned(fd) => Some(fd),
130                    // Safety: we just swapped a Fd::Owned into fd2
131                    Fd::Raw(_) => unsafe { std::hint::unreachable_unchecked() },
132                }
133            },
134        }
135    }
136
137    /// Take ownership of the file descriptor.
138    ///
139    /// # Panic
140    ///
141    /// Panics if the `Fd` does not own the file descriptor.
142    pub fn unwrap_owned(self) -> OwnedFd {
143        match self {
144            Fd::Raw(_) => panic!("file descriptor was not owned"),
145            Fd::Owned(fd) => fd,
146        }
147    }
148
149    /// Get a mutable reference to the owned file descriptor.
150    ///
151    /// # Panic
152    ///
153    /// Panics if the `Fd` does not own the file descriptor.
154    pub fn unwrap_owned_mut(&mut self) -> &mut OwnedFd {
155        match self {
156            Fd::Raw(_) => panic!("file descriptor was not owned"),
157            Fd::Owned(fd) => fd,
158        }
159    }
160}
161
162impl Fixed {
163    pub fn from_bits(v: i32) -> Self {
164        Self(fixed::FixedI32::from_bits(v))
165    }
166
167    pub fn from_num<Src: ToFixed>(src: Src) -> Self {
168        Self(fixed::FixedI32::from_num(src))
169    }
170}