All of lore.kernel.org
 help / color / mirror / Atom feed
From: ojeda@kernel.org
To: "Miguel Ojeda" <ojeda@kernel.org>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	patches@lists.linux.dev
Subject: [PATCH v2 07/28] rust: macros: take string literals in `module!`
Date: Fri,  2 Dec 2022 17:14:38 +0100	[thread overview]
Message-ID: <20221202161502.385525-8-ojeda@kernel.org> (raw)
In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org>

From: Gary Guo <gary@garyguo.net>

Instead of taking binary string literals, take string ones instead,
making it easier for users to define a module, i.e. instead of
calling `module!` like:

    module! {
        ...
        name: b"rust_minimal",
        ...
    }

now it is called as:

    module! {
        ...
        name: "rust_minimal",
        ...
    }

Module names, aliases and license strings are restricted to
ASCII only. However, the author and the description allows UTF-8.

For simplicity (avoid parsing), escape sequences and raw string
literals are not yet handled.

Link: https://github.com/Rust-for-Linux/linux/issues/252
Link: https://lore.kernel.org/lkml/YukvvPOOu8uZl7+n@yadro.com/
Signed-off-by: Gary Guo <gary@garyguo.net>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/macros/helpers.rs       | 24 ++++++++++++++++++------
 rust/macros/lib.rs           | 12 ++++++------
 rust/macros/module.rs        | 10 +++++-----
 samples/rust/rust_minimal.rs |  8 ++++----
 samples/rust/rust_print.rs   |  8 ++++----
 5 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs
index cdc7dc6135d2..cf7ad950dc1e 100644
--- a/rust/macros/helpers.rs
+++ b/rust/macros/helpers.rs
@@ -18,10 +18,16 @@ pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
     }
 }
 
