From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A8B1C4321E for ; Fri, 2 Dec 2022 16:17:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233919AbiLBQRL (ORCPT ); Fri, 2 Dec 2022 11:17:11 -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=20Roy=20Baron?= 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: rust-for-linux@vger.kernel.org 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::*; -- 2.38.1