Skip to main content

dope_sys/kernel/
raw.rs

1use std::num;
2
3use o3::base::num::bounded;
4
5use crate::{Errno, kernel};
6
7/// Access to errno immediately after a native operation reports failure.
8/// # Safety
9/// `read` returns the unchanged native errno slot of the calling thread.
10pub unsafe trait Source {
11    /// Reads this thread's unchanged native errno slot.
12    fn read() -> i32;
13}
14
15/// A positive errno established by one native failure or completion field.
16#[derive(Clone, Copy, Debug)]
17#[repr(transparent)]
18pub struct Code(num::NonZero<i32>);
19
20impl Code {
21    /// Retains the positive error number established by one failed native call.
22    ///
23    /// # Safety
24    /// A failed native call left this thread's errno positive and unchanged.
25    pub unsafe fn last<S: Source + ?Sized>() -> Self {
26        // SAFETY: the caller retains the immediately preceding native failure's positive errno.
27        unsafe { Self::new(S::read()) }
28    }
29
30    /// Retains the positive domain established by the native producer.
31    ///
32    /// # Safety
33    /// `raw` is positive, as established by the unchanged native error field.
34    pub const unsafe fn new(raw: i32) -> Self {
35        // SAFETY: the caller retains the native producer's positive errno proof.
36        Self(unsafe { num::NonZero::new_unchecked(raw) })
37    }
38
39    /// Retains the errno one native completion field reports, if any.
40    ///
41    /// # Safety
42    /// `raw` is that completion's exact field: zero or a positive c_int.
43    pub unsafe fn from_native(raw: libc::intptr_t) -> Option<Self> {
44        num::NonZero::new(raw as i32).map(Self)
45    }
46}
47
48impl From<Code> for Errno {
49    fn from(code: Code) -> Self {
50        Self(code.0)
51    }
52}
53
54/// A native scalar retained with the exact domain its producer guarantees, so
55/// every decoder below is total: the producer's ABI proof is discharged once at
56/// construction and reused without a second range check.
57#[derive(Clone, Copy, Debug)]
58#[repr(transparent)]
59pub struct Status<const MIN: libc::c_int, const MAX: libc::c_int>(libc::c_int);
60
61impl<const MIN: libc::c_int, const MAX: libc::c_int> Status<MIN, MAX> {
62    /// Retains the return domain established by one native operation.
63    ///
64    /// # Safety
65    /// `raw` lies in `MIN..=MAX`, as guaranteed by the operation producing it.
66    pub const unsafe fn new(raw: libc::c_int) -> Self {
67        Self(raw)
68    }
69}
70
71impl Status<-1, 0> {
72    /// Decodes a native zero-or-minus-one operation without erasing errno.
73    ///
74    /// # Safety
75    /// On failure, `S` reads this call's positive, unchanged errno.
76    pub unsafe fn try_zero<S: Source + ?Sized, E: From<Errno>>(self) -> Result<(), E> {
77        // SAFETY: -1..=0 is Exact<0>'s domain; S keeps the caller's errno contract.
78        unsafe { Exact::<0>(self.0).try_admit::<S, E>() }
79    }
80}
81
82impl Status<-1, { i32::MAX }> {
83    /// Retains a native count whose exact syscall operand already bounds it.
84    ///
85    /// # Safety
86    /// `raw` is the unchanged return of that count-returning syscall: native -1 or `0..=i32::MAX`.
87    pub const unsafe fn from_count(raw: libc::ssize_t) -> Self {
88        Self(raw as libc::c_int)
89    }
90
91    /// Converts the retained native scalar domain without checking its range again.
92    ///
93    /// # Safety
94    /// On failure, `S` reads this call's positive, unchanged errno.
95    pub unsafe fn try_value<S: Source + ?Sized, E: From<Errno>>(self) -> Result<kernel::Value, E> {
96        if self.0 == -1 {
97            // SAFETY: the caller retains this failed call's positive, unchanged errno.
98            Err(E::from(unsafe { Code::last::<S>() }.into()))
99        } else {
100            Ok(kernel::Value(self.0))
101        }
102    }
103
104    /// Decodes one count-returning operation against the exact extent it was given.
105    ///
106    /// # Safety
107    /// On failure, `S` reads this call's positive, unchanged errno.
108    pub unsafe fn try_count<S: Source + ?Sized, E: From<Errno>>(self) -> Result<Count, E> {
109        // SAFETY: the caller retains this failed call's positive, unchanged errno.
110        Ok(Count::new(unsafe { self.try_value::<S, E>() }?))
111    }
112
113    /// Tightens this scalar to one operation's exact `-1`-or-`VALUE` return.
114    ///
115    /// # Safety
116    /// That operation reports exactly `VALUE` or -1 for these arguments.
117    pub const unsafe fn admit_exact<const VALUE: libc::c_int>(self) -> Exact<VALUE> {
118        Exact(self.0)
119    }
120}
121
122/// One native return whose producer reports exactly -1 or its one success value,
123/// so the ABI's exactness proof is discharged once at admission and the decoder
124/// below is total without a second range check.
125#[derive(Clone, Copy, Debug)]
126#[repr(transparent)]
127pub struct Exact<const VALUE: libc::c_int>(libc::c_int);
128
129impl<const VALUE: libc::c_int> Exact<VALUE> {
130    /// Decodes the admitted success value without re-deriving its domain.
131    ///
132    /// # Safety
133    /// On failure, `S` reads this call's positive, unchanged errno.
134    pub unsafe fn try_admit<S: Source + ?Sized, E: From<Errno>>(self) -> Result<(), E> {
135        if self.0 == VALUE {
136            Ok(())
137        } else {
138            // SAFETY: the caller retains this failed call's positive, unchanged errno.
139            Err(E::from(unsafe { Code::last::<S>() }.into()))
140        }
141    }
142}
143
144impl Status<0, { i32::MAX }> {
145    /// Decodes the error code returned directly by a pthread operation.
146    pub fn try_pthread(self) -> Result<(), Errno> {
147        if self.0 == 0 {
148            Ok(())
149        } else {
150            // SAFETY: pthread's retained return domain makes every failure positive.
151            Err(unsafe { Code::new(self.0) }.into())
152        }
153    }
154}
155
156/// One live descriptor whose native producer or retained owner establishes its
157/// nonnegative domain, so later uses do not check that range again.
158#[derive(Clone, Copy, Debug)]
159#[repr(transparent)]
160pub struct Descriptor(bounded::raw::U32<0, { i32::MAX as u32 }>);
161
162impl Descriptor {
163    /// # Safety
164    /// `raw` is the unchanged live nonnegative descriptor produced by a successful
165    /// native operation or retained by its exclusive owner.
166    pub const unsafe fn new(raw: libc::c_int) -> Self {
167        // SAFETY: the native nonnegative c_int retains the exact 0..=i32::MAX bound.
168        Self(unsafe { bounded::raw::U32::from_proven(raw as u32) })
169    }
170
171    pub const fn get(self) -> libc::c_int {
172        self.0.get() as libc::c_int
173    }
174}
175
176/// One completed native count retaining its admitted ABI bound.
177#[derive(Clone, Copy, Debug)]
178#[repr(transparent)]
179pub struct Count(bounded::raw::U32<0, { i32::MAX as u32 }>);
180
181impl Count {
182    /// Retains the count one count-returning operation completed within its operand.
183    const fn new(value: kernel::Value) -> Self {
184        // SAFETY: Value admits exactly 0..=i32::MAX, the bound retained here.
185        Self(unsafe { bounded::raw::U32::from_proven(value.get() as u32) })
186    }
187
188    pub const fn get(self) -> usize {
189        self.0.into_usize()
190    }
191
192    pub const fn bounded(self) -> bounded::raw::U32<0, { i32::MAX as u32 }> {
193        self.0
194    }
195}