From patchwork Thu Nov 10 16:41:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18216 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp252120wru; Thu, 10 Nov 2022 08:45:20 -0800 (PST) X-Google-Smtp-Source: AMsMyM7DHDOwfLBQbmjDFuzeQxF6sdTUDZrGo556s/RKG+v5LazWrC1Ez9yfm+f1ePNTXySlM/fw X-Received: by 2002:a05:6402:11cc:b0:462:76cd:1215 with SMTP id j12-20020a05640211cc00b0046276cd1215mr62641242edw.318.1668098720602; Thu, 10 Nov 2022 08:45:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098720; cv=none; d=google.com; s=arc-20160816; b=YPXAL+5lxsb7Vjywn66SGfPEZiJ+vfdgkhBKiUEte5YIFPp4rHdPY0DeSS1HMH72VU dXD64ZLrgCuizdjep3w1BCqQNNatlbNo5WzZBcdK3qFR1G9/0TmC2KJ0rQgFmutdXXSL tKjJnAJPPQslydYftqwvW+c5myCiHV9/yzLrh1w5UkTgpMW4bkH9GujVa7gyNM4uuJZ4 Fg4m7esAPjBEuANPfxle2/IA+IZmR6XLFdwf/0u30k8+OIb5RAq0iUu9loCLEtlWy2fI 9gdPkPCc6PKKmUbc3OE+/+IowUGs6v8xoQU4/7YG9F0xbA+APDT3x7pTD9Ja+npQI1q6 fibA== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i2-20020a056402054200b0045d03c18e5bsi19086edx.560.2022.11.10.08.44.56; Thu, 10 Nov 2022 08:45:20 -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 + 99 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_Roy_Baron?= 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 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?1749128283697197136?= X-GMAIL-MSGID: =?utf-8?q?1749128283697197136?= 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