Skip to content

Commit 10a7108

Browse files
tamirdojeda
authored andcommitted
rust: str: add CStr methods matching core::ffi::CStr
Prepare for replacing `CStr` with `core::ffi::CStr` by soft-deprecating methods which don't exist on `core::ffi::CStr`. We could keep `as_bytes{,_with_nul}` through an extension trait but seeing as we have to introduce `as_char_ptr_in_const_context` as a free function, we may as well introduce `to_bytes{,_with_nul}` here to allow downstream code to migrate in one cycle rather than two. Link: Rust-for-Linux/linux#1075 Signed-off-by: Tamir Duberstein <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Reworded title. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 0f6d225 commit 10a7108

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

rust/kernel/str.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ macro_rules! b_str {
175175
}};
176176
}
177177

178+
/// Returns a C pointer to the string.
179+
// It is a free function rather than a method on an extension trait because:
180+
//
181+
// - error[E0379]: functions in trait impls cannot be declared const
182+
#[inline]
183+
pub const fn as_char_ptr_in_const_context(c_str: &CStr) -> *const c_char {
184+
c_str.0.as_ptr()
185+
}
186+
178187
/// Possible errors when using conversion functions in [`CStr`].
179188
#[derive(Debug, Clone, Copy)]
180189
pub enum CStrConvertError {
@@ -294,23 +303,45 @@ impl CStr {
294303
}
295304

296305
/// Returns a C pointer to the string.
306+
///
307+
/// Using this function in a const context is deprecated in favor of
308+
/// [`as_char_ptr_in_const_context`] in preparation for replacing `CStr` with `core::ffi::CStr`
309+
/// which does not have this method.
297310
#[inline]
298311
pub const fn as_char_ptr(&self) -> *const c_char {
299-
self.0.as_ptr()
312+
as_char_ptr_in_const_context(self)
300313
}
301314

302315
/// Convert the string to a byte slice without the trailing `NUL` byte.
303316
#[inline]
304-
pub fn as_bytes(&self) -> &[u8] {
317+
pub fn to_bytes(&self) -> &[u8] {
305318
&self.0[..self.len()]
306319
}
307320

321+
/// Convert the string to a byte slice without the trailing `NUL` byte.
322+
///
323+
/// This function is deprecated in favor of [`Self::to_bytes`] in preparation for replacing
324+
/// `CStr` with `core::ffi::CStr` which does not have this method.
325+
#[inline]
326+
pub fn as_bytes(&self) -> &[u8] {
327+
self.to_bytes()
328+
}
329+
308330
/// Convert the string to a byte slice containing the trailing `NUL` byte.
309331
#[inline]
310-
pub const fn as_bytes_with_nul(&self) -> &[u8] {
332+
pub const fn to_bytes_with_nul(&self) -> &[u8] {
311333
&self.0
312334
}
313335

336+
/// Convert the string to a byte slice containing the trailing `NUL` byte.
337+
///
338+
/// This function is deprecated in favor of [`Self::to_bytes_with_nul`] in preparation for
339+
/// replacing `CStr` with `core::ffi::CStr` which does not have this method.
340+
#[inline]
341+
pub const fn as_bytes_with_nul(&self) -> &[u8] {
342+
self.to_bytes_with_nul()
343+
}
344+
314345
/// Yields a [`&str`] slice if the [`CStr`] contains valid UTF-8.
315346
///
316347
/// If the contents of the [`CStr`] are valid UTF-8 data, this

0 commit comments

Comments
 (0)