1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Supporting traits for preparing values to be system call arguments.

/// Trait implemented by types that can be used as raw system call arguments.
pub trait AsRawV: Copy {
    fn from_raw_result(raw: crate::raw::V) -> Self;
    fn to_raw_arg(self) -> crate::raw::V;

    /// Determines whether this value should represent the absense of a
    /// value when used in a context where that makes sense, such as
    /// in the final argument of either [`crate::ioctl`] or [`crate::fcntl`]
    /// when the operation does not use the final argument.
    #[inline(always)]
    fn raw_is_void(self) -> bool {
        false
    }
}

macro_rules! trivial_raw_v {
    ($t:ty) => {
        impl AsRawV for $t {
            #[inline(always)]
            fn from_raw_result(raw: crate::raw::V) -> Self {
                raw as Self
            }
            #[inline(always)]
            fn to_raw_arg(self) -> crate::raw::V {
                self as _
            }
        }
    };
}

trivial_raw_v!(i8);
trivial_raw_v!(u8);
trivial_raw_v!(i16);
trivial_raw_v!(u16);
trivial_raw_v!(i32);
trivial_raw_v!(u32);
trivial_raw_v!(i64);
trivial_raw_v!(u64);
trivial_raw_v!(isize);
trivial_raw_v!(usize);

impl<T> AsRawV for *const T {
    #[inline(always)]
    fn from_raw_result(raw: crate::raw::V) -> Self {
        raw as Self
    }
    #[inline(always)]
    fn to_raw_arg(self) -> crate::raw::V {
        self as _
    }
}

impl<T> AsRawV for *mut T {
    #[inline(always)]
    fn from_raw_result(raw: crate::raw::V) -> Self {
        raw as Self
    }
    #[inline(always)]
    fn to_raw_arg(self) -> crate::raw::V {
        self as _
    }
}

impl AsRawV for () {
    #[inline(always)]
    fn from_raw_result(_: crate::raw::V) -> Self {
        ()
    }
    #[inline(always)]
    fn to_raw_arg(self) -> crate::raw::V {
        0
    }
    #[inline(always)]
    fn raw_is_void(self) -> bool {
        true
    }
}