Skip to content

Commit

Permalink
feat!: add #[actuate(path = "..")] attribute to Data macro and use …
Browse files Browse the repository at this point in the history
…fully-qualified path to Actuate by default
  • Loading branch information
matthunz committed Dec 3, 2024
1 parent 9d65ec8 commit b159478
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
24 changes: 19 additions & 5 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@ use proc_macro::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{
parse_macro_input, parse_quote, punctuated::Punctuated, token::Comma, Data, DeriveInput,
GenericParam, ItemTrait, TypeParamBound,
GenericParam, ItemTrait, MetaNameValue, TypeParamBound,
};

#[proc_macro_derive(Data)]
#[proc_macro_derive(Data, attributes(actuate))]
pub fn derive_data(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let ident = &input.ident;

let generics = &input.generics;

let mut cell = None;
if let Some(attr) = input
.attrs
.iter()
.find(|attr| attr.path().is_ident("actuate"))
{
let args: MetaNameValue = attr.parse_args().unwrap();
if args.path.get_ident().unwrap().to_string() == "path" {
let value = args.value.to_token_stream().to_string();
cell = Some(format_ident!("{}", &value[1..value.len() - 1]));
}
}
let actuate = cell.unwrap_or(format_ident!("actuate"));

let generic_params: Punctuated<_, Comma> = generics
.params
.iter()
Expand All @@ -21,7 +35,7 @@ pub fn derive_data(input: TokenStream) -> TokenStream {
let ident = &type_param.ident;

let mut bounds = type_param.bounds.clone();
bounds.push(parse_quote!(Data));
bounds.push(parse_quote!(#actuate::data::Data));

quote! {
#ident: #bounds
Expand Down Expand Up @@ -61,7 +75,7 @@ pub fn derive_data(input: TokenStream) -> TokenStream {
#( #checks )*

#[doc(hidden)]
unsafe impl <#generic_params> Data for #ident <#generic_ty_params> {}
unsafe impl <#generic_params> #actuate::data::Data for #ident <#generic_ty_params> {}
};
gen.into()
}
Expand Down Expand Up @@ -95,7 +109,7 @@ pub fn data(_attrs: TokenStream, input: TokenStream) -> TokenStream {
quote! {
#item

unsafe impl Data for Box<dyn #ident + '_> {}
unsafe impl actuate::data::Data for Box<dyn #ident + '_> {}
}
.into()
}
1 change: 1 addition & 0 deletions src/compose/catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn catch<'a, C: Compose>(
///
/// See [`catch`] for more.
#[derive(Data)]
#[actuate(path = "crate")]
pub struct Catch<'a, C> {
content: C,
f: Box<dyn Fn(Box<dyn StdError>) + 'a>,
Expand Down
1 change: 1 addition & 0 deletions src/compose/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ where
///
/// See [`memo`] for more.
#[derive(Data)]
#[actuate(path = "crate")]
#[must_use = "Composables do nothing unless composed or returned from other composables."]
pub struct Memo<T, C> {
dependency: T,
Expand Down
1 change: 1 addition & 0 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl<C: Compose> Compose for Option<C> {
///
/// This can be handled by a parent composable with [`Catch`].
#[derive(Data)]
#[actuate(path = "crate")]
pub struct Error {
make_error: Box<dyn Fn() -> Box<dyn core::error::Error>>,
}
Expand Down
7 changes: 7 additions & 0 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ mod tests {
};

#[derive(Data)]
#[actuate(path = "crate")]
struct Counter {
x: Rc<Cell<i32>>,
}
Expand All @@ -222,6 +223,7 @@ mod tests {
}

#[derive(Data)]
#[actuate(path = "crate")]
struct NonUpdateCounter {
x: Rc<Cell<i32>>,
}
Expand All @@ -235,6 +237,7 @@ mod tests {
#[test]
fn it_composes() {
#[derive(Data)]
#[actuate(path = "crate")]
struct Wrap {
x: Rc<Cell<i32>>,
}
Expand All @@ -260,6 +263,7 @@ mod tests {
#[test]
fn it_skips_recomposes() {
#[derive(Data)]
#[actuate(path = "crate")]
struct Wrap {
x: Rc<Cell<i32>>,
}
Expand All @@ -285,6 +289,7 @@ mod tests {
#[test]
fn it_composes_any_compose() {
#[derive(Data)]
#[actuate(path = "crate")]
struct Wrap {
x: Rc<Cell<i32>>,
}
Expand All @@ -310,6 +315,7 @@ mod tests {
#[test]
fn it_memoizes_composables() {
#[derive(Data)]
#[actuate(path = "crate")]
struct B {
x: Rc<RefCell<i32>>,
}
Expand All @@ -321,6 +327,7 @@ mod tests {
}

#[derive(Data)]
#[actuate(path = "crate")]
struct A {
x: Rc<RefCell<i32>>,
}
Expand Down
1 change: 1 addition & 0 deletions src/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ where
}

#[derive(Data)]
#[actuate(path = "crate")]
struct CompositionContent<C> {
content: C,
target: Entity,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ use std::collections::HashMap;
/// Prelude of commonly used items.
pub mod prelude {
pub use crate::{
compose::{self, catch, memo, Compose, DynCompose, Error, Memo,dyn_compose},
compose::{self, catch, dyn_compose, memo, Compose, DynCompose, Error, Memo},
data::{data, Data, DataField, FieldWrap, FnField, StaticField},
use_callback, use_context, use_drop, use_local_task, use_memo, use_mut, use_provider,
use_ref, Cow, Map, RefMap, Scope, ScopeState, Signal, SignalMut,
Expand Down

0 comments on commit b159478

Please sign in to comment.