Skip to content

Split trait_transformer back into its own crate #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"trait-variant",
"trait-transformer",
]

resolver = "2"
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

Utilities for working with impl traits in Rust.

## `make_variant`
## `trait_variant`

`make_variant` generates a specialized version of a base trait that uses `async fn` and/or `-> impl Trait`. For example, if you want a `Send`able version of your trait, you'd write:
`trait_variant` generates a specialized version of a base trait that uses `async fn` and/or `-> impl Trait`. For example, if you want a `Send`able version of your trait, you'd write:

```rust
#[trait_variant::make_variant(SendIntFactory: Send)]
#[trait_variant::make(SendIntFactory: Send)]
trait IntFactory {
async fn make(&self) -> i32;
// ..or..
Expand All @@ -20,10 +20,6 @@ Which creates a new `SendIntFactory: IntFactory + Send` trait and additionally b

Implementers of the trait can choose to implement the variant instead of the original trait. The macro creates a blanket impl which ensures that any type which implements the variant also implements the original trait.

## `trait_transformer`

`trait_transformer` does the same thing as `make_variant`, but using experimental nightly-only syntax that depends on the `return_type_notation` feature. It may be used to experiment with new kinds of trait transformations in the future.

#### License and usage notes

Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
Expand Down
30 changes: 30 additions & 0 deletions trait-transformer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.

[package]
name = "trait-transformer"
version = "0.0.0"
description = "Utilities for working with impl traits in Rust"
categories = ["asynchronous", "no-std", "rust-patterns"]
keywords = ["async", "trait", "impl"]
license.workspace = true
repository.workspace = true
edition = "2021"

[lib]
proc-macro = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", features = ["full"] }

[dev-dependencies]
tokio = { version = "1", features = ["rt"] }
3 changes: 3 additions & 0 deletions trait-transformer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## `trait_transformer`

`trait_transformer` does the same thing as `trait_variant`, but using experimental nightly-only syntax that depends on the `return_type_notation` feature. It may be used to experiment with new kinds of trait transformations in the future.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use std::iter;

use trait_variant::trait_transformer;
use trait_transformer::trait_transformer;

#[trait_transformer(SendIntFactory: Send)]
trait IntFactory {
Expand Down
17 changes: 17 additions & 0 deletions trait-transformer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

mod transformer;

#[proc_macro_attribute]
pub fn trait_transformer(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
transformer::trait_transformer(attr, item)
}
File renamed without changes.
13 changes: 9 additions & 4 deletions trait-variant/examples/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

use std::future::Future;

use trait_variant::make_variant;

#[make_variant(SendIntFactory: Send)]
trait IntFactory {
#[trait_variant::make(IntFactory: Send)]
pub trait LocalIntFactory {
const NAME: &'static str;

type MyFut<'a>: Future
Expand All @@ -24,4 +22,11 @@ trait IntFactory {
fn another_async(&self, input: Result<(), &str>) -> Self::MyFut<'_>;
}

#[allow(dead_code)]
fn spawn_task(factory: impl IntFactory + 'static) {
tokio::spawn(async move {
let _int = factory.make(1, "foo").await;
});
}

fn main() {}
13 changes: 2 additions & 11 deletions trait-variant/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,12 @@

#![doc = include_str!("../README.md")]

mod transformer;
mod variant;

#[proc_macro_attribute]
pub fn trait_transformer(
pub fn make(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
transformer::trait_transformer(attr, item)
}

#[proc_macro_attribute]
pub fn make_variant(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
variant::make_variant(attr, item)
variant::make(attr, item)
}
24 changes: 19 additions & 5 deletions trait-variant/src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,36 @@ impl Parse for MakeVariant {
}
}

pub fn make_variant(
pub fn make(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attrs = parse_macro_input!(attr as Attrs);
let item = parse_macro_input!(item as ItemTrait);

let maybe_allow_async_lint = if attrs
.variant
.bounds
.iter()
.any(|b| b.path.segments.last().unwrap().ident.to_string() == "Send")
{
quote! { #[allow(async_fn_in_trait)] }
} else {
quote! {}
};

let variant = mk_variant(&attrs, &item);
let blanket_impl = mk_blanket_impl(&attrs, &item);
let output = quote! {

quote! {
#maybe_allow_async_lint
#item

#variant
#blanket_impl
};

output.into()
#blanket_impl
}
.into()
}

fn mk_variant(attrs: &Attrs, tr: &ItemTrait) -> TokenStream {
Expand Down