mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-03-22 07:27:12 +08:00
rust: kernel: use core::ffi::CStr method names
Prepare for `core::ffi::CStr` taking the place of `kernel::str::CStr` by avoiding methods that only exist on the latter. Also avoid `Deref<Target=BStr> for CStr` as that impl doesn't exist on `core::ffi::CStr`. Link: https://github.com/Rust-for-Linux/linux/issues/1075 Signed-off-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20250704-core-cstr-prepare-v1-6-a91524037783@gmail.com [ Reworded title. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
committed by
Miguel Ojeda
parent
10a7108d4b
commit
1523590203
@@ -188,7 +188,7 @@ impl fmt::Debug for Error {
|
||||
Some(name) => f
|
||||
.debug_tuple(
|
||||
// SAFETY: These strings are ASCII-only.
|
||||
unsafe { core::str::from_utf8_unchecked(name) },
|
||||
unsafe { core::str::from_utf8_unchecked(name.to_bytes()) },
|
||||
)
|
||||
.finish(),
|
||||
}
|
||||
|
||||
@@ -57,11 +57,11 @@ impl fmt::Display for BStr {
|
||||
/// # use kernel::{prelude::fmt, b_str, str::{BStr, CString}};
|
||||
/// let ascii = b_str!("Hello, BStr!");
|
||||
/// let s = CString::try_from_fmt(fmt!("{ascii}"))?;
|
||||
/// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes());
|
||||
/// assert_eq!(s.to_bytes(), "Hello, BStr!".as_bytes());
|
||||
///
|
||||
/// let non_ascii = b_str!("🦀");
|
||||
/// let s = CString::try_from_fmt(fmt!("{non_ascii}"))?;
|
||||
/// assert_eq!(s.as_bytes(), "\\xf0\\x9f\\xa6\\x80".as_bytes());
|
||||
/// assert_eq!(s.to_bytes(), "\\xf0\\x9f\\xa6\\x80".as_bytes());
|
||||
/// # Ok::<(), kernel::error::Error>(())
|
||||
/// ```
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
@@ -89,11 +89,11 @@ impl fmt::Debug for BStr {
|
||||
/// // Embedded double quotes are escaped.
|
||||
/// let ascii = b_str!("Hello, \"BStr\"!");
|
||||
/// let s = CString::try_from_fmt(fmt!("{ascii:?}"))?;
|
||||
/// assert_eq!(s.as_bytes(), "\"Hello, \\\"BStr\\\"!\"".as_bytes());
|
||||
/// assert_eq!(s.to_bytes(), "\"Hello, \\\"BStr\\\"!\"".as_bytes());
|
||||
///
|
||||
/// let non_ascii = b_str!("😺");
|
||||
/// let s = CString::try_from_fmt(fmt!("{non_ascii:?}"))?;
|
||||
/// assert_eq!(s.as_bytes(), "\"\\xf0\\x9f\\x98\\xba\"".as_bytes());
|
||||
/// assert_eq!(s.to_bytes(), "\"\\xf0\\x9f\\x98\\xba\"".as_bytes());
|
||||
/// # Ok::<(), kernel::error::Error>(())
|
||||
/// ```
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
@@ -465,15 +465,15 @@ impl fmt::Display for CStr {
|
||||
/// # use kernel::str::CString;
|
||||
/// let penguin = c_str!("🐧");
|
||||
/// let s = CString::try_from_fmt(fmt!("{penguin}"))?;
|
||||
/// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
|
||||
/// assert_eq!(s.to_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
|
||||
///
|
||||
/// let ascii = c_str!("so \"cool\"");
|
||||
/// let s = CString::try_from_fmt(fmt!("{ascii}"))?;
|
||||
/// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes());
|
||||
/// assert_eq!(s.to_bytes_with_nul(), "so \"cool\"\0".as_bytes());
|
||||
/// # Ok::<(), kernel::error::Error>(())
|
||||
/// ```
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for &c in self.as_bytes() {
|
||||
for &c in self.to_bytes() {
|
||||
if (0x20..0x7f).contains(&c) {
|
||||
// Printable character.
|
||||
f.write_char(c as char)?;
|
||||
@@ -874,11 +874,11 @@ impl fmt::Write for Formatter {
|
||||
/// use kernel::{str::CString, prelude::fmt};
|
||||
///
|
||||
/// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20))?;
|
||||
/// assert_eq!(s.as_bytes_with_nul(), "abc1020\0".as_bytes());
|
||||
/// assert_eq!(s.to_bytes_with_nul(), "abc1020\0".as_bytes());
|
||||
///
|
||||
/// let tmp = "testing";
|
||||
/// let s = CString::try_from_fmt(fmt!("{tmp}{}", 123))?;
|
||||
/// assert_eq!(s.as_bytes_with_nul(), "testing123\0".as_bytes());
|
||||
/// assert_eq!(s.to_bytes_with_nul(), "testing123\0".as_bytes());
|
||||
///
|
||||
/// // This fails because it has an embedded `NUL` byte.
|
||||
/// let s = CString::try_from_fmt(fmt!("a\0b{}", 123));
|
||||
@@ -948,7 +948,7 @@ impl<'a> TryFrom<&'a CStr> for CString {
|
||||
fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
|
||||
let mut buf = KVec::new();
|
||||
|
||||
buf.extend_from_slice(cstr.as_bytes_with_nul(), GFP_KERNEL)?;
|
||||
buf.extend_from_slice(cstr.to_bytes_with_nul(), GFP_KERNEL)?;
|
||||
|
||||
// INVARIANT: The `CStr` and `CString` types have the same invariants for
|
||||
// the string data, and we copied it over without changes.
|
||||
|
||||
Reference in New Issue
Block a user