Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1267435rwr; Wed, 3 May 2023 12:31:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ79Bmc8uCoUQf1kjm8d86nc7+zSr7ikzxd1KrBPPb9Ha1URG39TWYJvPxBJkrJwsFuSLY6D X-Received: by 2002:a05:6a20:2d09:b0:f1:628:dcf3 with SMTP id g9-20020a056a202d0900b000f10628dcf3mr22854201pzl.27.1683142317836; Wed, 03 May 2023 12:31:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683142317; cv=none; d=google.com; s=arc-20160816; b=WLImqyd5yxct47isQBg7zZ3fwrSixOdisDjbjZKxZgQBgymgvds+AJXD/+Q76kcNSb TGJ7j8eS412StYi6tq5TrSA3UvAJkqpmQqHmMst4D15kdVY5l3w9ufrT11VGiF/ndSLS sL1er3Hig77FRoADhzF4hEADikXzniitElMu1PGMt2gQNHQSP0W6WlB7u6UTmOXNEqX4 CE3IezhVy3hk2aTTkHvi9cdwUhnnT/gmlKDGh8G95jP+/L2kEMASK40AHYfIK2HqJHWz hvunZB6giwGeOwbeBwP++CsG20ji5zOlReV7x7g/AeaqDwvIulUekoxrQG0b+kSIlhPj 3nvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=U0zanob5psot+MN0L3Jo7naT487S45wikpSmD7HcN6I=; b=t5BWe1ltgnBWu3mVdijVzaD0a7K6B0Czz7sv+UrWZVEGLJCaky98DVTjKfmeyW78Bm tx8xd/RlvxAapoItq56/6NNrU8WcZ98rbpMQKRIuRD8eXeEyPrKiHSvIOE25IJ9x2O4A dwdAIaS/dQkDuwb8F1Yv8cgQfNVMkLsc46E5xkIv8f2H0OIT4osy1tHEMQJuDAzVtfnZ VK/H8PTjeWg+ZgNtOCv1EmMpQmSFLkBaCjIj7xZBiHDTGY7ktQaXW/dIFadWmNe1ydJZ 6JVaXAhuiepBtdWhTG1K3X0MhKHtyOcG9OM+mDuCvqak9GhmiyJUgRGoXecgjsgZrEII ziCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Y7pSDCAs; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 11-20020a62190b000000b0063b630df52asi33067332pfz.252.2023.05.03.12.31.46; Wed, 03 May 2023 12:31:57 -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=@gmail.com header.s=20221208 header.b=Y7pSDCAs; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230046AbjECTDT (ORCPT + 99 others); Wed, 3 May 2023 15:03:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229498AbjECTDS (ORCPT ); Wed, 3 May 2023 15:03:18 -0400 Received: from mail-ot1-x332.google.com (mail-ot1-x332.google.com [IPv6:2607:f8b0:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E9C67D8E; Wed, 3 May 2023 12:03:16 -0700 (PDT) Received: by mail-ot1-x332.google.com with SMTP id 46e09a7af769-6a5febde157so2045733a34.3; Wed, 03 May 2023 12:03:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683140596; x=1685732596; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=U0zanob5psot+MN0L3Jo7naT487S45wikpSmD7HcN6I=; b=Y7pSDCAs4pgUkzQE6IaengKTj1KGR+dM7m5U3wjgt7c/cAzupQ8ZjRa/epStopoCba Wyq07v5dOlnTpxO7t5xG29NSHxsXk2PcsRwDTFlq6fLPQBjmf4pPov6zNik3HWm3WHGN GhAi44H8g1tTcl2btjSqtOPOoCgQLsteMFrkzQqXZZ3MPOqpBcTXZ7Ima21AVABQusnH 65dIh2UplkfUHuEMOTqqTtTzq2GFZUgwLfQsjiz7/6p5qq4mprddOXnvEWVPpIB5PIWg kDNXb/T4+KqC1xLF/PMMpWiqvNhSzdm0DQZJNsakJrvOwxvzyIVu89RgUIaYh4/mY5+O rchA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683140596; x=1685732596; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=U0zanob5psot+MN0L3Jo7naT487S45wikpSmD7HcN6I=; b=NbtHjyKkszgVfP3gLdDdNOAPAnG0xnTBUKgwXC0RMPCJjrmuymRxFjexnW/37CUxlb HxYEeHr5tQvbZTWQIUyr4abWvDBXucNwKXSrz4oxN+NJxrgoWASkxBGiyuuosWe35OeH EQyBuEm+OfBWR6Ac9Lvwqzc/+1T/dOYCb2auPQsLOKani8cgCKf23EeI1VKEhvdeIwtn p66jqIr3d3oyrRKIyws88OJvcxyKzWn4J/aZNrkw5abp2r6XjSvjNcWmudT8fpmkCiwN /EiMMRhw9f7XZPcBvsHYen8Fn4rmHvf+/UAn3BP93ak2P3FdD5SIe6vDny/DtOszBMwJ Cq3A== X-Gm-Message-State: AC+VfDwvWlRi95obIOLCCPTuIDAQxyJdQIYJaOECQGoZQbRNxqUnWzJ8 AbyKbsruCra1tLJYS3WpAs8= X-Received: by 2002:a05:6870:a704:b0:191:d6cd:6f5b with SMTP id g4-20020a056870a70400b00191d6cd6f5bmr5786911oam.22.1683140595817; Wed, 03 May 2023 12:03:15 -0700 (PDT) Received: from [192.168.54.90] (static.220.238.itcsa.net. [190.15.220.238]) by smtp.gmail.com with ESMTPSA id dv5-20020a056870d88500b0018459cc0f52sm897161oab.24.2023.05.03.12.03.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 May 2023 12:03:15 -0700 (PDT) Message-ID: <0aef40dd-a437-58e1-5fa5-b44412a2a89f@gmail.com> Date: Wed, 3 May 2023 16:01:00 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.1 Subject: Re: [PATCH v2] rust: str: add conversion from `CStr` to `CString` Content-Language: en-US To: Alice Ryhl , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= , Benno Lossin , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev References: <20230503141016.683634-1-aliceryhl@google.com> From: Martin Rodriguez Reboredo In-Reply-To: <20230503141016.683634-1-aliceryhl@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED 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 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 > Signed-off-by: Alice Ryhl > --- > 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 Reviewed-by: Martin Rodriguez Reboredo