Re: [PATCH] rust: str: add to_ascii_{upper,lower}case() to CString

From: Alice Ryhl
Date: Thu Jan 25 2024 - 04:10:59 EST


On Mon, Jan 22, 2024 at 7:46 PM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
> + /// Converts the whole CString to lowercase.
> + pub fn to_ascii_lowercase(&mut self) {
> + self.buf.make_ascii_lowercase();
> + }
> +
> + /// Converts the whole CString to uppercase.
> + pub fn to_ascii_uppercase(&mut self) {
> + self.buf.make_ascii_uppercase();
> + }
> }

It looks like these methods are defined on `CString`. However, there's
no requirement that you need *ownership* of the c string to change its
contents - you just need mutable access.

I think it would make more sense to introduce an `impl DerefMut for
CString` that returns a `&mut CStr`, and then define these methods on
`CStr` as `&mut self`. That way, you can still call them on `CString`,
but you can also call it on other mutable c strings.

Alice