[1/3] rust: macros: Make expect_punct() return the Punct directly

Message ID 20230224-rust-macros-v1-1-b39fae46e102@asahilina.net
State New
Headers
Series rust: Miscellaneous macro improvements |

Commit Message

Asahi Lina Feb. 24, 2023, 7:25 a.m. UTC
  This makes it mirror the way expect_ident() works, and means we can more
easily push the result back into the token stream.

Signed-off-by: Asahi Lina <lina@asahilina.net>
---
 rust/macros/concat_idents.rs | 2 +-
 rust/macros/helpers.rs       | 6 +++---
 rust/macros/module.rs        | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
  

Comments

Gary Guo Feb. 25, 2023, 12:29 a.m. UTC | #1
On Fri, 24 Feb 2023 16:25:55 +0900
Asahi Lina <lina@asahilina.net> wrote:

> This makes it mirror the way expect_ident() works, and means we can more
> easily push the result back into the token stream.
> 
> Signed-off-by: Asahi Lina <lina@asahilina.net>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/macros/concat_idents.rs | 2 +-
>  rust/macros/helpers.rs       | 6 +++---
>  rust/macros/module.rs        | 4 ++--
>  3 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/rust/macros/concat_idents.rs b/rust/macros/concat_idents.rs
> index 7e4b450f3a50..6d955d65016e 100644
> --- a/rust/macros/concat_idents.rs
> +++ b/rust/macros/concat_idents.rs
> @@ -15,7 +15,7 @@ fn expect_ident(it: &mut token_stream::IntoIter) -> Ident {
>  pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream {
>      let mut it = ts.into_iter();
>      let a = expect_ident(&mut it);
> -    assert_eq!(expect_punct(&mut it), ',');
> +    assert_eq!(expect_punct(&mut it).as_char(), ',');
>      let b = expect_ident(&mut it);
>      assert!(it.next().is_none(), "only two idents can be concatenated");
>      let res = Ident::new(&format!("{a}{b}"), b.span());
> diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs
> index cf7ad950dc1e..65706ecc007e 100644
> --- a/rust/macros/helpers.rs
> +++ b/rust/macros/helpers.rs
> @@ -1,6 +1,6 @@
>  // SPDX-License-Identifier: GPL-2.0
>  
> -use proc_macro::{token_stream, TokenTree};
> +use proc_macro::{token_stream, Punct, TokenTree};
>  
>  pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
>      if let Some(TokenTree::Ident(ident)) = it.next() {
> @@ -38,9 +38,9 @@ pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
>      try_ident(it).expect("Expected Ident")
>  }
>  
> -pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
> +pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> Punct {
>      if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") {
> -        punct.as_char()
> +        punct
>      } else {
>          panic!("Expected Punct");
>      }
> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index a7e363c2b044..07503b242d2d 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -104,7 +104,7 @@ impl ModuleInfo {
>                  );
>              }
>  
> -            assert_eq!(expect_punct(it), ':');
> +            assert_eq!(expect_punct(it).as_char(), ':');
>  
>              match key.as_str() {
>                  "type" => info.type_ = expect_ident(it),
> @@ -119,7 +119,7 @@ impl ModuleInfo {
>                  ),
>              }
>  
> -            assert_eq!(expect_punct(it), ',');
> +            assert_eq!(expect_punct(it).as_char(), ',');
>  
>              seen_keys.push(key);
>          }
>
  
Finn Behrens Feb. 28, 2023, 9:29 a.m. UTC | #2
On 24 Feb 2023, at 8:25, Asahi Lina wrote:

> This makes it mirror the way expect_ident() works, and means we can more
> easily push the result back into the token stream.
>
> Signed-off-by: Asahi Lina <lina@asahilina.net>

Reviewed-by: Finn Behrens <fin@nyantec.com>

