Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Tamir Duberstein and committed by
Miguel Ojeda
ab8a6c7b 305b7957

+29 -30
+29 -30
rust/kernel/str.rs
··· 277 277 /// Formats printable ASCII characters, escaping the rest. 278 278 /// 279 279 /// ``` 280 - /// # use kernel::c_str; 281 280 /// # use kernel::prelude::fmt; 282 281 /// # use kernel::str::CStr; 283 282 /// # use kernel::str::CString; 284 - /// let penguin = c_str!("🐧"); 283 + /// let penguin = c"🐧"; 285 284 /// let s = CString::try_from_fmt(fmt!("{penguin}"))?; 286 285 /// assert_eq!(s.to_bytes_with_nul(), "\\xf0\\x9f\\x90\\xa7\0".as_bytes()); 287 286 /// 288 - /// let ascii = c_str!("so \"cool\""); 287 + /// let ascii = c"so \"cool\""; 289 288 /// let s = CString::try_from_fmt(fmt!("{ascii}"))?; 290 289 /// assert_eq!(s.to_bytes_with_nul(), "so \"cool\"\0".as_bytes()); 291 290 /// # Ok::<(), kernel::error::Error>(()) ··· 723 724 /// # Examples 724 725 /// 725 726 /// ``` 726 - /// # use kernel::{c_str, str::kstrtobool}; 727 + /// # use kernel::str::kstrtobool; 727 728 /// 728 729 /// // Lowercase 729 - /// assert_eq!(kstrtobool(c_str!("true")), Ok(true)); 730 - /// assert_eq!(kstrtobool(c_str!("tr")), Ok(true)); 731 - /// assert_eq!(kstrtobool(c_str!("t")), Ok(true)); 732 - /// assert_eq!(kstrtobool(c_str!("twrong")), Ok(true)); 733 - /// assert_eq!(kstrtobool(c_str!("false")), Ok(false)); 734 - /// assert_eq!(kstrtobool(c_str!("f")), Ok(false)); 735 - /// assert_eq!(kstrtobool(c_str!("yes")), Ok(true)); 736 - /// assert_eq!(kstrtobool(c_str!("no")), Ok(false)); 737 - /// assert_eq!(kstrtobool(c_str!("on")), Ok(true)); 738 - /// assert_eq!(kstrtobool(c_str!("off")), Ok(false)); 730 + /// assert_eq!(kstrtobool(c"true"), Ok(true)); 731 + /// assert_eq!(kstrtobool(c"tr"), Ok(true)); 732 + /// assert_eq!(kstrtobool(c"t"), Ok(true)); 733 + /// assert_eq!(kstrtobool(c"twrong"), Ok(true)); 734 + /// assert_eq!(kstrtobool(c"false"), Ok(false)); 735 + /// assert_eq!(kstrtobool(c"f"), Ok(false)); 736 + /// assert_eq!(kstrtobool(c"yes"), Ok(true)); 737 + /// assert_eq!(kstrtobool(c"no"), Ok(false)); 738 + /// assert_eq!(kstrtobool(c"on"), Ok(true)); 739 + /// assert_eq!(kstrtobool(c"off"), Ok(false)); 739 740 /// 740 741 /// // Camel case 741 - /// assert_eq!(kstrtobool(c_str!("True")), Ok(true)); 742 - /// assert_eq!(kstrtobool(c_str!("False")), Ok(false)); 743 - /// assert_eq!(kstrtobool(c_str!("Yes")), Ok(true)); 744 - /// assert_eq!(kstrtobool(c_str!("No")), Ok(false)); 745 - /// assert_eq!(kstrtobool(c_str!("On")), Ok(true)); 746 - /// assert_eq!(kstrtobool(c_str!("Off")), Ok(false)); 742 + /// assert_eq!(kstrtobool(c"True"), Ok(true)); 743 + /// assert_eq!(kstrtobool(c"False"), Ok(false)); 744 + /// assert_eq!(kstrtobool(c"Yes"), Ok(true)); 745 + /// assert_eq!(kstrtobool(c"No"), Ok(false)); 746 + /// assert_eq!(kstrtobool(c"On"), Ok(true)); 747 + /// assert_eq!(kstrtobool(c"Off"), Ok(false)); 747 748 /// 748 749 /// // All caps 749 - /// assert_eq!(kstrtobool(c_str!("TRUE")), Ok(true)); 750 - /// assert_eq!(kstrtobool(c_str!("FALSE")), Ok(false)); 751 - /// assert_eq!(kstrtobool(c_str!("YES")), Ok(true)); 752 - /// assert_eq!(kstrtobool(c_str!("NO")), Ok(false)); 753 - /// assert_eq!(kstrtobool(c_str!("ON")), Ok(true)); 754 - /// assert_eq!(kstrtobool(c_str!("OFF")), Ok(false)); 750 + /// assert_eq!(kstrtobool(c"TRUE"), Ok(true)); 751 + /// assert_eq!(kstrtobool(c"FALSE"), Ok(false)); 752 + /// assert_eq!(kstrtobool(c"YES"), Ok(true)); 753 + /// assert_eq!(kstrtobool(c"NO"), Ok(false)); 754 + /// assert_eq!(kstrtobool(c"ON"), Ok(true)); 755 + /// assert_eq!(kstrtobool(c"OFF"), Ok(false)); 755 756 /// 756 757 /// // Numeric 757 - /// assert_eq!(kstrtobool(c_str!("1")), Ok(true)); 758 - /// assert_eq!(kstrtobool(c_str!("0")), Ok(false)); 758 + /// assert_eq!(kstrtobool(c"1"), Ok(true)); 759 + /// assert_eq!(kstrtobool(c"0"), Ok(false)); 759 760 /// 760 761 /// // Invalid input 761 - /// assert_eq!(kstrtobool(c_str!("invalid")), Err(EINVAL)); 762 - /// assert_eq!(kstrtobool(c_str!("2")), Err(EINVAL)); 762 + /// assert_eq!(kstrtobool(c"invalid"), Err(EINVAL)); 763 + /// assert_eq!(kstrtobool(c"2"), Err(EINVAL)); 763 764 /// ``` 764 765 pub fn kstrtobool(string: &CStr) -> Result<bool> { 765 766 // SAFETY: