Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp866007rwr; Wed, 3 May 2023 07:14:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ79bKqrI2G1uKXTBUjalt4C3hkYitL8tFxh+IRHNaLCwlKMzK1Ir1e3+uMhGkYg6w072k0X X-Received: by 2002:a17:902:e847:b0:1a6:cf4b:4d7d with SMTP id t7-20020a170902e84700b001a6cf4b4d7dmr7539493plg.2.1683123240495; Wed, 03 May 2023 07:14:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683123240; cv=none; d=google.com; s=arc-20160816; b=DU8+IR2lGt7RKYJyzdkwaOYCt64nxRiAAKJl3uY9+AgjwCgnBAHhRhPLIGBR2oEM0f I96nWVrRO52W1wMiVfv3loQWCK3c+9+IJdDBP1FE5m/z7Qyp4YRr3Tuh4rMkpliVwJrn urunUz/gNZrFX1+LVEQa9nSvo3bKeBj/MQotoDaE1o9owu5mark8yB70dXORRPRd4uMc WeIhdIxSofLtipVDhibbcYJaNhLnWSVKKVdveMDvphUgRBLuW/6ULiDKo6VyaS0qLC02 IwVvI+sXG1TwG8U3V4+RDcOWRucNZh2TTAMlX9jkHNRAuA2D/lN4xCLaQ5fOwdlIKfyC +OsQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a10-20020a170902710a00b001a1e4051da5si30876810pll.498.2023.05.03.07.13.46; Wed, 03 May 2023 07:14:00 -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 S230042AbjECOK6 (ORCPT + 99 others); Wed, 3 May 2023 10:10:58 -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 ); 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 ; Wed, 3 May 2023 07:10:54 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-50bcbb5724aso2831634a12.1 for ; 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 To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Martin Rodriguez Reboredo , Alice Ryhl , 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: X-Mailing-List: linux-kernel@vger.kernel.org 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 -- 2.40.1.521.gf1e218fcd8-goog