Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp972473rwb; Thu, 10 Nov 2022 09:32:12 -0800 (PST) X-Google-Smtp-Source: AMsMyM4zuW1VZAci4KkfeVeQ5y9wZA5ewEDkzCAEy/HRDLRAy3egPXRXvwCHnh/mDJ3WQlR8lilt X-Received: by 2002:a17:90b:2783:b0:212:fe4a:c378 with SMTP id pw3-20020a17090b278300b00212fe4ac378mr1593596pjb.82.1668101532506; Thu, 10 Nov 2022 09:32:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668101532; cv=none; d=google.com; s=arc-20160816; b=emeubVuvJs5UDPvUqBUWRC4DllFZETGcNR+ncR0y4+CIzh+q9D1H+DY/it8xEL60BD Pw51j7ClwG/h9fGKuuCN0YmkQYILfy6NZ2om3OkgZ09t44GeSUw84e1/mMlvCmpgs1Mq smdvSL+8K2jmu+7F9yTqakqmLzUdcf/rtzg6PxDPUOXyNJlnii+ubrvX1N8pxXOtsaDy ShY/ROWbdEUpXH8KdlHTEhG50X9qZGwTFUwNt7DADpEVQgmXeR9G/CaFz2X050TkCdlh AqBm9rGQpQdfnObvd64CC5nfJTxCcK+O5FkLk0XfThFVQrYxvMj3x9FwViqzNPiA/5J0 ERJw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l12-20020a170903244c00b001884ba979e7si25147222pls.194.2022.11.10.09.31.59; Thu, 10 Nov 2022 09:32:12 -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 + 92 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=20Roy=20Baron?= 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 Content-Transfer-Encoding: 8bit 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 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::*; -- 2.38.1