Re: [PATCH v2] rust: str: add conversion from `CStr` to `CString`

From: Gary Guo
Date: Wed May 17 2023 - 14:09:44 EST


On Tue, 16 May 2023 11:12:02 +0000
Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:

> Andreas Hindborg <nmi@xxxxxxxxxxxx> writes:
> > Alice Ryhl <alice@xxxxxxx> writes:
> >> On 5/8/23 13:41, Gary Guo wrote:
> >>> On Wed, 3 May 2023 14:10:16 +0000
> >>> Alice Ryhl <aliceryhl@xxxxxxxxxx> 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 `&str`
> >>>> and `String` types. The `ToOwned` trait 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?
> >>
> >> 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
>
> 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