summaryrefslogtreecommitdiffstats
path: root/rust/kernel/src/types.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/kernel/src/types.rs')
-rw-r--r--rust/kernel/src/types.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/rust/kernel/src/types.rs b/rust/kernel/src/types.rs
new file mode 100644
index 000000000000..ea5f8d7c2ebb
--- /dev/null
+++ b/rust/kernel/src/types.rs
@@ -0,0 +1,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(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 {
+ &*(data as *const str as *const CStr)
+ }
+}
+
+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 = cstr!("My awesome CStr!");
+/// ```
+#[macro_export]
+macro_rules! cstr {
+ ($str:expr) => {{
+ let s = concat!($str, "\x00");
+ unsafe { $crate::CStr::new_unchecked(s) }
+ }};
+}