Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2383911rwb; Fri, 2 Dec 2022 09:04:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf40grpED0pP63vBRGjKRWNaIStOeYWqB5VgoKvagTa5F1Qpn4M+/HVmV8nunCIqCbB6F3QH X-Received: by 2002:a05:6402:1857:b0:46b:19aa:cfaa with SMTP id v23-20020a056402185700b0046b19aacfaamr24126306edy.229.1670000679765; Fri, 02 Dec 2022 09:04:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670000679; cv=none; d=google.com; s=arc-20160816; b=LElg/tOK4VIZQ33Qk6Qz3VZ+Ch9y3HOx0jibmq1jneMkYJfyh0sGCX6zHeTlp6sOLK x4M9d+y7xXIDJKRBrXrTEQnNOslcHXFN1i2wLL0ALAohft4wJTTzHd5I3EsqiOVa7d0W GGHMrE7io38y4bUf2xZyXZeD2WoMeZOuy3M9E22RtyaqSKqemeiiiAAf5k7D8u/oaATK AkayxO9NH5xNriLBUvhbtdsTDF2s7PJZQdH84C/Hrp77ghid//6FUlhG2RMwbA2PL4vl +/KlKimvBq+nYhOMNvnfdKgUzMQrJqY7IZLK5vlSM0D1744kKwydWH9AQMhOE4KVxmB8 Fd4w== 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=ydoJUqXazgwnWbaMlFDwhYIhc0ksVvWGxMqlWTgV7OHzr5AeIgXZ+SpuNixxd9jWq5 jZTzZqi18l43JgFjVg+8bYRefFY7fuz2leZ7ZX0/rTcidhcWVuU5UI0Hp9GYyG90yUW2 R2oZG4HYVWURo8Xpqz2gVPif2yjc9EYkRXF+YMYKHAJPSoKDRndsMFkBlETFeDCCf++X ++9qa/GJF7Qg9KOK/62/jZ+Ru3BUS8k1kMgxgfOSUdt0HEvULxjbbXel6zH0Dwq3zmcJ YEtmnY4sRs0LgI+RYyCAiq0xKtSHugnuPpPxTRcFts/EQcqthpK1kYgHg795hcjsjnUy P5bQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fky4ZsDJ; 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 hv14-20020a17090760ce00b007c0d4f96d8esi36113ejc.566.2022.12.02.09.04.17; Fri, 02 Dec 2022 09:04:39 -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=fky4ZsDJ; 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 S233974AbiLBQTA (ORCPT + 82 others); Fri, 2 Dec 2022 11:19:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233911AbiLBQQb (ORCPT ); Fri, 2 Dec 2022 11:16:31 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 717BAE0753; Fri, 2 Dec 2022 08:16:12 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 0A54062333; Fri, 2 Dec 2022 16:16:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73AAAC43470; Fri, 2 Dec 2022 16:16:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997771; bh=Pqv7nrnSw/i2nkLoHhZe36xor+0aWZ+RAfK3x5lb5qk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fky4ZsDJiU3etZhLmzF50RMo1Y6FOs5obZoZgvhHWPXb8facOP7VEFr4Vbq/zrBQR FE2/+nVF2wE3qw+KzUdijKQbNSLTznHojE3xp7hR++wAAwz9vAr4yosMQQKblualkO 0MpAJMnouHyArC23yrUXCRkJoCzS6rUaD8nFEFSUe8gP86xCz/mzyIuKZ2oag3jX0N KlTyl1gSJ4ID2Nh/WND4JAuXxS+P3i6vfLBYEyHz1zU4NTLlHcB0RupT7yExgcgWQq P5SnUwok4tIbVVl/N+RiT853BgCTYF4NNyDUbxYC1hXpyy7VLaxj7/52MaQsuJfWui EICKYz3IfPfew== 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 15/28] rust: str: add `b_str!` macro Date: Fri, 2 Dec 2022 17:14:46 +0100 Message-Id: <20221202161502.385525-16-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 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