From 4975ac79ef43bfeb3b151d1fff4ac18618524002 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Mon, 24 Feb 2025 08:22:58 +0100 Subject: [PATCH] rusticl/util: add missing comment and assert to char_arr_to_cstr I forgot to push those changes... Part-of: --- src/gallium/frontends/rusticl/util/string.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/gallium/frontends/rusticl/util/string.rs b/src/gallium/frontends/rusticl/util/string.rs index 87e32402967..8e6d00f5d4c 100644 --- a/src/gallium/frontends/rusticl/util/string.rs +++ b/src/gallium/frontends/rusticl/util/string.rs @@ -12,9 +12,15 @@ pub fn c_string_to_string(cstr: *const c_char) -> String { String::from(res.unwrap_or("")) } +/// Simple wrapper around [`CStr::from_ptr`] to bind the lifetime to a slice containing the +/// nul-terminated string. +/// /// # Safety /// /// Same as [`CStr::from_ptr`] pub unsafe fn char_arr_to_cstr(c_str: &[c_char]) -> &CStr { + // We make sure that at least the end of the slice is nul-terminated. We don't really care if + // there is another nul within the slice. + debug_assert_eq!(c_str.last().copied(), Some(b'\0' as c_char)); unsafe { CStr::from_ptr(c_str.as_ptr()) } }