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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// ctl_type.rs

use ctl_value::*;

/// An Enum that represents a sysctl's type information.
///
/// # Example
///
/// ```ignore
/// # extern crate sysctl;
/// #
/// let val_enum = &sysctl::value("kern.osrevision").unwrap();
/// let val_type: sysctl::CtlType = val_enum.into();
/// assert_eq!(val_type, sysctl::CtlType::Int);
/// ```
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(u32)]
pub enum CtlType {
    Node = 1,
    Int = 2,
    String = 3,
    S64 = 4,
    Struct = 5,
    Uint = 6,
    Long = 7,
    Ulong = 8,
    U64 = 9,
    U8 = 10,
    U16 = 11,
    S8 = 12,
    S16 = 13,
    S32 = 14,
    U32 = 15,
    // Added custom types below
    None = 0,
    #[cfg(target_os = "freebsd")]
    Temperature = 16,
}
impl std::convert::From<u32> for CtlType {
    fn from(t: u32) -> Self {
        assert!(t <= 16);
        unsafe { std::mem::transmute(t) }
    }
}
impl<'a> std::convert::From<&'a CtlValue> for CtlType {
    fn from(t: &'a CtlValue) -> Self {
        match t {
            &CtlValue::None => CtlType::None,
            &CtlValue::Node(_) => CtlType::Node,
            &CtlValue::Int(_) => CtlType::Int,
            &CtlValue::String(_) => CtlType::String,
            &CtlValue::S64(_) => CtlType::S64,
            &CtlValue::Struct(_) => CtlType::Struct,
            &CtlValue::Uint(_) => CtlType::Uint,
            &CtlValue::Long(_) => CtlType::Long,
            &CtlValue::Ulong(_) => CtlType::Ulong,
            &CtlValue::U64(_) => CtlType::U64,
            &CtlValue::U8(_) => CtlType::U8,
            &CtlValue::U16(_) => CtlType::U16,
            &CtlValue::S8(_) => CtlType::S8,
            &CtlValue::S16(_) => CtlType::S16,
            &CtlValue::S32(_) => CtlType::S32,
            &CtlValue::U32(_) => CtlType::U32,
            #[cfg(target_os = "freebsd")]
            &CtlValue::Temperature(_) => CtlType::Temperature,
        }
    }
}

impl CtlType {
    pub fn min_type_size(self: &Self) -> usize {
        match self {
            &CtlType::None => 0,
            &CtlType::Node => 0,
            &CtlType::Int => std::mem::size_of::<libc::c_int>(),
            &CtlType::String => 0,
            &CtlType::S64 => std::mem::size_of::<i64>(),
            &CtlType::Struct => 0,
            &CtlType::Uint => std::mem::size_of::<libc::c_uint>(),
            &CtlType::Long => std::mem::size_of::<libc::c_long>(),
            &CtlType::Ulong => std::mem::size_of::<libc::c_ulong>(),
            &CtlType::U64 => std::mem::size_of::<u64>(),
            &CtlType::U8 => std::mem::size_of::<u8>(),
            &CtlType::U16 => std::mem::size_of::<u16>(),
            &CtlType::S8 => std::mem::size_of::<i8>(),
            &CtlType::S16 => std::mem::size_of::<i16>(),
            &CtlType::S32 => std::mem::size_of::<i32>(),
            &CtlType::U32 => std::mem::size_of::<u32>(),
            // Added custom types below
            #[cfg(target_os = "freebsd")]
            &CtlType::Temperature => 0,
        }
    }
}