From 750ad15484ee115700f369e0233cad5bc4ddd4b9 Mon Sep 17 00:00:00 2001 From: dewert99 Date: Tue, 18 Jun 2024 17:35:49 -0700 Subject: [PATCH] Switched macro export handling --- ambassador/Cargo.toml | 4 -- ambassador/src/lib.rs | 56 ++++++++++++++----- ambassador/src/register.rs | 28 +++------- .../tests/run-pass/backwards_compatible.rs | 48 ---------------- .../run-pass/derive_and_trait_in_modules.rs | 5 +- ambassador/tests/run-pass/derive_in_module.rs | 2 +- 6 files changed, 54 insertions(+), 89 deletions(-) delete mode 100644 ambassador/tests/run-pass/backwards_compatible.rs diff --git a/ambassador/Cargo.toml b/ambassador/Cargo.toml index bddc35b..ba9090c 100644 --- a/ambassador/Cargo.toml +++ b/ambassador/Cargo.toml @@ -12,10 +12,6 @@ readme = "README.md" [lib] proc-macro = true -[features] -default = ["backward_compatible"] -backward_compatible = [] - [dependencies] syn = { version = "1.0.25", features = ["full", "extra-traits"] } quote = "1.0.2" diff --git a/ambassador/src/lib.rs b/ambassador/src/lib.rs index 3012cd5..1f13fef 100644 --- a/ambassador/src/lib.rs +++ b/ambassador/src/lib.rs @@ -35,7 +35,6 @@ //! //! #[delegatable_trait] //! pub trait Map { -//! type K; //! type V; //! } //! @@ -45,12 +44,10 @@ //! } //! //! impl Map for HashMap { -//! type K = K; //! type V = V; //! } //! //! impl Map for BTreeMap { -//! type K = K; //! type V = V; //! } //! @@ -66,7 +63,7 @@ //! //! #[derive(Delegate)] //! #[delegate(Map)] -//! #[delegate(Get, generics = "X", where = "X: ?Sized, B: Map")] //auto where clause misses required on super trait +//! #[delegate(Get, generics = "X", where = "X: ?Sized, B: Map")] //auto where clause misses required on super trait //! pub enum Either { //! Left(A), //! Right(B), @@ -74,30 +71,63 @@ //! //! #[delegate_to_remote_methods] //! #[delegate(Map, target_ref = "deref")] -//! impl Map for Box { +//! #[delegate(Get, target_ref = "deref", generics = "X", where = "X: ?Sized")] +//! impl Box { //! fn deref(&self) -> &M; //! } //! -//! fn takes_map(_m: &impl Map) { } -//! //! pub fn main() { -//! let my_map: Either, BTreeMap<&'static str, u32>> = Either::Left([("a", 1)].into()); +//! let x: HashMap<&'static str, u32> = [("a", 1)].into(); +//! let my_map: Either>, BTreeMap<&'static str, u32>> = Either::Left(Box::new(x)); //! assert_eq!(my_map.get("a"), Some(&1)); +//! } +//! ``` //! -//! let boxed: Box> = Box::new(my_map); -//! takes_map(&boxed); +//! # Cross module uses +//! Modules using delegateable traits should add `use ::ambassador_impl_;` +//! where `` is the path to the module which used either +//! [`macro@delegatable_trait`] or [`macro@delegatable_trait_remote`] +//! ### Example +//! ``` +//! mod m{ +//! pub mod m1 { +//! use ambassador::delegatable_trait; +//! #[delegatable_trait] +//! pub trait Shout { +//! fn shout(&self); +//! } +//! } +//! +//! mod m2 { +//! use ambassador::Delegate; +//! use super::m1::{Shout, ambassador_impl_Shout}; +//! +//! #[derive(Delegate)] +//! #[delegate(Shout)] +//! struct Wrap(X); +//! } //! } +//! //! ``` //! //! # Backwards Compatibility +//! ## 0.3.x -> 0.4.x +//! ### Creating delegateable traits +//! Delagatable trait macros `ambassador_impl_Trait` are no longer exported at the crate root, and +//! are instead exported in the module where [`macro@delegatable_trait`] or +//! [`macro@delegatable_trait_remote`] are used. If these traits are public then upgrading is also +//! a breaking change for users of your library. The "backward_compatible" is also removed. +//! ### Using delegateable traits +//! Switching versions does not affect usages of delegateable traits +//! ## 0.2.x -> 0.3.x //! Since delegateable traits from one crate can be used in anther crate backwards compatibility of switching to 0.3.x depends on the use case -//! ## Self Contained Crate +//! ### Self Contained Crate //! Switching to 0.3.x should just work, //! in this case it safe to disable the "backward_compatible" feature -//! ## Library with public delegatable traits +//! ### Library with public delegatable traits //! Make sure use the "backward_compatible" feature (enabled by default), //! this makes sure users of your library using an older version of ambassador aren't affected by the upgrade -//! ## Users of a library with public delegatable traits +//! ### Users of a library with public delegatable traits //! Try to use the same version of ambassador as the library you're using extern crate core; diff --git a/ambassador/src/register.rs b/ambassador/src/register.rs index 0bb7467..f0da155 100644 --- a/ambassador/src/register.rs +++ b/ambassador/src/register.rs @@ -33,6 +33,7 @@ fn compile_error_or_none(message: &str, return_cmp_err: bool) -> Option TokenStream { let trait_ident = &original_item.ident; let macro_name = macro_name(trait_ident); + let macro_def = quote::format_ident!("_{}", macro_name); let match_name = match_name(trait_ident); let gen_params = &original_item.generics.params; let gen_idents: Vec<_> = gen_params.iter().map(param_to_ident).collect(); @@ -66,10 +67,11 @@ pub fn build_register_trait(original_item: &ItemTrait) -> TokenStream { "target_mut was not specified but was needed", used_recievers.ref_mut, ); - let mut register_trait = quote! { - #[doc = concat!("A macro to be used by [`ambassador::Delegate`] to delegate [`", stringify!(#trait_ident), "`]")] + let vis = &original_item.vis; + quote! { #[macro_export] - macro_rules! #macro_name { + #[doc(hidden)] + macro_rules! #macro_def { (body_struct(<#gen_matcher>, $ty:ty, $field_ident:tt)) => { #macro_name!{body_struct(<#gen_idents_pat>, $ty, ($field_ident), ($field_ident), ($field_ident))} }; @@ -102,24 +104,10 @@ pub fn build_register_trait(original_item: &ItemTrait) -> TokenStream { }; } - - }; - if cfg!(feature = "backward_compatible") { - let enum_name = quote::format_ident!("{}_body_enum", macro_name); - let struct_name = quote::format_ident!("{}_body_single_struct", macro_name); - let legacy_macros = quote! { - #[macro_export] - macro_rules! #struct_name { - ($field_ident:tt) => {#macro_name!{body_struct(<>, (), $field_ident)}}; - } - #[macro_export] - macro_rules! #enum_name { - ($( $variants:path ),+) => {#macro_name!{body_enum(<>, (), (()), ($( $variants),*))}}; - } - }; - register_trait.extend(legacy_macros); + #[doc(inline)] + #[doc = concat!("A macro to be used by [`ambassador::Delegate`] to delegate [`", stringify!(#trait_ident), "`]")] + #vis use #macro_def as #macro_name; } - register_trait } fn param_to_ident(param: &GenericParam) -> &Ident { diff --git a/ambassador/tests/run-pass/backwards_compatible.rs b/ambassador/tests/run-pass/backwards_compatible.rs deleted file mode 100644 index 5c0e0a2..0000000 --- a/ambassador/tests/run-pass/backwards_compatible.rs +++ /dev/null @@ -1,48 +0,0 @@ -extern crate ambassador; - -use ambassador::{delegatable_trait}; - -#[delegatable_trait] -pub trait Shout { - fn shout(&self, input: &str) -> String; -} - -pub struct Cat; - -impl Shout for Cat { - fn shout(&self, input: &str) -> String { - format!("{} - meow!", input) - } -} - -pub struct Dog; - -impl Shout for Dog { - fn shout(&self, input: &str) -> String { - format!("{} - wuff!", input) - } -} - -pub enum Either { - Left(A), - Right(B), -} - -impl < A : Shout, B : Shout > Shout for Either < A, B > -{ ambassador_impl_Shout_body_enum! { Either :: Left, Either :: Right } } - - -pub struct WrappedAnimals { - foo: Cat, - bar: A, -} - -impl < A : Shout > Shout for WrappedAnimals < A > -{ ambassador_impl_Shout_body_single_struct! { bar } } - -pub fn main() { - let foo_animal = Either::Left::(Cat); - println!("{}", foo_animal.shout("BAR")); - let bar_animal = Either::Right::(Dog); - println!("{}", bar_animal.shout("BAR")); -} \ No newline at end of file diff --git a/ambassador/tests/run-pass/derive_and_trait_in_modules.rs b/ambassador/tests/run-pass/derive_and_trait_in_modules.rs index be7df94..2a39403 100644 --- a/ambassador/tests/run-pass/derive_and_trait_in_modules.rs +++ b/ambassador/tests/run-pass/derive_and_trait_in_modules.rs @@ -1,6 +1,5 @@ extern crate ambassador; -#[macro_use] mod baz { use ambassador::delegatable_trait; @@ -11,7 +10,7 @@ mod baz { } mod bar { - use super::Shout; + use super::{ambassador_impl_Shout, Shout}; use ambassador::Delegate; pub struct Cat; @@ -39,7 +38,7 @@ mod bar { } use bar::{Animals, Cat}; -use baz::Shout; +use baz::{ambassador_impl_Shout, Shout}; pub fn main() { let foo_animal = Animals::Cat(Cat); diff --git a/ambassador/tests/run-pass/derive_in_module.rs b/ambassador/tests/run-pass/derive_in_module.rs index acaf47b..10ea215 100644 --- a/ambassador/tests/run-pass/derive_in_module.rs +++ b/ambassador/tests/run-pass/derive_in_module.rs @@ -8,7 +8,7 @@ pub trait Shout { } mod bar { - use super::Shout; + use super::{ambassador_impl_Shout, Shout}; use ambassador::Delegate; pub struct Cat;