kstat/
kstat_named.rs

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
use super::ffi;
use std::borrow::Cow;

/// The types of data a kstat named/value pair can contain
#[derive(Debug)]
pub enum KstatNamedData {
    /// KSTAT_DATA_CHAR
    DataChar(i8),
    /// KSTAT_DATA_INT32
    DataInt32(i32),
    /// KSTAT_DATA_UINT32
    DataUInt32(u32),
    /// KSTAT_DATA_INT64 or KSTAT_DATA_LONG
    DataInt64(i64),
    /// KSTAT_DATA_UINT64 or KSTAT_DATA_ULONG
    DataUInt64(u64),
    /// KSTAT_DATA_STRING
    DataString(String),
}

#[derive(Debug)]
pub(crate) struct KstatNamed {
    inner: *const ffi::kstat_named_t,
}

impl KstatNamed {
    pub fn new(ptr: *const ffi::kstat_named_t) -> Self {
        KstatNamed { inner: ptr }
    }

    pub fn name(&self) -> Cow<str> {
        unsafe { (*self.inner).get_name() }
    }

    fn get_data_type(&self) -> u8 {
        unsafe { (*self.inner).data_type }
    }

    pub fn read(&self) -> (String, KstatNamedData) {
        (self.name().into_owned(), self.into())
    }
}

impl<'a> From<&'a KstatNamed> for KstatNamedData {
    fn from(t: &'a KstatNamed) -> Self {
        match t.get_data_type() {
            ffi::KSTAT_DATA_CHAR => KstatNamedData::DataChar(unsafe { (*t.inner).value_as_char() }),
            ffi::KSTAT_DATA_INT32 => {
                KstatNamedData::DataInt32(unsafe { (*t.inner).value_as_i32() })
            }
            ffi::KSTAT_DATA_UINT32 => {
                KstatNamedData::DataUInt32(unsafe { (*t.inner).value_as_u32() })
            }
            ffi::KSTAT_DATA_INT64 => {
                KstatNamedData::DataInt64(unsafe { (*t.inner).value_as_i64() })
            }
            ffi::KSTAT_DATA_UINT64 => {
                KstatNamedData::DataUInt64(unsafe { (*t.inner).value_as_u64() })
            }
            ffi::KSTAT_DATA_STRING => {
                KstatNamedData::DataString(unsafe { (*t.inner).value_as_string() })
            }
            _ => panic!("unknown kstat data type"),
        }
    }
}