Skip to content

Commit ab8a6c7

Browse files
tamirdojeda
authored andcommitted
rust: str: replace kernel::c_str! with C-Strings
C-String literals were added in Rust 1.77. Replace instances of `kernel::c_str!` with C-String literals where possible. Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Link: https://patch.msgid.link/20251113-core-cstr-cstrings-v3-3-411b34002774@gmail.com [ Removed unused `c_str` import in doctest. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 305b795 commit ab8a6c7

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

rust/kernel/str.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,14 @@ impl fmt::Display for CStr {
277277
/// Formats printable ASCII characters, escaping the rest.
278278
///
279279
/// ```
280-
/// # use kernel::c_str;
281280
/// # use kernel::prelude::fmt;
282281
/// # use kernel::str::CStr;
283282
/// # use kernel::str::CString;
284-
/// let penguin = c_str!("🐧");
283+
/// let penguin = c"🐧";
285284
/// let s = CString::try_from_fmt(fmt!("{penguin}"))?;
286285
/// assert_eq!(s.to_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes());
287286
///
288-
/// let ascii = c_str!("so \"cool\"");
287+
/// let ascii = c"so \"cool\"";
289288
/// let s = CString::try_from_fmt(fmt!("{ascii}"))?;
290289
/// assert_eq!(s.to_bytes_with_nul(), "so \"cool\"\0".as_bytes());
291290
/// # Ok::<(), kernel::error::Error>(())
@@ -724,43 +723,43 @@ unsafe fn kstrtobool_raw(string: *const u8) -> Result<bool> {
724723
/// # Examples
725724
///
726725
/// ```
727-
/// # use kernel::{c_str, str::kstrtobool};
726+
/// # use kernel::str::kstrtobool;
728727
///
729728
/// // Lowercase
730-
/// assert_eq!(kstrtobool(c_str!("true")), Ok(true));
731-
/// assert_eq!(kstrtobool(c_str!("tr")), Ok(true));
732-
/// assert_eq!(kstrtobool(c_str!("t")), Ok(true));
733-
/// assert_eq!(kstrtobool(c_str!("twrong")), Ok(true));
734-
/// assert_eq!(kstrtobool(c_str!("false")), Ok(false));
735-
/// assert_eq!(kstrtobool(c_str!("f")), Ok(false));
736-
/// assert_eq!(kstrtobool(c_str!("yes")), Ok(true));
737-
/// assert_eq!(kstrtobool(c_str!("no")), Ok(false));
738-
/// assert_eq!(kstrtobool(c_str!("on")), Ok(true));
739-
/// assert_eq!(kstrtobool(c_str!("off")), Ok(false));
729+
/// assert_eq!(kstrtobool(c"true"), Ok(true));
730+
/// assert_eq!(kstrtobool(c"tr"), Ok(true));
731+
/// assert_eq!(kstrtobool(c"t"), Ok(true));
732+
/// assert_eq!(kstrtobool(c"twrong"), Ok(true));
733+
/// assert_eq!(kstrtobool(c"false"), Ok(false));
734+
/// assert_eq!(kstrtobool(c"f"), Ok(false));
735+
/// assert_eq!(kstrtobool(c"yes"), Ok(true));
736+
/// assert_eq!(kstrtobool(c"no"), Ok(false));
737+
/// assert_eq!(kstrtobool(c"on"), Ok(true));
738+
/// assert_eq!(kstrtobool(c"off"), Ok(false));
740739
///
741740
/// // Camel case
742-
/// assert_eq!(kstrtobool(c_str!("True")), Ok(true));
743-
/// assert_eq!(kstrtobool(c_str!("False")), Ok(false));
744-
/// assert_eq!(kstrtobool(c_str!("Yes")), Ok(true));
745-
/// assert_eq!(kstrtobool(c_str!("No")), Ok(false));
746-
/// assert_eq!(kstrtobool(c_str!("On")), Ok(true));
747-
/// assert_eq!(kstrtobool(c_str!("Off")), Ok(false));
741+
/// assert_eq!(kstrtobool(c"True"), Ok(true));
742+
/// assert_eq!(kstrtobool(c"False"), Ok(false));
743+
/// assert_eq!(kstrtobool(c"Yes"), Ok(true));
744+
/// assert_eq!(kstrtobool(c"No"), Ok(false));
745+
/// assert_eq!(kstrtobool(c"On"), Ok(true));
746+
/// assert_eq!(kstrtobool(c"Off"), Ok(false));
748747
///
749748
/// // All caps
750-
/// assert_eq!(kstrtobool(c_str!("TRUE")), Ok(true));
751-
/// assert_eq!(kstrtobool(c_str!("FALSE")), Ok(false));
752-
/// assert_eq!(kstrtobool(c_str!("YES")), Ok(true));
753-
/// assert_eq!(kstrtobool(c_str!("NO")), Ok(false));
754-
/// assert_eq!(kstrtobool(c_str!("ON")), Ok(true));
755-
/// assert_eq!(kstrtobool(c_str!("OFF")), Ok(false));
749+
/// assert_eq!(kstrtobool(c"TRUE"), Ok(true));
750+
/// assert_eq!(kstrtobool(c"FALSE"), Ok(false));
751+
/// assert_eq!(kstrtobool(c"YES"), Ok(true));
752+
/// assert_eq!(kstrtobool(c"NO"), Ok(false));
753+
/// assert_eq!(kstrtobool(c"ON"), Ok(true));
754+
/// assert_eq!(kstrtobool(c"OFF"), Ok(false));
756755
///
757756
/// // Numeric
758-
/// assert_eq!(kstrtobool(c_str!("1")), Ok(true));
759-
/// assert_eq!(kstrtobool(c_str!("0")), Ok(false));
757+
/// assert_eq!(kstrtobool(c"1"), Ok(true));
758+
/// assert_eq!(kstrtobool(c"0"), Ok(false));
760759
///
761760
/// // Invalid input
762-
/// assert_eq!(kstrtobool(c_str!("invalid")), Err(EINVAL));
763-
/// assert_eq!(kstrtobool(c_str!("2")), Err(EINVAL));
761+
/// assert_eq!(kstrtobool(c"invalid"), Err(EINVAL));
762+
/// assert_eq!(kstrtobool(c"2"), Err(EINVAL));
764763
/// ```
765764
pub fn kstrtobool(string: &CStr) -> Result<bool> {
766765
// SAFETY:

0 commit comments

Comments
 (0)