rust: str: add conversion from `CStr` to `CString`
Alice Ryhl
reviewerPATCH v2 1/1 rust: str: add conversion from `CStr` to `CString`
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the &str and String types. The ToOwned trait is not implemented because it assumes that allocations are infallible.
--- rust/kernel/str.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
rust/kernel/str.rs +22 -0
@@ -2,6 +2,7 @@//! String representations. +use alloc::alloc::AllocError ;use alloc::vec::Vec ;use core::fmt::{self,Write };use core::ops::{self,Deref ,Index };
@@ -199,6 +200,12 @@ impl CStr {pub unsafe fn as_str_unchecked (&self) -> &str {unsafe { core::str::from_utf8_unchecked (self.as_bytes ()) } } + +/// Convert this [`CStr`] into a [`CString`] by allocating memory and +/// copying over the string data. +pub fn to_cstring (&self) ->Result <CString ,AllocError > { +CString ::try_from (self) + } }impl fmt::Display for CStr {
@@ -584,6 +591,21 @@ impl Deref for CString { } } +impl <'a>TryFrom <&'aCStr >for CString { +type Error =AllocError ; + +fn try_from (cstr: &'aCStr ) ->Result <CString ,AllocError > { +let mut buf =Vec ::new (); + + buf.try_extend_from_slice (cstr.as_bytes_with_nul ()) + .map_err (|_|AllocError )?; + +// INVARIANT: The `CStr` and `CString` types have the same invariants for +// the string data, and we copied it over without changes. +Ok (CString { buf }) + } +} +/// A convenience alias for [`core::format_args`]. #[macro_export] macro_rules! fmt {
base-commit: ea76e08f4d901a450619831a255e9e0a4c0ed162
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
Quoted 62 lines · Alice Ryhl
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.Reviewed-by Benno Lossin <[email protected]>Signed-off-by Alice Ryhl <[email protected]>--- rust/kernel/str.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)rust/kernel/str.rs +22
-0@@ -2,6 +2,7 @@//! String representations. +use alloc::alloc::AllocError ;use alloc::vec::Vec ;use core::fmt::{self,Write };use core::ops::{self,Deref ,Index };@@ -199,6 +200,12 @@ impl CStr {pub unsafe fn as_str_unchecked (&self) -> &str {unsafe { core::str::from_utf8_unchecked (self.as_bytes ()) } } + +/// Convert this [`CStr`] into a [`CString`] by allocating memory and +/// copying over the string data. +pub fn to_cstring (&self) ->Result <CString ,AllocError > { +CString ::try_from (self) + } }impl fmt::Display for CStr {@@ -584,6 +591,21 @@ impl Deref for CString { } } +impl <'a>TryFrom <&'aCStr >for CString { +type Error =AllocError ; + +fn try_from (cstr: &'aCStr ) ->Result <CString ,AllocError > { +let mut buf =Vec ::new (); + + buf.try_extend_from_slice (cstr.as_bytes_with_nul ()) + .map_err (|_|AllocError )?; + +// INVARIANT: The `CStr` and `CString` types have the same invariants for +// the string data, and we copied it over without changes. +Ok (CString { buf }) + } +} +/// A convenience alias for [`core::format_args`]. #[macro_export] macro_rules! fmt {
Quoted 1 line
base-commit:
ea76e08f4d901a450619831a255e9e0a4c0ed162
Gary Guo
reviewerQuoted 7 lines · On Wed, 3 May 2023 14:10:16 +0000 Alice Ryhl <[email protected]> wrote:
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.
How about add a TryToOwned trait to the kernel crate and implement that trait for CStr instead?
Best,
Gary
Quoted 56 lines
Reviewed-by Benno Lossin <[email protected]>Signed-off-by Alice Ryhl <[email protected]>--- rust/kernel/str.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)rust/kernel/str.rs +22
-0@@ -2,6 +2,7 @@//! String representations. +use alloc::alloc::AllocError ;use alloc::vec::Vec ;use core::fmt::{self,Write };use core::ops::{self,Deref ,Index };@@ -199,6 +200,12 @@ impl CStr {pub unsafe fn as_str_unchecked (&self) -> &str {unsafe { core::str::from_utf8_unchecked (self.as_bytes ()) } } + +/// Convert this [`CStr`] into a [`CString`] by allocating memory and +/// copying over the string data. +pub fn to_cstring (&self) ->Result <CString ,AllocError > { +CString ::try_from (self) + } }impl fmt::Display for CStr {@@ -584,6 +591,21 @@ impl Deref for CString { } } +impl <'a>TryFrom <&'aCStr >for CString { +type Error =AllocError ; + +fn try_from (cstr: &'aCStr ) ->Result <CString ,AllocError > { +let mut buf =Vec ::new (); + + buf.try_extend_from_slice (cstr.as_bytes_with_nul ()) + .map_err (|_|AllocError )?; + +// INVARIANT: The `CStr` and `CString` types have the same invariants for +// the string data, and we copied it over without changes. +Ok (CString { buf }) + } +} +/// A convenience alias for [`core::format_args`]. #[macro_export] macro_rules! fmt {base-commit:
ea76e08f4d901a450619831a255e9e0a4c0ed162
Quoted 7 lines · Gary Guo
Quoted 4 lines · On Wed, 3 May 2023 14:10:16 +0000 Alice Ryhl <[email protected]> wrote:
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
Quoted 6 lines
Quoted 3 lines
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.How about add a
TryToOwnedtrait to the kernel crate and implement that trait forCStrinstead?
Eh, I don't think it's worth it. It doesn't give anything new to the CStr api, and I think it's rather unlikely that someone will actually need to be generic over such a trait any time soon.
Quoted 11 lines · Alice Ryhl
Quoted 10 lines · Gary Guo
Quoted 7 lines · On Wed, 3 May 2023 14:10:16 +0000 Alice Ryhl <[email protected]> wrote:
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.
Quoted 6 lines
Quoted 2 lines
How about add a
TryToOwnedtrait to the kernel crate and implement that trait forCStrinstead?Eh, I don't think it's worth it. It doesn't give anything new to the CStr api, and I think it's rather unlikely that someone will actually need to be generic over such a trait any time soon.
It is just as valid as having From<&str> and ToOwned<&str>. While it does not add anything in terms of function, it carries intention. I think we should consider adding it at some point.
BR Andreas
Alice Ryhl
reviewerQuoted 18 lines · Andreas Hindborg
Quoted 10 lines · Alice Ryhl
Quoted 9 lines · Gary Guo
Quoted 7 lines · On Wed, 3 May 2023 14:10:16 +0000 Alice Ryhl <[email protected]> wrote:
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.Quoted 6 lines
Quoted 2 lines
How about add a
TryToOwnedtrait to the kernel crate and implement that trait forCStrinstead?Eh, I don't think it's worth it. It doesn't give anything new to the CStr api, and I think it's rather unlikely that someone will actually need to be generic over such a trait any time soon.
Quoted 5 lines
It is just as valid as having
From<&str>andToOwned<&str>. While it does not add anything in terms of function, it carries intention. I think we should consider adding it at some point.BR Andreas
Sure, I think its quite reasonable to add new traits, I just don't think it should be part of this patch. Adding new traits makes it a significantly bigger change IMO, and my changes have an actual user down the road.
Alice
Gary Guo
reviewerQuoted 23 lines · On Tue, 16 May 2023 11:12:02 +0000 Alice Ryhl <[email protected]> wrote:
Quoted 22 lines · Andreas Hindborg
Quoted 10 lines · Alice Ryhl
Quoted 9 lines · Gary Guo
Quoted 7 lines · On Wed, 3 May 2023 14:10:16 +0000 Alice Ryhl <[email protected]> wrote:
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.Quoted 6 lines
Quoted 2 lines
How about add a
TryToOwnedtrait to the kernel crate and implement that trait forCStrinstead?Eh, I don't think it's worth it. It doesn't give anything new to the CStr api, and I think it's rather unlikely that someone will actually need to be generic over such a trait any time soon.
It is just as valid as having
From<&str>andToOwned<&str>. While it does not add anything in terms of function, it carries intention. I think we should consider adding it at some point.
Quoted 8 lines
Quoted 1 line
BR Andreas
Sure, I think its quite reasonable to add new traits, I just don't think it should be part of this patch. Adding new traits makes it a significantly bigger change IMO, and my changes have an actual user down the road.
Alice
Personally I think CStr to CString conversion should be implemented on top of [u8] to Vec<u8> conversion. Now we have two conversions that fit in the concept of TryToOwned, so a trait is warranted.
Best,
Gary
Quoted 11 lines · Alice Ryhl
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.Reviewed-by Benno Lossin <[email protected]>Signed-off-by Alice Ryhl <[email protected]>---
Quoted 52 lines
rust/kernel/str.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)rust/kernel/str.rs +22
-0@@ -2,6 +2,7 @@//! String representations. +use alloc::alloc::AllocError ;use alloc::vec::Vec ;use core::fmt::{self,Write };use core::ops::{self,Deref ,Index };@@ -199,6 +200,12 @@ impl CStr {pub unsafe fn as_str_unchecked (&self) -> &str {unsafe { core::str::from_utf8_unchecked (self.as_bytes ()) } } + +/// Convert this [`CStr`] into a [`CString`] by allocating memory and +/// copying over the string data. +pub fn to_cstring (&self) ->Result <CString ,AllocError > { +CString ::try_from (self) + } }impl fmt::Display for CStr {@@ -584,6 +591,21 @@ impl Deref for CString { } } +impl <'a>TryFrom <&'aCStr >for CString { +type Error =AllocError ; + +fn try_from (cstr: &'aCStr ) ->Result <CString ,AllocError > { +let mut buf =Vec ::new (); + + buf.try_extend_from_slice (cstr.as_bytes_with_nul ()) + .map_err (|_|AllocError )?; + +// INVARIANT: The `CStr` and `CString` types have the same invariants for +// the string data, and we copied it over without changes. +Ok (CString { buf }) + } +} +/// A convenience alias for [`core::format_args`]. #[macro_export] macro_rules! fmt {base-commit:
ea76e08f4d901a450619831a255e9e0a4c0ed162
Miguel Ojeda
AppliedQuoted 11 lines · Alice Ryhl
These methods can be used to copy the data in a temporary c string into a separate allocation, so that it can be accessed later even if the original is deallocated.
The API in this change mirrors the standard library API for the
&strandStringtypes. TheToOwnedtrait is not implemented because it assumes that allocations are infallible.Reviewed-by Benno Lossin <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Applied to rust-next -- thanks everyone!
Cheers,
Miguel