Skip to content

Commit 1523590

Browse files
tamirdojeda
authored andcommitted
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: 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 10a7108 commit 1523590

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

rust/kernel/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl fmt::Debug for Error {
188188
Some(name) => f
189189
.debug_tuple(
190190
// SAFETY: These strings are ASCII-only.
191-
unsafe { core::str::from_utf8_unchecked(name) },
191+
unsafe { core::str::from_utf8_unchecked(name.to_bytes()) },
192192
)
193193
.finish(),
194194
}

rust/kernel/str.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ impl fmt::Display for BStr {
5757
/// # use kernel::{prelude::fmt, b_str, str::{BStr, CString}};
5858
/// let ascii = b_str!("Hello, BStr!");
5959
/// let s = CString::try_from_fmt(fmt!("{ascii}"))?;
60-
/// assert_eq!(s.as_bytes(), "Hello, BStr!".as_bytes());
60+
/// assert_eq!(s.to_bytes(), "Hello, BStr!".as_bytes());
6161
///
6262
/// let non_ascii = b_str!("🦀");
6363
/// let s = CString::try_from_fmt(fmt!("{non_ascii}"))?;
64-
/// assert_eq!(s.as_bytes(), "\\xf0\\x9f\\xa6\\x80".as_bytes());
64+
/// assert_eq!(s.to_bytes(), "\\xf0\\x9f\\xa6\\x80".as_bytes());
6565
/// # Ok::<(), kernel::error::Error>(())
6666
/// ```
6767
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -89,11 +89,11 @@ impl fmt::Debug for BStr {
8989
/// // Embedded double quotes are escaped.
9090
/// let ascii = b_str!("Hello, \"BStr\"!");
9191
/// let s = CString::try_from_fmt(fmt!("{ascii:?}"))?;
92-
/// assert_eq!(s.as_bytes(), "\"Hello, \\\"BStr\\\"!\"".as_bytes());
92+
/// assert_eq!(s.to_bytes(), "\"Hello, \\\"BStr\\\"!\"".as_bytes());
9393
///
9494
/// let non_ascii = b_str!("😺");
9595
/// let s = CString::try_from_fmt(fmt!("{non_ascii:?}"))?;
96-
/// assert_eq!(s.as_bytes(), "\"\\xf0\\x9f\\x98\\xba\"".as_bytes());
96+
/// assert_eq!(s.to_bytes(), "\"\\xf0\\x9f\\x98\\xba\"".as_bytes());
9797
/// # Ok::<(), kernel::error::Error>(())
9898
/// ```
9999
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -465,15 +465,15 @@ impl fmt::Display for CStr {
465465
/// # use kernel::str::CString;
466466
/// let penguin = c_str!("🐧");
467467
/// let s = CString::try_from_fmt(fmt!("{penguin}"))?;
468-
/// assert_eq!(s.as_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
468+
/// assert_eq!(s.to_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
469469
///
470470
/// let ascii = c_str!("so \"cool\"");
471471
/// let s = CString::try_from_fmt(fmt!("{ascii}"))?;
472-
/// assert_eq!(s.as_bytes_with_nul(), "so \"cool\"\0".as_bytes());
472+
/// assert_eq!(s.to_bytes_with_nul(), "so \"cool\"\0".as_bytes());
473473
/// # Ok::<(), kernel::error::Error>(())
474474
/// ```
475475
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
476-
for &c in self.as_bytes() {
476+
for &c in self.to_bytes() {
477477
if (0x20..0x7f).contains(&c) {
478478
// Printable character.
479479
f.write_char(c as char)?;
@@ -874,11 +874,11 @@ impl fmt::Write for Formatter {
874874
/// use kernel::{str::CString, prelude::fmt};
875875
///
876876
/// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20))?;
877-
/// assert_eq!(s.as_bytes_with_nul(), "abc1020\0".as_bytes());
877+
/// assert_eq!(s.to_bytes_with_nul(), "abc1020\0".as_bytes());
878878
///
879879
/// let tmp = "testing";
880880
/// let s = CString::try_from_fmt(fmt!("{tmp}{}", 123))?;
881-
/// assert_eq!(s.as_bytes_with_nul(), "testing123\0".as_bytes());
881+
/// assert_eq!(s.to_bytes_with_nul(), "testing123\0".as_bytes());
882882
///
883883
/// // This fails because it has an embedded `NUL` byte.
884884
/// let s = CString::try_from_fmt(fmt!("a\0b{}", 123));
@@ -948,7 +948,7 @@ impl<'a> TryFrom<&'a CStr> for CString {
948948
fn try_from(cstr: &'a CStr) -> Result<CString, AllocError> {
949949
let mut buf = KVec::new();
950950

951-
buf.extend_from_slice(cstr.as_bytes_with_nul(), GFP_KERNEL)?;
951+
buf.extend_from_slice(cstr.to_bytes_with_nul(), GFP_KERNEL)?;
952952

953953
// INVARIANT: The `CStr` and `CString` types have the same invariants for
954954
// the string data, and we copied it over without changes.

0 commit comments

Comments
 (0)