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
44use super::ErrorStatus;
45
46/// A nullable muttable pointer to `T`
47///
48/// garuanteed to be accepted by the syscall if it is null,
49/// however the syscall will return [`ErrorStatus::InvaildPtr`] if it is not aligned to `align_of::<T>()`
50///
51/// this is typically for the syscall to return optional values
52pub type OptionalPtrMut<T> = *mut T;
53
54/// A nullable imuttable pointer to `T`
55///
56/// garuanteed to be accepted by the syscall if it is null,
57/// however the syscall will return [`ErrorStatus::InvaildPtr`] if it is not aligned to `align_of::<T>()`
58///
59/// this is typically for the syscall to return optional values
60pub type OptionalPtr<T> = *const T;
61
62/// A muttable pointer to `T`
63///
64/// the syscall will return [`ErrorStatus::InvaildPtr`] if it is not aligned to `align_of::<T>()` or if it is null
65///
66/// typically used for the syscall to return a value
67pub type RequiredPtrMut<T> = *mut T;
68
69/// An immuttable pointer to `T`
70///
71/// the syscall will return [`ErrorStatus::InvaildPtr`] if it is not aligned to `align_of::<T>()` or if it is null
72///
73/// typically used for the syscall to return a value
74pub type RequiredPtr<T> = *const T;
75
76/// An immuttable pointer to a byte array
77///
78/// the syscall will return [`ErrorStatus::InvaildPtr`] if it is null
79///
80/// the syscall will return [`ErrorStatus::InvaildStr`] if it is not valid utf-8
81///
82/// typically followed by a `len` parameter to specify the length of the string
83pub type StrPtr = RequiredPtr<u8>;
84
85/// A muttable pointer to a byte array
86///
87/// the syscall will return [`ErrorStatus::InvaildPtr`] if it is null
88///
89/// typically used for the syscall to return a string meaning that after the syscall is successful it should contain a valid utf-8 string
90///
91/// typically followed by a `len` parameter to specify the length of the string
92pub type StrPtrMut = RequiredPtrMut<u8>;
93
94/// An optional immuttable nullable pointer to a byte array
95///
96/// the syscall will return [`ErrorStatus::InvaildStr`] if it is not null and is not valid utf-8
97///
98/// typically followed by a `len` parameter to specify the length of the string
99///
100/// can be null
101pub type OptionalStrPtr = OptionalPtr<u8>;
102
103/// An opaque type that represents a syscall result
104/// the underlying type is a 16 bit unsigned integer, in which 0 is success and any other value is in error
105/// respresented by the [`ErrorStatus`] enum
106#[derive(Debug, Clone, Copy, PartialEq, Eq)]
107#[repr(transparent)]
108pub struct SyscallResult(u16);
109
110impl SyscallResult {
111    #[inline(always)]
112    pub const fn into_result(self) -> Result<(), ErrorStatus> {
113        match self.0 {
114            0 => Ok(()),
115            x => unsafe { Err(core::mem::transmute(x)) },
116        }
117    }
118
119    #[inline(always)]
120    pub const fn is_success(self) -> bool {
121        self.0 == 0
122    }
123}
124
125/// A process id
126pub type Pid = usize;
127
128/// A resource id
129/// this is a generic type that can be used to represent any resource (file, directory, device, directory iterator, etc.)
130pub type Ri = usize;