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

From: Andreas Hindborg
Date: Mon May 15 2023 - 14:19:27 EST



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