Skip to content

Commit 1aa9963

Browse files
committed
Parse variant
1 parent d71eeb4 commit 1aa9963

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

trait_transformer/examples/variant.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
use trait_transformer::variant;
10+
11+
#[variant(SendIntFactory: Send)]
12+
trait IntFactory {
13+
async fn make(&self) -> i32;
14+
// ..or..
15+
fn stream(&self) -> impl Iterator<Item = i32>;
16+
fn call(&self) -> u32;
17+
}
18+
19+
fn main() {}

trait_transformer/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// except according to those terms.
88

99
mod transformer;
10+
mod variant;
1011

1112
#[proc_macro_attribute]
1213
pub fn trait_transformer(
@@ -15,3 +16,11 @@ pub fn trait_transformer(
1516
) -> proc_macro::TokenStream {
1617
transformer::trait_transformer(attr, item)
1718
}
19+
20+
#[proc_macro_attribute]
21+
pub fn variant(
22+
attr: proc_macro::TokenStream,
23+
item: proc_macro::TokenStream,
24+
) -> proc_macro::TokenStream {
25+
variant::variant(attr, item)
26+
}

trait_transformer/src/variant.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
use proc_macro2::TokenStream;
10+
use quote::quote;
11+
use syn::{
12+
parse::{Parse, ParseStream},
13+
parse_macro_input,
14+
punctuated::Punctuated,
15+
token::Comma,
16+
Ident, ItemTrait, Path, Result, ReturnType, Token, TraitBound, TraitBoundModifier, TraitItem,
17+
Type,
18+
};
19+
20+
struct Attrs {
21+
variant: Variant,
22+
}
23+
24+
impl Parse for Attrs {
25+
fn parse(input: ParseStream) -> Result<Self> {
26+
Ok(Self {
27+
variant: Variant::parse(input)?,
28+
})
29+
}
30+
}
31+
32+
struct Variant {
33+
name: Ident,
34+
#[allow(unused)]
35+
colon: Token![:],
36+
supertrait: Path,
37+
}
38+
39+
impl Parse for Variant {
40+
fn parse(input: ParseStream) -> Result<Self> {
41+
Ok(Self {
42+
name: input.parse()?,
43+
colon: input.parse()?,
44+
supertrait: input.parse()?,
45+
})
46+
}
47+
}
48+
49+
pub fn variant(
50+
attr: proc_macro::TokenStream,
51+
item: proc_macro::TokenStream,
52+
) -> proc_macro::TokenStream {
53+
let attrs = parse_macro_input!(attr as Attrs);
54+
let item = parse_macro_input!(item as ItemTrait);
55+
56+
quote! {}.into()
57+
}

trait_transformer_test/src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
#![feature(async_fn_in_trait, return_position_impl_trait_in_trait, return_type_notation)]
9+
#![feature(
10+
async_fn_in_trait,
11+
return_position_impl_trait_in_trait,
12+
return_type_notation
13+
)]
1014

1115
use trait_transformer::trait_transformer;
1216

@@ -19,9 +23,9 @@ trait IntFactory {
1923
}
2024

2125
fn thing(factory: impl SendIntFactory) {
22-
tokio::task::spawn(|| async {
26+
tokio::task::spawn(async {
2327
factory.make().await;
24-
})
28+
});
2529
}
2630

2731
struct MyFactory;

0 commit comments

Comments
 (0)