Message ID | 20230503141016.683634-1-aliceryhl@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1365938vqo; Wed, 3 May 2023 07:20:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ffxyOzJccWu6TekQaojFQsZiisibhxm8k5cC5LZ9aJ6UbzDysiaO1UAKvKswSfQtdNU7K X-Received: by 2002:a05:6a21:6d84:b0:d6:80a4:f0b6 with SMTP id wl4-20020a056a216d8400b000d680a4f0b6mr27600445pzb.6.1683123610205; Wed, 03 May 2023 07:20:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683123610; cv=none; d=google.com; s=arc-20160816; b=lELPKPo2famDPK6IFXtL0x77e9AJNdC3MnsKk+K3lwyr9/YbrgFqwH9CCWK0JP1KhE 36s0yvWYtF1bN4Brn/AnwSmhYCMVhOIQSmIpfOb3OE32Qsptll1OXdkeGIhDYdrv7Hi1 JHZDT07XK3+X2/FahsHXr0Pw1z5Wi+4ZqDTvhoOmy8LD9bGzXj83vJ2xNlk3Pvq1Q2+F zbBh58cFa5D/yrTmEjXNAnQQSPwjWU/9vxXe1gQLPSkyKzMAFYuAtY2Q8aT2qHFbK/qM tyt3WwS7m6d4pmdHk4C3n2/4Zlj678IKNYn+LSuQgLsKOiZj/2TnHAkn/1Hsb05DLgv1 5yoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=PscYnQxei+RrA3NkIhxRI+jonTcqCawkESGLIumeLz8=; b=viu1LbTfyc53s1rprZQiipKT39qHzRooSumiMc6E9e/6hfHC7uKF/hhpo7F2n6abfw Lq+Tft/FEQeE0xTbDsBBw9sX8qbNVxnwXPDRnX7WFjGucdEMPSANIjhtVJ8f3SsO7EU0 JxVKp9l5gTADlEqzmzjIm1X1FIleYZ2AYNoKmI0hxP5bT288dbDFkef4ZfsxbJvKZY// Y9LhfdcSR+zhru3U8M1dXRbPasUJ7GrGugUMS5X6WHyuPiVFcCNVQzF3mQJbmco0H0kN 8i5ZGbW2efHsD+C6mtm//Ajg3u89/v3A7ugOXjcfEj8Ld7BJnstC7EQYd8NetXFsuVxh ySGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=i6wbV3qz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t127-20020a625f85000000b006434a04f2fdsi1029800pfb.364.2023.05.03.07.19.55; Wed, 03 May 2023 07:20:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=i6wbV3qz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229701AbjECOK7 (ORCPT <rfc822;lhua1029@gmail.com> + 99 others); Wed, 3 May 2023 10:10:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229830AbjECOK4 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 3 May 2023 10:10:56 -0400 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D77359F7 for <linux-kernel@vger.kernel.org>; Wed, 3 May 2023 07:10:54 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-50bcbb5724aso2831634a12.1 for <linux-kernel@vger.kernel.org>; Wed, 03 May 2023 07:10:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683123053; x=1685715053; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=PscYnQxei+RrA3NkIhxRI+jonTcqCawkESGLIumeLz8=; b=i6wbV3qzZv6N/1ImH/nzBnfPh7xlGmGxBUX8RJPeTpz3tkVH4mRLomkibMuRQlKsUu Mqee2m33k6mtzRHeQ2Vuw4bVPr03n/SZALHA+gNsfJNF+zqbWVnKSFnV0QNCBXO/WyRj 71xunrtFeEha39pnex5dhZSGZySmNlv4pz/07QSarAu+3Sa/HY+MzLbu/QtHAsBziPJI bd+P3Lh8NFBr/9hxE+DXRJt0Uc528bq198eDXLISEIAdqdgIhH6TCEqOM2TPfXsWh3nH REsCRFj2VCIum6c5B2luTkXrsQ40hKLLav9bfRMsgA1At64d1Fd+9EbzeLqpoiz7FPm2 igwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683123053; x=1685715053; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=PscYnQxei+RrA3NkIhxRI+jonTcqCawkESGLIumeLz8=; b=gxlDydSjhD0AkMtAlCMDmrfv6QwG1EQLT1mVyIbJiRkMaJbe9CSKfIHkG4jNIbgtt1 QAl3XRvFKnPWV/WccrYPwfAelnJkTlSyerLYlSEFSbe0O8JZJ0z/n8HdkpDKvgJ3fME9 K35yz1ON8Gcz2CUvoksU+WUpByGrTpKAg7VrvkfQc1+lKWxE2uz0DCOOxByT1NTf0CUk RY8ymtVRKZVTM8Nml3jGUvNzcyjy35o90PjF0PBee4MQhk0X4BIGtz7txVbGaRzjRHyx 8BuSHZrlR1ZKdLuJ34BZW3/ydNllWJGhXTQGPD1TwpOz4Zd/MjNwNwCLUYc+x+tRdhmu Z9Dg== X-Gm-Message-State: AC+VfDynBE817ikHKKpjIlgciLMbOls/TNU1NtZdXcaZ94ib48SAq2u/ Wg8gAVg/set8eg5NJkrX97uKcxGXIce4n2M= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a50:d482:0:b0:50b:d28d:a95c with SMTP id s2-20020a50d482000000b0050bd28da95cmr2144272edi.4.1683123052719; Wed, 03 May 2023 07:10:52 -0700 (PDT) Date: Wed, 3 May 2023 14:10:16 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230503141016.683634-1-aliceryhl@google.com> Subject: [PATCH v2] rust: str: add conversion from `CStr` to `CString` From: Alice Ryhl <aliceryhl@google.com> To: Miguel Ojeda <ojeda@kernel.org>, Wedson Almeida Filho <wedsonaf@gmail.com>, Alex Gaynor <alex.gaynor@gmail.com> Cc: Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " <bjorn3_gh@protonmail.com>, Benno Lossin <benno.lossin@proton.me>, Martin Rodriguez Reboredo <yakoyoku@gmail.com>, Alice Ryhl <aliceryhl@google.com>, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764883023043185747?= X-GMAIL-MSGID: =?utf-8?q?1764883023043185747?= |
Series |
[v2] rust: str: add conversion from `CStr` to `CString`
|
|
Commit Message
Alice Ryhl
May 3, 2023, 2:10 p.m. UTC
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. Reviewed-by: Benno Lossin <benno.lossin@proton.me> Signed-off-by: Alice Ryhl <aliceryhl@google.com> --- rust/kernel/str.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) base-commit: ea76e08f4d901a450619831a255e9e0a4c0ed162
Comments
On 5/3/23 11:10, Alice Ryhl 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. > > Reviewed-by: Benno Lossin <benno.lossin@proton.me> > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > --- > rust/kernel/str.rs | 22 ++++++++++++++++++++++ > 1 file changed, 22 insertions(+) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index b771310fa4a4..f3dc5b24ea55 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -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<&'a CStr> for CString { > + type Error = AllocError; > + > + fn try_from(cstr: &'a CStr) -> 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 Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
On Wed, 3 May 2023 14:10:16 +0000 Alice Ryhl <aliceryhl@google.com> 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? Best, Gary > > Reviewed-by: Benno Lossin <benno.lossin@proton.me> > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > --- > rust/kernel/str.rs | 22 ++++++++++++++++++++++ > 1 file changed, 22 insertions(+) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index b771310fa4a4..f3dc5b24ea55 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -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<&'a CStr> for CString { > + type Error = AllocError; > + > + fn try_from(cstr: &'a CStr) -> 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
On 5/8/23 13:41, Gary Guo wrote: > On Wed, 3 May 2023 14:10:16 +0000 > Alice Ryhl <aliceryhl@google.com> 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.
Alice Ryhl <alice@ryhl.io> writes: > On 5/8/23 13:41, Gary Guo wrote: >> On Wed, 3 May 2023 14:10:16 +0000 >> Alice Ryhl <aliceryhl@google.com> 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
Alice Ryhl <aliceryhl@google.com> writes: > 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. > > Reviewed-by: Benno Lossin <benno.lossin@proton.me> > Signed-off-by: Alice Ryhl <aliceryhl@google.com> > --- Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> > rust/kernel/str.rs | 22 ++++++++++++++++++++++ > 1 file changed, 22 insertions(+) > > diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs > index b771310fa4a4..f3dc5b24ea55 100644 > --- a/rust/kernel/str.rs > +++ b/rust/kernel/str.rs > @@ -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<&'a CStr> for CString { > + type Error = AllocError; > + > + fn try_from(cstr: &'a CStr) -> 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
Andreas Hindborg <nmi@metaspace.dk> writes: > Alice Ryhl <alice@ryhl.io> writes: >> On 5/8/23 13:41, Gary Guo wrote: >>> On Wed, 3 May 2023 14:10:16 +0000 >>> Alice Ryhl <aliceryhl@google.com> 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
On Tue, 16 May 2023 11:12:02 +0000 Alice Ryhl <aliceryhl@google.com> wrote: > Andreas Hindborg <nmi@metaspace.dk> writes: > > Alice Ryhl <alice@ryhl.io> writes: > >> On 5/8/23 13:41, Gary Guo wrote: > >>> On Wed, 3 May 2023 14:10:16 +0000 > >>> Alice Ryhl <aliceryhl@google.com> 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
On Wed, May 3, 2023 at 4:10 PM Alice Ryhl <aliceryhl@google.com> 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. > > Reviewed-by: Benno Lossin <benno.lossin@proton.me> > Signed-off-by: Alice Ryhl <aliceryhl@google.com> Applied to `rust-next` -- thanks everyone! Cheers, Miguel
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index b771310fa4a4..f3dc5b24ea55 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -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<&'a CStr> for CString { + type Error = AllocError; + + fn try_from(cstr: &'a CStr) -> 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 {