-pub(crate) fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String> {
-    try_literal(it).and_then(|byte_string| {
-        if byte_string.starts_with("b\"") && byte_string.ends_with('\"') {
-            Some(byte_string[2..byte_string.len() - 1].to_string())
+pub(crate) fn try_string(it: &mut token_stream::IntoIter) -> Option<String> {
+    try_literal(it).and_then(|string| {
+        if string.starts_with('\"') && string.ends_with('\"') {
+            let content = &string[1..string.len() - 1];
+            if content.contains('\\') {
+                panic!("Escape sequences in string literals not yet handled");
+            }
+            Some(content.to_string())
+        } else if string.starts_with("r\"") {
+            panic!("Raw string literals are not yet handled");
         } else {
             None
         }
@@ -40,8 +46,14 @@ pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
     }
 }
 
-pub(crate) fn expect_byte_string(it: &mut token_stream::IntoIter) -> String {
-    try_byte_string(it).expect("Expected byte string")
+pub(crate) fn expect_string(it: &mut token_stream::IntoIter) -> String {
+    try_string(it).expect("Expected string")
+}
+
+pub(crate) fn expect_string_ascii(it: &mut token_stream::IntoIter) -> String {
+    let string = try_string(it).expect("Expected string");
+    assert!(string.is_ascii(), "Expected ASCII string");
+    string
 }
 
 pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {
diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
index e40caaf0a656..c1d385e345b9 100644
--- a/rust/macros/lib.rs
+++ b/rust/macros/lib.rs
@@ -25,20 +25,20 @@ use proc_macro::TokenStream;
 ///
 /// module!{
 ///     type: MyModule,
-///     name: b"my_kernel_module",
-///     author: b"Rust for Linux Contributors",
-///     description: b"My very own kernel module!",
-///     license: b"GPL",
+///     name: "my_kernel_module",
+///     author: "Rust for Linux Contributors",
+///     description: "My very own kernel module!",
+///     license: "GPL",
 ///     params: {
 ///        my_i32: i32 {
 ///            default: 42,
 ///            permissions: 0o000,
-///            description: b"Example of i32",
+///            description: "Example of i32",
 ///        },
 ///        writeable_i32: i32 {
 ///            default: 42,
 ///            permissions: 0o644,
-///            description: b"Example of i32",
+///            description: "Example of i32",
 ///        },
 ///    },
 /// }
diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index 186a5b8be23c..a7e363c2b044 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -108,11 +108,11 @@ impl ModuleInfo {
 
             match key.as_str() {
                 "type" => info.type_ = expect_ident(it),
-                "name" => info.name = expect_byte_string(it),
-                "author" => info.author = Some(expect_byte_string(it)),
-                "description" => info.description = Some(expect_byte_string(it)),
-                "license" => info.license = expect_byte_string(it),
-                "alias" => info.alias = Some(expect_byte_string(it)),
+                "name" => info.name = expect_string_ascii(it),
+                "author" => info.author = Some(expect_string(it)),
+                "description" => info.description = Some(expect_string(it)),
+                "license" => info.license = expect_string_ascii(it),
+                "alias" => info.alias = Some(expect_string_ascii(it)),
                 _ => panic!(
                     "Unknown key \"{}\". Valid keys are: {:?}.",
                     key, EXPECTED_KEYS
diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs
index 54ad17685742..dc05f4bbe27e 100644
--- a/samples/rust/rust_minimal.rs
+++ b/samples/rust/rust_minimal.rs
@@ -6,10 +6,10 @@ use kernel::prelude::*;
 
 module! {
     type: RustMinimal,
-    name: b"rust_minimal",
-    author: b"Rust for Linux Contributors",
-    description: b"Rust minimal sample",
-    license: b"GPL",
+    name: "rust_minimal",
+    author: "Rust for Linux Contributors",
+    description: "Rust minimal sample",
+    license: "GPL",
 }
 
 struct RustMinimal {
diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs
index 09f737790f3f..8b39d9cef6d1 100644
--- a/samples/rust/rust_print.rs
+++ b/samples/rust/rust_print.rs
@@ -7,10 +7,10 @@ use kernel::prelude::*;
 
 module! {
     type: RustPrint,
-    name: b"rust_print",
-    author: b"Rust for Linux Contributors",
-    description: b"Rust printing macros sample",
-    license: b"GPL",
+    name: "rust_print",
+    author: "Rust for Linux Contributors",
+    description: "Rust printing macros sample",
+    license: "GPL",
 }
 
 struct RustPrint;
-- 
2.38.1


  parent reply	other threads:[~2022-12-02 16:15 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-02 16:14 [PATCH v2 00/28] Rust core additions ojeda
2022-12-02 16:14 ` [PATCH v2 01/28] rust: prelude: split re-exports into groups ojeda
2022-12-02 16:14 ` [PATCH v2 02/28] rust: print: add more `pr_*!` levels ojeda
2022-12-02 16:14 ` [PATCH v2 03/28] rust: print: add `pr_cont!` macro ojeda
2022-12-02 16:14 ` [PATCH v2 04/28] rust: samples: add `rust_print` example ojeda
2022-12-02 16:14 ` [PATCH v2 05/28] rust: macros: add `concat_idents!` proc macro ojeda
2022-12-04  0:20   ` Gary Guo
2022-12-02 16:14 ` [PATCH v2 06/28] rust: macros: add `#[vtable]` " ojeda
2022-12-06 12:49   ` Finn Behrens
2022-12-06 15:44     ` Miguel Ojeda
2022-12-02 16:14 ` ojeda [this message]
2022-12-02 16:14 ` [PATCH v2 08/28] rust: error: declare errors using macro ojeda
2022-12-02 16:14 ` [PATCH v2 09/28] rust: error: add codes from `errno-base.h` ojeda
2022-12-06 12:52   ` Finn Behrens
2022-12-02 16:14 ` [PATCH v2 10/28] rust: error: add `From` implementations for `Error` ojeda
2022-12-02 16:14 ` [PATCH v2 11/28] rust: prelude: add `error::code::*` constant items ojeda
2022-12-02 16:14 ` [PATCH v2 12/28] rust: alloc: add `RawVec::try_with_capacity_in()` constructor ojeda
2022-12-02 16:14 ` [PATCH v2 13/28] rust: alloc: add `Vec::try_with_capacity{,_in}()` constructors ojeda
2022-12-06 12:55   ` Finn Behrens
2022-12-02 16:14 ` [PATCH v2 14/28] rust: str: add `BStr` type ojeda
2022-12-02 16:14 ` [PATCH v2 15/28] rust: str: add `b_str!` macro ojeda
2022-12-02 16:14 ` [PATCH v2 16/28] rust: str: add `CStr` type ojeda
2022-12-02 16:14 ` [PATCH v2 17/28] rust: str: implement several traits for `CStr` ojeda
2022-12-02 16:14 ` [PATCH v2 18/28] rust: str: add `CStr` unit tests ojeda
2022-12-02 16:14 ` [PATCH v2 19/28] rust: str: add `c_str!` macro ojeda
2022-12-02 16:14 ` [PATCH v2 20/28] rust: str: add `Formatter` type ojeda
2022-12-04 15:41   ` Dr. David Alan Gilbert
2022-12-04 17:26     ` Wedson Almeida Filho
2022-12-04 18:05       ` Dr. David Alan Gilbert
2022-12-02 16:14 ` [PATCH v2 21/28] rust: str: add `CString` type ojeda
2022-12-02 16:14 ` [PATCH v2 22/28] rust: str: add `fmt!` macro ojeda
2022-12-02 16:14 ` [PATCH v2 23/28] rust: std_vendor: add `dbg!` macro based on `std`'s one ojeda
2022-12-02 16:14 ` [PATCH v2 24/28] rust: static_assert: add `static_assert!` macro ojeda
2022-12-02 16:14 ` [PATCH v2 25/28] rust: add `build_error` crate ojeda
2022-12-02 18:31   ` Wei Liu
2022-12-02 16:14 ` [PATCH v2 26/28] rust: build_assert: add `build_{error,assert}!` macros ojeda
2022-12-02 18:32   ` Wei Liu
2022-12-02 16:14 ` [PATCH v2 27/28] rust: types: add `Either` type ojeda
2022-12-02 23:41   ` Josh Triplett
2022-12-04  0:58     ` Miguel Ojeda
2022-12-04 10:31     ` Gary Guo
2022-12-04 17:36       ` Wedson Almeida Filho
2022-12-05 13:53         ` Miguel Ojeda
2022-12-05 23:00         ` Josh Triplett
2022-12-02 16:14 ` [PATCH v2 28/28] rust: types: add `Opaque` type ojeda
2022-12-04  1:05 ` [PATCH v2 00/28] Rust core additions Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221202161502.385525-8-ojeda@kernel.org \
    --to=ojeda@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.