summaryrefslogtreecommitdiffstats
path: root/rust/kernel/src/types.rs
blob: e9b4d13d4f8ce4c4146ee75d6f68dfaf2ebfaba2 (plain)
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
// SPDX-License-Identifier: GPL-2.0

use core::ops::Deref;

use crate::bindings;

pub struct Mode(bindings::umode_t);

impl Mode {
    pub fn from_int(m: u16) -> Mode {
        Mode(m)
    }

    pub fn as_int(&self) -> u16 {
        self.0
    }
}

/// A string that is guaranteed to have exactly one NUL byte, which is at the
/// end. Used for interoperability with kernel APIs that take C strings.
#[repr(transparent)]
pub struct CStr<'a>(&'a str);

impl CStr<'_> {
    /// Creates a new CStr from a str without performing any additional checks.
    /// # Safety
    ///
    /// `data` _must_ end with a NUL byte, and should only have only a single
    /// NUL byte, or the string will be truncated.
    pub const unsafe fn new_unchecked(data: &str) -> CStr {
        CStr(data)
    }
}

impl Deref for CStr<'_> {
    type Target = str;

    fn deref(&self) -> &str {
        self.0
    }
}

/// Creates a new `CStr` from a string literal. The string literal should not contain any NUL
/// bytes. Example usage:
/// ```
/// const MY_CSTR: CStr<'static> = cstr!("My awesome CStr!");
/// ```
#[macro_export]
macro_rules! cstr {
    ($str:expr) => {{
        let s = concat!($str, "\x00");
        unsafe { $crate::CStr::new_unchecked(s) }
    }};
}