Skip to content

Commit 8aa2b15

Browse files
committed
kernel: str: Add/fix SAFETY comments
Signed-off-by: Asahi Lina <[email protected]>
1 parent 3fa0394 commit 8aa2b15

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

rust/kernel/str.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ impl CStr {
7272
/// Returns the length of this string with `NUL`.
7373
#[inline]
7474
pub const fn len_with_nul(&self) -> usize {
75-
// SAFETY: This is one of the invariant of `CStr`.
76-
// We add a `unreachable_unchecked` here to hint the optimizer that
77-
// the value returned from this function is non-zero.
7875
if self.0.is_empty() {
76+
// SAFETY: This is one of the invariant of `CStr`.
77+
// We add a `unreachable_unchecked` here to hint the optimizer that
78+
// the value returned from this function is non-zero.
7979
unsafe { core::hint::unreachable_unchecked() };
8080
}
8181
self.0.len()
@@ -198,6 +198,7 @@ impl CStr {
198198
/// ```
199199
#[inline]
200200
pub unsafe fn as_str_unchecked(&self) -> &str {
201+
// SAFETY: Depends on the above safety contract
201202
unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
202203
}
203204

0 commit comments

Comments
 (0)