From 6911d7b1451e6ab37a7f83983f35453ffc04b6ed Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Wed, 20 Dec 2023 22:02:06 -0800 Subject: [PATCH] Add macro docs, move details there --- Cargo.lock | 2 +- README.md | 7 ++++--- trait-variant/src/lib.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0bffee7..8a231a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,7 +146,7 @@ dependencies = [ [[package]] name = "trait-variant" -version = "0.0.0" +version = "0.0.1" dependencies = [ "proc-macro2", "quote", diff --git a/README.md b/README.md index f1ec077..bdd0453 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,14 @@ trait LocalIntFactory { async fn make(&self) -> i32; // ..or.. fn stream(&self) -> impl Iterator; - fn call(&self) -> u32; } ``` -Which creates a new `IntFactory: Send` trait and additionally bounds `IntFactory::make(): Send` and `IntFactory::stream(): Send`. Ordinary methods are not affected. +Which creates a new `IntFactory: Send` trait and additionally bounds `IntFactory::make(): Send` and `IntFactory::stream(): Send`. Implementers of the trait can choose to implement the variant instead of the original trait. -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. +For more details, see the docs for [`trait_variant::make`]. + +[`trait_variant::make`]: https://docs.rs/trait-variant/latest/trait_variant/attr.make.html #### License and usage notes diff --git a/trait-variant/src/lib.rs b/trait-variant/src/lib.rs index 7c03519..d3286d9 100644 --- a/trait-variant/src/lib.rs +++ b/trait-variant/src/lib.rs @@ -10,6 +10,34 @@ mod variant; +/// Creates a specialized version of a base trait that adds bounds to `async +/// fn` and/or `-> impl Trait` return types. +/// +/// ``` +/// #[trait_variant::make(IntFactory: Send)] +/// trait LocalIntFactory { +/// async fn make(&self) -> i32; +/// fn stream(&self) -> impl Iterator; +/// fn call(&self) -> u32; +/// } +/// ``` +/// +/// The above example causes a second trait called `IntFactory` to be created: +/// +/// ``` +/// # use core::future::Future; +/// trait IntFactory: Send { +/// fn make(&self) -> impl Future + Send; +/// fn stream(&self) -> impl Iterator + Send; +/// fn call(&self) -> u32; +/// } +/// ``` +/// +/// Note that ordinary methods such as `call` are not affected. +/// +/// 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. #[proc_macro_attribute] pub fn make( attr: proc_macro::TokenStream,