From patchwork Thu Nov 10 16:41:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18220 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp252416wru; Thu, 10 Nov 2022 08:45:53 -0800 (PST) X-Google-Smtp-Source: AA0mqf537ln7+ogxK3U1+/xjm0hP+kTG9oUDLUdHMiVozncMporgrXEMg2dLPd4JKX75Y4Mcuutf X-Received: by 2002:a17:906:c839:b0:7ae:b2e3:ce15 with SMTP id dd25-20020a170906c83900b007aeb2e3ce15mr631298ejb.136.1668098753400; Thu, 10 Nov 2022 08:45:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098753; cv=none; d=google.com; s=arc-20160816; b=wcX6a74mO8Qm7/v40kG+sgevl5qCf+llbNNW4AJLfnn0aTIOGqqk3FW6nq7ij3v8zA xyjM4NMPsm/HchyfS+rnM6Nzuy9h2rNiQnm2hpYwbF9PCIBkafkoArSFIGJND1v70PW9 Q15rbBsNgxk/Dk4Pec7PhfmIvrUVS6TU+l8dfTRuB4PYf3e5UDlUV86/1Y9K4UMyae9B ZAmHqNiS/iatJp+1OIsdCeTEqeAEOBd3hyFzjS8oZmhPd59fTh89xJbBlGn2W022iY0C DOF/pnCwq4x8/fqMn7fsrrufxiu/okCHeiBUJhHA8QdrzK9bNeYvkJJIOtcniRMTHheL j1bw== 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=vEXAk2WWdrEr5V41nwmE+vwwllebZCHywQ6twWLMjCE=; b=Y6u0TAgBJQbC67O3LE19vGX+JJtEkq9XTK//wPoK9Af55EvpXsImTkIqf6Z/BjyBHo p8+n98XNuuI+Rg85yX1cf4NdxR/s+YF1jjhxeLnGWH9vO6xq035rbfX82F3h1EE/t+6U fSkiClwJf3qJHdKHtvOP6nGbszicr6HY4cv4JhuYPpNV1yu6t+8An4+C7rhXPbE3qVlo Fhl0vp9tDiIkb3TBq2bbjy/1RCHvzm56C6ggyMcYVwkWAUo8PaXs+YV8nmpXFFEGmYnM +5HeFEsUVKEcdzuE6uAw+LKrSS3kH34gyqW6wMeSBSTMdcY65kqOlAg7Aj/0zmAxNblu kX5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CadoW2qB; 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 x7-20020a05640226c700b004520e27e5b9si18712edd.626.2022.11.10.08.45.29; Thu, 10 Nov 2022 08:45:53 -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=CadoW2qB; 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 S232324AbiKJQo3 (ORCPT + 99 others); Thu, 10 Nov 2022 11:44:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232308AbiKJQnz (ORCPT ); Thu, 10 Nov 2022 11:43:55 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AF8847312; Thu, 10 Nov 2022 08:43:17 -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 8B44DB82251; Thu, 10 Nov 2022 16:43:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 362CAC433D6; Thu, 10 Nov 2022 16:43:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098595; bh=bTwV3nkr6jPDZcA7hO2dJXGMLx9sZQV043G74OSH4H0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CadoW2qBa6bNQMyp1nnAz/fKRxL9PT6Cjyz4lVonGscW0FXbELPIh151r/bpTZLX0 RNBGpEae5EeDzpOjPC/lFxm/X6VPej6rwjFjK/TaJOCaNEXZ0jFlLbaFPm9LxgnORm uK6QlIAdByyC1uohiB+V3QQ/ch52eA5RRBBq4PHpLLDPVcrDgDVBcwBaod1F2V71v8 b7i8y6F1+iKuiSPcH9IjuBo3kRwpV4TJEVJYuWJ/T9fQNrk4BEQPnomVI4rHV7K/k+ sC16wilA/IOLzQ6c7yZUqcRsX5a1u2QcVF/sjIQuDByyUbd8CgZ8SFCbo5L9Hf53wh CYzN6evp+pGOA== From: Miguel Ojeda 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 v1 19/28] rust: str: add `c_str!` macro Date: Thu, 10 Nov 2022 17:41:31 +0100 Message-Id: <20221110164152.26136-20-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-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?1749128318235681985?= X-GMAIL-MSGID: =?utf-8?q?1749128318235681985?= 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 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 3ed685cb5a3c..3fb73b888dce 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -128,6 +128,18 @@ impl CStr { Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) }) } + /// Creates a [`CStr`] from a `[u8]`, panic if input is not valid. + /// + /// This function is only meant to be used by `c_str!` macro, so + /// crates using `c_str!` macro don't have to enable `const_panic` feature. + #[doc(hidden)] + pub const fn from_bytes_with_nul_unwrap(bytes: &[u8]) -> &Self { + match Self::from_bytes_with_nul(bytes) { + Ok(v) => v, + Err(_) => panic!("string contains interior NUL"), + } + } + /// Creates a [`CStr`] from a `[u8]` without performing any additional /// checks. /// @@ -321,6 +333,26 @@ 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 = $crate::str::CStr::from_bytes_with_nul_unwrap(S.as_bytes()); + C + }}; +} + #[cfg(test)] mod tests { use super::*;