Skip to content

Commit 69309bf

Browse files
hoshinolinajannau
authored andcommitted
kernel: str: Add/fix SAFETY comments
Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 7e8b04f commit 69309bf

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
@@ -69,10 +69,10 @@ impl CStr {
6969
/// Returns the length of this string with `NUL`.
7070
#[inline]
7171
pub const fn len_with_nul(&self) -> usize {
72-
// SAFETY: This is one of the invariant of `CStr`.
73-
// We add a `unreachable_unchecked` here to hint the optimizer that
74-
// the value returned from this function is non-zero.
7572
if self.0.is_empty() {
73+
// SAFETY: This is one of the invariant of `CStr`.
74+
// We add a `unreachable_unchecked` here to hint the optimizer that
75+
// the value returned from this function is non-zero.
7676
unsafe { core::hint::unreachable_unchecked() };
7777
}
7878
self.0.len()
@@ -208,6 +208,7 @@ impl CStr {
208208
/// ```
209209
#[inline]
210210
pub unsafe fn as_str_unchecked(&self) -> &str {
211+
// SAFETY: Depends on the above safety contract
211212
unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
212213
}
213214

0 commit comments

Comments
 (0)