Skip to content

Commit

Permalink
Switched macro export handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dewert99 committed Jun 20, 2024
1 parent 54b8247 commit 750ad15
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 89 deletions.
4 changes: 0 additions & 4 deletions ambassador/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 43 additions & 13 deletions ambassador/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
//!
//! #[delegatable_trait]
//! pub trait Map {
//! type K;
//! type V;
//! }
//!
Expand All @@ -45,12 +44,10 @@
//! }
//!
//! impl<K, V, S> Map for HashMap<K, V, S> {
//! type K = K;
//! type V = V;
//! }
//!
//! impl<K, V> Map for BTreeMap<K, V> {
//! type K = K;
//! type V = V;
//! }
//!
Expand All @@ -66,38 +63,71 @@
//!
//! #[derive(Delegate)]
//! #[delegate(Map)]
//! #[delegate(Get<X>, generics = "X", where = "X: ?Sized, B: Map<K=A::K, V=A::V>")] //auto where clause misses required on super trait
//! #[delegate(Get<X>, generics = "X", where = "X: ?Sized, B: Map<V=A::V>")] //auto where clause misses required on super trait
//! pub enum Either<A, B> {
//! Left(A),
//! Right(B),
//! }
//!
//! #[delegate_to_remote_methods]
//! #[delegate(Map, target_ref = "deref")]
//! impl<M: ?Sized + Map> Map for Box<M> {
//! #[delegate(Get<X>, target_ref = "deref", generics = "X", where = "X: ?Sized")]
//! impl<M: ?Sized> Box<M> {
//! fn deref(&self) -> &M;
//! }
//!
//! fn takes_map(_m: &impl Map<K = &'static str, V = u32>) { }
//!
//! pub fn main() {
//! let my_map: Either<HashMap<&'static str, u32>, BTreeMap<&'static str, u32>> = Either::Left([("a", 1)].into());
//! let x: HashMap<&'static str, u32> = [("a", 1)].into();
//! let my_map: Either<Box<dyn Get<str, V = u32>>, BTreeMap<&'static str, u32>> = Either::Left(Box::new(x));
//! assert_eq!(my_map.get("a"), Some(&1));
//! }
//! ```
//!
//! let boxed: Box<dyn Map<K = &'static str, V = u32>> = Box::new(my_map);
//! takes_map(&boxed);
//! # Cross module uses
//! Modules using delegateable traits should add `use <MODULE PATH>::ambassador_impl_<TRAIT NAME>;`
//! where `<MODULE PATH>` 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>(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;
Expand Down
28 changes: 8 additions & 20 deletions ambassador/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn compile_error_or_none(message: &str, return_cmp_err: bool) -> Option<TokenStr
pub fn build_register_trait(original_item: &ItemTrait) -> 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();
Expand Down Expand Up @@ -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))}
};
Expand Down Expand Up @@ -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 {
Expand Down
48 changes: 0 additions & 48 deletions ambassador/tests/run-pass/backwards_compatible.rs

This file was deleted.

5 changes: 2 additions & 3 deletions ambassador/tests/run-pass/derive_and_trait_in_modules.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate ambassador;

#[macro_use]
mod baz {
use ambassador::delegatable_trait;

Expand All @@ -11,7 +10,7 @@ mod baz {
}

mod bar {
use super::Shout;
use super::{ambassador_impl_Shout, Shout};
use ambassador::Delegate;

pub struct Cat;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ambassador/tests/run-pass/derive_in_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub trait Shout {
}

mod bar {
use super::Shout;
use super::{ambassador_impl_Shout, Shout};
use ambassador::Delegate;

pub struct Cat;
Expand Down

0 comments on commit 750ad15

Please sign in to comment.