> ---
>  rust/macros/concat_idents.rs | 2 +-
>  rust/macros/helpers.rs       | 6 +++---
>  rust/macros/module.rs        | 4 ++--
>  3 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/rust/macros/concat_idents.rs b/rust/macros/concat_idents.rs
> index 7e4b450f3a50..6d955d65016e 100644
> --- a/rust/macros/concat_idents.rs
> +++ b/rust/macros/concat_idents.rs
> @@ -15,7 +15,7 @@ fn expect_ident(it: &mut token_stream::IntoIter) -> Ident {
>  pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream {
>      let mut it = ts.into_iter();
>      let a = expect_ident(&mut it);
> -    assert_eq!(expect_punct(&mut it), ',');
> +    assert_eq!(expect_punct(&mut it).as_char(), ',');
>      let b = expect_ident(&mut it);
>      assert!(it.next().is_none(), "only two idents can be concatenated");
>      let res = Ident::new(&format!("{a}{b}"), b.span());
> diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs
> index cf7ad950dc1e..65706ecc007e 100644
> --- a/rust/macros/helpers.rs
> +++ b/rust/macros/helpers.rs
> @@ -1,6 +1,6 @@
>  // SPDX-License-Identifier: GPL-2.0
>
> -use proc_macro::{token_stream, TokenTree};
> +use proc_macro::{token_stream, Punct, TokenTree};
>
>  pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
>      if let Some(TokenTree::Ident(ident)) = it.next() {
> @@ -38,9 +38,9 @@ pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
>      try_ident(it).expect("Expected Ident")
>  }
>
> -pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
> +pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> Punct {
>      if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") {
> -        punct.as_char()
> +        punct
>      } else {
>          panic!("Expected Punct");
>      }
> diff --git a/rust/macros/module.rs b/rust/macros/module.rs
> index a7e363c2b044..07503b242d2d 100644
> --- a/rust/macros/module.rs
> +++ b/rust/macros/module.rs
> @@ -104,7 +104,7 @@ impl ModuleInfo {
>                  );
>              }
>
> -            assert_eq!(expect_punct(it), ':');
> +            assert_eq!(expect_punct(it).as_char(), ':');
>
>              match key.as_str() {
>                  "type" => info.type_ = expect_ident(it),
> @@ -119,7 +119,7 @@ impl ModuleInfo {
>                  ),
>              }
>
> -            assert_eq!(expect_punct(it), ',');
> +            assert_eq!(expect_punct(it).as_char(), ',');
>
>              seen_keys.push(key);
>          }
>
> -- 
> 2.35.1
  
Vincenzo Palazzo March 1, 2023, 5:16 p.m. UTC | #3
> This makes it mirror the way expect_ident() works, and means we can more
> easily push the result back into the token stream.
>
> Signed-off-by: Asahi Lina <lina@asahilina.net>
> ---

Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  

Patch

diff --git a/rust/macros/concat_idents.rs b/rust/macros/concat_idents.rs
index 7e4b450f3a50..6d955d65016e 100644
--- a/rust/macros/concat_idents.rs
+++ b/rust/macros/concat_idents.rs
@@ -15,7 +15,7 @@  fn expect_ident(it: &mut token_stream::IntoIter) -> Ident {
 pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream {
     let mut it = ts.into_iter();
     let a = expect_ident(&mut it);
-    assert_eq!(expect_punct(&mut it), ',');
+    assert_eq!(expect_punct(&mut it).as_char(), ',');
     let b = expect_ident(&mut it);
     assert!(it.next().is_none(), "only two idents can be concatenated");
     let res = Ident::new(&format!("{a}{b}"), b.span());
diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs
index cf7ad950dc1e..65706ecc007e 100644
--- a/rust/macros/helpers.rs
+++ b/rust/macros/helpers.rs
@@ -1,6 +1,6 @@ 
 // SPDX-License-Identifier: GPL-2.0
 
-use proc_macro::{token_stream, TokenTree};
+use proc_macro::{token_stream, Punct, TokenTree};
 
 pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
     if let Some(TokenTree::Ident(ident)) = it.next() {
@@ -38,9 +38,9 @@  pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
     try_ident(it).expect("Expected Ident")
 }
 
-pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
+pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> Punct {
     if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") {
-        punct.as_char()
+        punct
     } else {
         panic!("Expected Punct");
     }
diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index a7e363c2b044..07503b242d2d 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -104,7 +104,7 @@  impl ModuleInfo {
                 );
             }
 
-            assert_eq!(expect_punct(it), ':');
+            assert_eq!(expect_punct(it).as_char(), ':');
 
             match key.as_str() {
                 "type" => info.type_ = expect_ident(it),
@@ -119,7 +119,7 @@  impl ModuleInfo {
                 ),
             }
 
-            assert_eq!(expect_punct(it), ',');
+            assert_eq!(expect_punct(it).as_char(), ',');
 
             seen_keys.push(key);
         }