interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / macros.rs

108 lines · 3047 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3/// Encode a string literal as a [`&CStr8`].
4///
5/// The encoding is done at compile time, so the result can be used in a
6/// `const` item.
7///
8/// An empty string containing just a null character can be created with either
9/// `cstr8!()` or `cstr8!("")`.
10///
11/// # Example
12///
13/// ```
14/// use uefi::{CStr8, cstr8};
15///
16/// const S: &CStr8 = cstr8!("abÿ");
17/// assert_eq!(S.as_bytes(), [97, 98, 255, 0]);
18///
19/// const EMPTY: &CStr8 = cstr8!();
20/// assert_eq!(EMPTY.as_bytes(), [0]);
21/// assert_eq!(cstr8!(""), EMPTY);
22/// ```
23///
24/// [`&CStr8`]: crate::CStr8
25#[macro_export]
26macro_rules! cstr8 {
27 () => {{
28 const S: &[u8] = &[0];
29 // SAFETY: `S` is a trivially correct Latin-1 C string.
30 unsafe { $crate::CStr8::from_bytes_with_nul_unchecked(S) }
31 }};
32 ($s:literal) => {{
33 // Use `const` values here to force errors to happen at compile
34 // time.
35
36 // Add one for the null char.
37 const NUM_CHARS: usize = $crate::data_types::str_num_latin1_chars($s) + 1;
38
39 const VAL: [u8; NUM_CHARS] = $crate::data_types::str_to_latin1($s);
40
41 // SAFETY: the `str_to_latin1` function always produces a valid Latin-1
42 // string with a trailing null character.
43 unsafe { $crate::CStr8::from_bytes_with_nul_unchecked(&VAL) }
44 }};
45}
46
47/// Encodes a char literal as a [`Char16`].
48///
49/// The encoding is done at compile time, so the result can be used in a
50/// `const` item.
51///
52/// # Example
53///
54/// ```
55/// use uefi::{CStr8, cstr8};
56///
57/// const S: &CStr8 = cstr8!("ÿ");
58/// assert_eq!(S.as_bytes(), [255, 0]);
59/// ```
60///
61/// [`Char16`]: crate::Char16
62#[macro_export]
63macro_rules! char16 {
64 ($s:literal) => {{
65 const C: $crate::Char16 = match $crate::Char16::try_from_char($s) {
66 Ok(c) => c,
67 Err(_) => panic!("input contains a character which cannot be represented in UCS-2"),
68 };
69 C
70 }};
71}
72
73/// Encode a string literal as a [`&CStr16`].
74///
75/// The encoding is done at compile time, so the result can be used in a
76/// `const` item.
77///
78/// An empty string containing just a null character can be created with either
79/// `cstr16!()` or `cstr16!("")`.
80///
81/// # Example
82///
83/// ```
84/// use uefi::{CStr16, cstr16};
85///
86/// const S: &CStr16 = cstr16!("abc");
87/// assert_eq!(S.to_u16_slice_with_nul(), [97, 98, 99, 0]);
88///
89/// const EMPTY: &CStr16 = cstr16!();
90/// assert_eq!(EMPTY.to_u16_slice_with_nul(), [0]);
91/// assert_eq!(cstr16!(""), EMPTY);
92/// ```
93///
94/// [`&CStr16`]: crate::CStr16
95#[macro_export]
96macro_rules! cstr16 {
97 () => {{
98 const S: &[u16] = &[0];
99 // SAFETY: `S` is a trivially correct UCS-2 C string.
100 unsafe { $crate::CStr16::from_u16_with_nul_unchecked(S) }
101 }};
102 ($s:literal) => {{
103 const S: &[u16] = &$crate::ucs2_cstr!($s);
104 // SAFETY: the ucs2_cstr macro always produces a valid UCS-2 string with
105 // a trailing null character.
106 unsafe { $crate::CStr16::from_u16_with_nul_unchecked(S) }
107 }};
108}