safa_api/syscalls/types.rs
1pub(crate) trait IntoSyscallArg {
2 fn into_syscall_arg(self) -> usize;
3}
4
5macro_rules! impl_into_syscall_as {
6 ($ty:ty) => {
7 impl IntoSyscallArg for $ty {
8 #[inline(always)]
9 fn into_syscall_arg(self) -> usize {
10 self as usize
11 }
12 }
13 };
14}
15
16impl_into_syscall_as!(usize);
17impl_into_syscall_as!(isize);
18impl_into_syscall_as!(u8);
19impl_into_syscall_as!(i8);
20impl_into_syscall_as!(u16);
21impl_into_syscall_as!(i16);
22impl_into_syscall_as!(u32);
23impl_into_syscall_as!(i32);
24impl_into_syscall_as!(u64);
25impl_into_syscall_as!(i64);
26impl_into_syscall_as!(u128);
27impl_into_syscall_as!(i128);
28impl_into_syscall_as!(bool);
29
30impl<T> IntoSyscallArg for *const T {
31 #[inline(always)]
32 fn into_syscall_arg(self) -> usize {
33 self as usize
34 }
35}
36
37impl<T> IntoSyscallArg for *mut T {
38 #[inline(always)]
39 fn into_syscall_arg(self) -> usize {
40 self as usize
41 }
42}
43
44impl IntoSyscallArg for safa_abi::raw::io::OpenOptions {
45 #[inline(always)]
46 fn into_syscall_arg(self) -> usize {
47 let bits: u8 = unsafe { core::mem::transmute(self) };
48 bits.into_syscall_arg()
49 }
50}
51
52use crate::errors::ErrorStatus;
53
54/// A nullable muttable pointer to `T`
55///
56/// guaranteed to be accepted by the syscall if it is null,
57/// however the syscall will return [`ErrorStatus::InvalidPtr`] if it is not aligned to `align_of::<T>()`
58///
59/// this is typically for the syscall to return optional values
60pub type OptionalPtrMut<T> = *mut T;
61
62/// A nullable imuttable pointer to `T`
63///
64/// guaranteed to be accepted by the syscall if it is null,
65/// however the syscall will return [`ErrorStatus::InvalidPtr`] if it is not aligned to `align_of::<T>()`
66///
67/// this is typically for the syscall to return optional values
68pub type OptionalPtr<T> = *const T;
69
70/// A muttable pointer to `T`
71///
72/// the syscall will return [`ErrorStatus::InvalidPtr`] if it is not aligned to `align_of::<T>()` or if it is null
73///
74/// typically used for the syscall to return a value
75pub type RequiredPtrMut<T> = *mut T;
76
77/// An immuttable pointer to `T`
78///
79/// the syscall will return [`ErrorStatus::InvalidPtr`] if it is not aligned to `align_of::<T>()` or if it is null
80///
81/// typically used for the syscall to return a value
82pub type RequiredPtr<T> = *const T;
83
84/// An immuttable pointer to a byte array
85///
86/// the syscall will return [`ErrorStatus::InvalidPtr`] if it is null
87///
88/// the syscall will return [`ErrorStatus::InvalidStr`] if it is not valid utf-8
89///
90/// typically followed by a `len` parameter to specify the length of the string
91pub type StrPtr = RequiredPtr<u8>;
92
93/// A muttable pointer to a byte array
94///
95/// the syscall will return [`ErrorStatus::InvalidPtr`] if it is null
96///
97/// typically used for the syscall to return a string meaning that after the syscall is successful it should contain a valid utf-8 string
98///
99/// typically followed by a `len` parameter to specify the length of the string
100pub type StrPtrMut = RequiredPtrMut<u8>;
101
102/// An optional immuttable nullable pointer to a byte array
103///
104/// the syscall will return [`ErrorStatus::InvalidStr`] if it is not null and is not valid utf-8
105///
106/// typically followed by a `len` parameter to specify the length of the string
107///
108/// can be null
109pub type OptionalStrPtr = OptionalPtr<u8>;
110
111/// An opaque type that represents a syscall result
112/// the underlying type is a 16 bit unsigned integer, in which 0 is success and any other value is in error
113/// represented by the [`ErrorStatus`] enum
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115#[repr(transparent)]
116pub struct SyscallResult(u16);
117
118impl SyscallResult {
119 #[inline(always)]
120 pub const fn into_result(self) -> Result<(), ErrorStatus> {
121 match self.0 {
122 0 => Ok(()),
123 x => unsafe { Err(core::mem::transmute(x)) },
124 }
125 }
126
127 #[inline(always)]
128 pub const fn is_success(self) -> bool {
129 self.0 == 0
130 }
131}
132
133/// A process ID
134pub type Pid = u32;
135/// A thread ID
136pub type Cid = u32;
137
138/// A resource id
139/// this is a generic type that can be used to represent any resource (file, directory, device, directory iterator, etc.)
140pub type Ri = usize;