Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp7219253rwr; Tue, 2 May 2023 11:08:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4XBnfeG9v4WtbGbgRHpAm/v0AFDTd1APF7r40GyTD8aqqkeLG6w92f7MiHIcPOiEUj9m2r X-Received: by 2002:a17:90a:fa91:b0:24e:180a:e1e5 with SMTP id cu17-20020a17090afa9100b0024e180ae1e5mr5334394pjb.26.1683050910969; Tue, 02 May 2023 11:08:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683050910; cv=none; d=google.com; s=arc-20160816; b=XDentomRbdWqz6zs47s4sW4MeD0bXhX05DKSFlfwWvK1hIfFVqW/yaUwa7E9mL8AOK HT4n1JNKE7ovT+D5442ZrtIrg99QonQ1Cn+oXINOh9AHUJvLS8D8oKR56Gi3xl28Vm2I ScDtWd5lH9b1Mle9uCmPUZ9/PHl3QKymMlDpphVUDs+A+q9AdsMaXucGrsyvGnopKQX8 AxvARqYbPI+39k4oFv67c9s6/JN6z4ETD1TSpn/U222RcSyshcDb2XylUF+J++gHmglq k2HDoDFRMreuoFBkWJkuD6yFBOEY8oHxZBwOPjYrLYtUer9gG2O7bCbjhzQvOu5qbM6q bseA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=MbKHDVEDDmLAHzgC1P5XW5ikDauK/JRL4lyA13f78FE=; b=MvcvfA8j7nv5FerDnw0wywIm/U9HBqcUBWE2PNwjv/GExUHevj7QxJqwsZ5BsOB9GO kSDjlELcfuqYGyvZmbAQegqL5FhKpybOqT0EmEKOkdkr0mLsr8/tSh4iIRG5cKoXeUd1 MNCWr18Ig7TPXDIrY/X/P7XtWC/bYaTGIb+16exsWEpGAJZmZj/V7w5PIb7Zy0PtJiq9 cieHI/XoSzd811/Z7abf6p4nVLcZSL93XEe2GUjtSrIdAO+8tLKCK8VeN1pOINPKhp+4 U7yZRd5P0A/iKalJ1ZV6Kee29tugVE8S86VRQvqj2SG1HZtcIO+S2L0sanC59f0JDoAN PhuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=adnfOjUS; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a8-20020a17090a688800b002465d98f18csi34147421pjd.170.2023.05.02.11.08.12; Tue, 02 May 2023 11:08:30 -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=@proton.me header.s=protonmail header.b=adnfOjUS; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232402AbjEBSDT (ORCPT + 99 others); Tue, 2 May 2023 14:03:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229584AbjEBSDS (ORCPT ); Tue, 2 May 2023 14:03:18 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAFDBE50 for ; Tue, 2 May 2023 11:03:13 -0700 (PDT) Date: Tue, 02 May 2023 18:02:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1683050591; x=1683309791; bh=MbKHDVEDDmLAHzgC1P5XW5ikDauK/JRL4lyA13f78FE=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=adnfOjUSCLRyGxgYEBOrKWA//Ipj4Xf2LiwvJIjRkxD5PwhqDx6Vw2RTDEB+G4IMh en6JN1Y1TkhrGMRwVsrTdWk1jBeaCnQ2SiYYdf0MbqnW7pgBxFA1H2xiuKwEiWkUsh V3zbgWqcwjFAAeUXlj2sVFpSY7XuuSkmPIVf2sq5F/U8gklCatvyN9O0N8G+lWRHpm DyzTBw7402FmDgScrf+sWoug1Oyvv42RD11p58CwIAr8PJyBDD7GhnCJBhB1PzrC9J 0Kn2OwiCqNXvbwJ4D6twx6/nO5yWoLyMr007PyZE6OojpQ99+fxIRxdudMWLBFSWoX KnUHdL9Y02B6g== To: Wedson Almeida Filho From: Benno Lossin Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH] rust: str: add conversion from `CStr` to `CString` Message-ID: In-Reply-To: References: <20230502125306.358283-1-aliceryhl@google.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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: X-Mailing-List: linux-kernel@vger.kernel.org On 02.05.23 18:59, Wedson Almeida Filho wrote: > On Tue, 2 May 2023 at 09:53, 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 file mirrors the standard library API for the `&str` and >> `String` types. The `ToOwned` trait is not implemented because it >> assumes that allocations are infallible. >> >> Signed-off-by: Alice Ryhl >> --- >> rust/kernel/str.rs | 21 +++++++++++++++++++++ >> 1 file changed, 21 insertions(+) >> >> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs >> index b771310fa4a4..54935ff3a610 100644 >> --- a/rust/kernel/str.rs >> +++ b/rust/kernel/str.rs >> @@ -2,6 +2,7 @@ >> >> //! String representations. >> >> +use alloc::collections::TryReserveError; >> 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 a= nd >> + /// copying over the string data. >> + pub fn to_cstring(&self) -> Result { >> + CString::try_from(self) >> + } >> } >> >> impl fmt::Display for CStr { >> @@ -584,6 +591,20 @@ impl Deref for CString { >> } >> } >> >> +impl<'a> TryFrom<&'a CStr> for CString { >> + type Error =3D TryReserveError; >=20 > Wouldn't `AllocError` make more sense? Or even Error (with ENOMEM value). >=20 > `TryReserveError` is documented as "The error type for try_reserve > methods." -- that fact the we use a `Vec` is an implementation detail, > I feel it's better not to leak this fact through the public API. I agree, it should be `AllocError`. There is a `From for Error` with `ENOMEM` as the value, so `AllocError` is the most compatible, since i= t simply converts to `Error` via `?`. Technically, `TryReserveError` represents two different kinds of errors: - CapacityOverflow -- triggered when exceeding `isize::MAX` bytes of size - AllocError -- memory allocation failed I think it is fine to coalesce these into `AllocError`, since allocating `isize::MAX` might as well be considered an OOM error. With that fixed: Reviewed-by: Benno Lossin >> + >> + fn try_from(cstr: &'a CStr) -> Result { >> + let len =3D cstr.len_with_nul(); >> + let mut buf =3D Vec::try_with_capacity(len)?; >> + buf.try_extend_from_slice(cstr.as_bytes_with_nul())?; >> + >> + // INVARIANT: The CStr and CString types have the same invarian= ts 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 >> -- >> 2.40.1.495.gc816e09b53d-goog >> --=20 Cheers, Benno