Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp945138rwb; Thu, 10 Nov 2022 09:12:03 -0800 (PST) X-Google-Smtp-Source: AMsMyM7HNoImMHX2pIUn3rKMd0crdYOuWlT8QSrTL/4vaDsMPoLXA2Pzb0xU9PhbU6nuIXVYxdF5 X-Received: by 2002:a17:90a:5910:b0:213:df24:ed80 with SMTP id k16-20020a17090a591000b00213df24ed80mr1503631pji.186.1668100322740; Thu, 10 Nov 2022 09:12:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668100322; cv=none; d=google.com; s=arc-20160816; b=qhUX3P8HDNKe30Vo+4nOkGOqLCFtxowJZ6kaa2MhAm82pSEsSn6CHaGS4SIQAbMVw5 d52vkMyPJFo7m7OhJBrvJexD1zusmv8Ec+Ncv1kr5yN25dlHlevPC78n9tOicTiNvqp1 VPUAzbSYkOtlD76+q1/1UGzxPv7XX2vn/sMWdB/yNAUGeAj+KzjR9+Nni+OiFYOfuz6X LhV7gM7o8H2gs3R1RggAlto84qnWJuq0hzn/s+4d8/4qS28XXx1wl/F1EKPqTK6xCpVC q8iFpKP/+JCUF2uxFr/1HYR35I+GqkhiK8JlK5e+6QyomMiQnwqfQuUBg6E1OghqOKPr GgpQ== 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=IyPWy02EE0dpTq9qkVjDt+wOCLTSekhAmKKv/tjb4m4=; b=Y/ZF8bLKWHPYtveaS6Y0cANfG6yph5TFLQX52D7J9R9+Xy+mPEez1eCN9K7ESlxEMk 9zo730ZeFaUWSA2PvG76qlL0CCFVwjUjLcgAyejkgQcuOzr4GvFjiJf44Ej+a7Gcdev+ NlTHP9Ym6F4jQLPhB0mqish1TjQMcQ7JyTjxi0oWjmkpR3SPehGQ5IYzOoC23z7XupyQ wi8dxJvezNoQNqTa19gX0kmlPheOkR9j6k/7LK/yA8DVzFhDz43a9mzjf2dJ89E/ulcH WqDnDvgGIZUs+j5R/vli6BKVoij07YDzfjgr7hfSGCva6DksEjGvrJu6iNRNxtvql02b b0Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KQn3g9qg; 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 i187-20020a6254c4000000b0056aff71af29si18709732pfb.209.2022.11.10.09.11.44; Thu, 10 Nov 2022 09:12:02 -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=KQn3g9qg; 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 S232483AbiKJQoG (ORCPT + 92 others); Thu, 10 Nov 2022 11:44:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232209AbiKJQn3 (ORCPT ); Thu, 10 Nov 2022 11:43:29 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C184A45EE3; Thu, 10 Nov 2022 08:43:05 -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 6DB33B82251; Thu, 10 Nov 2022 16:43:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C2AAC433B5; Thu, 10 Nov 2022 16:43:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098583; bh=Pqv7nrnSw/i2nkLoHhZe36xor+0aWZ+RAfK3x5lb5qk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KQn3g9qgwmBkJ3Wnm9vxu2L3S8bIXyFNlEQYHy0tMIwN5QBj06PvJhG4rpm0GpuEB GLZQFqXmFCsWDvzpYXSnEZxwbmW+nz0dbZgfnHLjqmA+H2VYUxCELTkC/mq1yAEVIk sY/IcDdQAIBb/RKTHzX5Ii2HbHpIjSBRIARMOPRaC2bm0XUncJ7go5zYE+xD8YA/w8 K5haTcYQCzGir4ppjj/ehtE3OySb7CFv5p5m8QS+W8wpnIs78z1cQnh1grp3YiQqRG eKeTVeJx6ov9zDF/6cRSau+xC4Sbxxl5cO4ULDLG2dzc2WxQVmJCToVNpobyP4ds3v fpLwbnifHBhbA== 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 15/28] rust: str: add `b_str!` macro Date: Thu, 10 Nov 2022 17:41:27 +0100 Message-Id: <20221110164152.26136-16-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 the `b_str!` macro, which creates a new `BStr` from a string literal. It is usable in const contexts, for instance: const X: &BStr = b_str!("Example"); Signed-off-by: Gary Guo [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda --- rust/kernel/str.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 3aa1a0cb9bf8..95eb757c619d 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -9,6 +9,27 @@ use core::fmt; /// `BStr` is simply an alias to `[u8]`, but has a more evident semantical meaning. pub type BStr = [u8]; +/// Creates a new [`BStr`] from a string literal. +/// +/// `b_str!` converts the supplied string literal to byte string, so non-ASCII +/// characters can be included. +/// +/// # Examples +/// +/// ``` +/// # use kernel::b_str; +/// # use kernel::str::BStr; +/// const MY_BSTR: &BStr = b_str!("My awesome BStr!"); +/// ``` +#[macro_export] +macro_rules! b_str { + ($str:literal) => {{ + const S: &'static str = $str; + const C: &'static $crate::str::BStr = S.as_bytes(); + C + }}; +} + /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// It does not fail if callers write past the end of the buffer so that they can calculate the -- 2.38.1