Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2458959rwd; Mon, 15 May 2023 12:00:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5tfC8SJOi3AmaP1yo5+GOKZo3tLAl3JQupP5P3hKvkt9bC4wphG9oTORuDY81zZRWdJCXj X-Received: by 2002:a05:6a00:148b:b0:63b:7a55:ae89 with SMTP id v11-20020a056a00148b00b0063b7a55ae89mr48066118pfu.27.1684177228066; Mon, 15 May 2023 12:00:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684177228; cv=none; d=google.com; s=arc-20160816; b=u6ZTxplr/Cts0SC0uDTBZGOBQaaRFDJLSHMxCgcZFzOtzbue3qWfu1Acfqutve9p/5 l5yQ80FUL1QAY6/goQSwKLOzxzosC6lp7iDUtKEaODI1meiqaVAEnZvzYI8g6Zytb1bb nfvNnesYdq63HnKHp4Lf2xxvY+MIJ6iPjb0zghpdfO69ryUF/JU0FdIekzJqPm0kylqX kbNM3UK/OYYLCha920zsKLEj/reilCJF4veRkBrK6d5CGLjIYUOrFafg276oANJDzYv4 nFkd3jclhb861RZSMt/hHHVrBdA4iC/OGbPDsDWoX3St6A2OVQ2tyYSjRdBfSW+T9/3c uU3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:in-reply-to:date:subject :cc:to:from:user-agent:references:dkim-signature; bh=Dl1Ick5+y0z3ChuC16Xif3bBPKbyd3ReO5tblUB4trg=; b=aVFLj6ynwopQ9Z3cV+81MYWnTKDe6AVV55BdrmuwJ+wToHiV+DLBR8UbpmqRTy6oKi A42WlHhfg4tk0WWNIyJBFXBbiPomMwbPrpwiBJAsR9DbJbSoryJ1nJI8EuV7OxvGtI3m 8iOCAeD/I8L26EQ+0K61sPCwSXIlqmrO/SQeD+0R6UcSlj2WOmJ6QOpyykmKCjtUZTPH WbnvL7q7b6ADFvCvDmkysjO77YIgqvdvNPLAfH0YQQJcxmHImOYP3tZ67+5C/05hDRmi YnBRxRwjZPyj0Mes2k+mYd+F72352YE1GJJ8J/4fLWYhZFXsH2l6W/1mqqlgYAGZbXUO lBXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=HoLkpmx8; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h188-20020a636cc5000000b0051f047acf17si9247127pgc.525.2023.05.15.12.00.13; Mon, 15 May 2023 12:00:28 -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=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=HoLkpmx8; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243631AbjEOSji (ORCPT + 99 others); Mon, 15 May 2023 14:39:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243328AbjEOSjh (ORCPT ); Mon, 15 May 2023 14:39:37 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11374268F for ; Mon, 15 May 2023 11:39:36 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-50bcb4a81ceso23192371a12.2 for ; Mon, 15 May 2023 11:39:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1684175974; x=1686767974; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=Dl1Ick5+y0z3ChuC16Xif3bBPKbyd3ReO5tblUB4trg=; b=HoLkpmx8b32mON58aaOI26AqqsUYSvMn9llJihQNKRcCVEIhYzP1Kh6XnoR2/7VVs5 98mdhcMwnnLLjvxd7RUNdzWzXGdNQW03szhF0KTRiLgcuhWy9EPjHAh4aC/zRSY5Uuuv kzEhp3KcES4f7umsQ8lXqPjR2XSsBUcJpZsLbw9b5kbcu6mMuMmB3jNUyDt7njPz3MNz Pw73U77KJx1oLxJJhndezhAN/jmFj87J8uGovtsBF3JP0BlShlyGb2flXtTrO84aBunl LEtG4QWnatvyl8awCNcVewNM/dv8Kqse77t4MQng+0fqnbTEHJeDJa9JVDFHJRBa3Kef UyWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684175974; x=1686767974; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Dl1Ick5+y0z3ChuC16Xif3bBPKbyd3ReO5tblUB4trg=; b=j+1EqVzY9NA8kJCVqqonV4aPrMYC9ZmbwWfbt7fUR9wh50nBMyhhwZ/NeJavk15kYk qcn3FiGSvl3MGabVulS6/9JjduShxs0nLugQySOjCbv82JYU57xfz42UlgowhjsbbvQY hh0lsS0UuaLQNtDAfCTaX+lA3ihSAVwHh4cqx2iagp74RkLSMsEDFHfz2uFMm3ngjbxP UtIwmxOmzXa3oxWfM+Wy/01BNyAx++W3NTRqjg0ZM6iQkeuNNMff5mmiWiVk6LezGeMr PHQVZbg+YyE/i/wycKNDB1CTrx8YI5aMaE9Gnhd7L2yeXJO/PrMx6qYRNuZMOdh+dZ9O RRbg== X-Gm-Message-State: AC+VfDwHdEV9F+PI1m6ntMlv78b6Q6DGwrkHMQpVV2toZ62xUD8GBMMx y/hEwoHNQyyZRcclZ38z7RvGdQ== X-Received: by 2002:a17:907:9308:b0:965:b2c3:9575 with SMTP id bu8-20020a170907930800b00965b2c39575mr29323647ejc.57.1684175974420; Mon, 15 May 2023 11:39:34 -0700 (PDT) Received: from localhost ([79.142.230.34]) by smtp.gmail.com with ESMTPSA id y15-20020a17090614cf00b00965ff04f9f6sm9804211ejc.161.2023.05.15.11.39.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 11:39:33 -0700 (PDT) References: <20230503141016.683634-1-aliceryhl@google.com> User-agent: mu4e 1.10.3; emacs 28.2.50 From: Andreas Hindborg To: Alice Ryhl Cc: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , Benno Lossin , Martin Rodriguez Reboredo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Andreas Hindborg Subject: Re: [PATCH v2] rust: str: add conversion from `CStr` to `CString` Date: Mon, 15 May 2023 20:36:47 +0200 In-reply-to: <20230503141016.683634-1-aliceryhl@google.com> Message-ID: <87bkilo2vu.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham 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 Alice Ryhl 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 > Signed-off-by: Alice Ryhl > --- Reviewed-by: Andreas Hindborg > 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::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 { > + 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