From patchwork Fri Dec 2 16:14:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 28985 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp948868wrr; Fri, 2 Dec 2022 08:20:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf4bdUU4RQIl5mx+hRZYg/pJ9kcZFPCUNeHZrDcHRzEEHA3wX28Np/5PYFuT68Hs9hYCnB/j X-Received: by 2002:a17:902:ce90:b0:186:b46d:da5e with SMTP id f16-20020a170902ce9000b00186b46dda5emr54034633plg.92.1669998036764; Fri, 02 Dec 2022 08:20:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669998036; cv=none; d=google.com; s=arc-20160816; b=lKS/P5EkOnbqCo7ovKKoKZIMILjYmKZMEKiSH7Xt6N66fBH0V02Hk01ew+eMdkw+Zl qPYZViayjigCqP2TaC6RNgO7E/Wkf0Dy1sAjO84OnrtPqyQNtUqe/s4yAWgpmepGVX9i fO2WJWB6h+GcLFh3GSr3JaxT6v3+1NNVxPINfh/OSk7Eh7f3AyIaEdPI+nZHWD7agta+ 6Q3iP5sSaPE/DS/Hcvc/vpAqKz/vnrWe/C22l9ksNIm3enag4c0JEoQ7JEkl23o9gcRj iP15nHXNAJeqJxmBvEqlhYL85ZLpVEEX+XRjZe4kEyLByXLC5YKnCQWDpPPofUWM9W5z optw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=X459mjYQMhHgNGjScvZUf41IYsPRRuS/HAL1lgTwBGc=; b=y+5u98GcGCqRdl++ZpI22Q0iNjdIhvKkJNXngDimnyTP8Q1FggYvVUIWiexIHMcNMA ZHJ1MvOZRR8sQEyAcpGnMd3B/L/6fDbUaaJKEMj4gw1zFH+grmt+eLg+fa+VC7SZkuBp IzPtVoeLfW1vJVqxmkP2+Li7jyZ5PLBOVsiuEmzaE8eEw5Mwiw3ZkV407G4ECOLbARcZ 7H8vcnAmLBSjxib6UhQtqqGeTcDpHO+FR16GOdflT62jbcVXVmUo+C8DDn7iRM/po33g t7yGQmdqTAo/wb6BAABDBV5u8nt2rdukCW/e9HASSr5Bu5xh9NmwkypHNlN+R1hbzVse gTbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qdHfVr+f; 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=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i64-20020a638743000000b00477ba9e69b9si7517826pge.533.2022.12.02.08.20.21; Fri, 02 Dec 2022 08:20:36 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=qdHfVr+f; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233621AbiLBQRd (ORCPT + 99 others); Fri, 2 Dec 2022 11:17:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233774AbiLBQQs (ORCPT ); Fri, 2 Dec 2022 11:16:48 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16659E076D; Fri, 2 Dec 2022 08:16:25 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A93AEB821E6; Fri, 2 Dec 2022 16:16:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74B15C43470; Fri, 2 Dec 2022 16:16:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997782; bh=rwwioSQhWGlbsSveUMx0tqInUdVD+U2ybxu5VIpUbRs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qdHfVr+fC3u1HwOHRGvxny6qMSmv3OfyzAhLmZZ7j/F/TrmrzFJ6s84m7LjHucTKp TS6N0rnkE/xFe1YstIfnTSkEtUqfzhf0Y3qhLKhskGzpmr4TufyQo5Lo15ee+2mOdu w4g14HgGx1uIsrYgu8IOD/bER8fvYm7UKyHup70UaX4ujOnAuh7eqHhtaq8B4J8xsb sqOwnD2etHNPp7uYQbIwvuUoJEtbMYOtvPy0F70Psnxd/aRK0xIwzi4V7rXs+8RDg3 8orTszzXRwKeY2xuyDMhN0jRiGl4LzpZQIaUShQLDRngz93H5W5Fb7QV73cnoUzqVX ARc6ohQ3LMCtw== From: ojeda@kernel.org To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 19/28] rust: str: add `c_str!` macro Date: Fri, 2 Dec 2022 17:14:50 +0100 Message-Id: <20221202161502.385525-20-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751119861458003992?= X-GMAIL-MSGID: =?utf-8?q?1751119861458003992?= From: Gary Guo Add `c_str!`, which is a convenience macro that creates a new `CStr` from a string literal. It is designed to be similar to a `str` in usage, and it is usable in const contexts, for instance: const X: &CStr = c_str!("Example"); Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Signed-off-by: Gary Guo [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda --- rust/kernel/str.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 3ed685cb5a3c..a995db36486f 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -321,6 +321,29 @@ where } } +/// Creates a new [`CStr`] from a string literal. +/// +/// The string literal should not contain any `NUL` bytes. +/// +/// # Examples +/// +/// ``` +/// # use kernel::c_str; +/// # use kernel::str::CStr; +/// const MY_CSTR: &CStr = c_str!("My awesome CStr!"); +/// ``` +#[macro_export] +macro_rules! c_str { + ($str:expr) => {{ + const S: &str = concat!($str, "\0"); + const C: &$crate::str::CStr = match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) { + Ok(v) => v, + Err(_) => panic!("string contains interior NUL"), + }; + C + }}; +} + #[cfg(test)] mod tests { use super::*;