linux_unsafe/
args.rs

1//! Supporting traits for preparing values to be system call arguments.
2
3/// Trait implemented by types that can be used as raw system call arguments.
4pub trait AsRawV: Copy {
5    fn from_raw_result(raw: crate::raw::V) -> Self;
6    fn to_raw_arg(self) -> crate::raw::V;
7
8    /// Determines whether this value should represent the absense of a
9    /// value when used in a context where that makes sense, such as
10    /// in the final argument of either [`crate::ioctl`] or [`crate::fcntl`]
11    /// when the operation does not use the final argument.
12    #[inline(always)]
13    fn raw_is_void(self) -> bool {
14        false
15    }
16}
17
18macro_rules! trivial_raw_v {
19    ($t:ty) => {
20        impl AsRawV for $t {
21            #[inline(always)]
22            fn from_raw_result(raw: crate::raw::V) -> Self {
23                raw as Self
24            }
25            #[inline(always)]
26            fn to_raw_arg(self) -> crate::raw::V {
27                self as _
28            }
29        }
30    };
31}
32
33trivial_raw_v!(i8);
34trivial_raw_v!(u8);
35trivial_raw_v!(i16);
36trivial_raw_v!(u16);
37trivial_raw_v!(i32);
38trivial_raw_v!(u32);
39trivial_raw_v!(i64);
40trivial_raw_v!(u64);
41trivial_raw_v!(isize);
42trivial_raw_v!(usize);
43
44impl<T> AsRawV for *const T {
45    #[inline(always)]
46    fn from_raw_result(raw: crate::raw::V) -> Self {
47        raw as Self
48    }
49    #[inline(always)]
50    fn to_raw_arg(self) -> crate::raw::V {
51        self as _
52    }
53}
54
55impl<T> AsRawV for *mut T {
56    #[inline(always)]
57    fn from_raw_result(raw: crate::raw::V) -> Self {
58        raw as Self
59    }
60    #[inline(always)]
61    fn to_raw_arg(self) -> crate::raw::V {
62        self as _
63    }
64}
65
66impl AsRawV for () {
67    #[inline(always)]
68    fn from_raw_result(_: crate::raw::V) -> Self {
69        ()
70    }
71    #[inline(always)]
72    fn to_raw_arg(self) -> crate::raw::V {
73        0
74    }
75    #[inline(always)]
76    fn raw_is_void(self) -> bool {
77        true
78    }
79}