Skip to content

Commit

Permalink
- Move dynTrait docs to #[proc_macro_attribute]
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarwin committed Nov 18, 2024
1 parent 0e88d49 commit 43aa173
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
41 changes: 0 additions & 41 deletions godot-macros/src/class/dyn_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,47 +205,6 @@ fn parse_associated_function(f: &mut venial::Function) -> DynTraitMethod {
check_if_dispatchable(f, signature)
}

/// Proc-macro attribute to be used with `trait` blocks that allows to use user-defined `GodotClass` as Trait Objects.
///
/// ```no_run
/// # use godot::prelude::*;
///
/// #[dyn_trait(name=MyTraitObjectName, base=RefCounted)]
/// trait GdDynTrait {
/// fn method(&self) -> GString;
/// fn non_dispatchable_method() where Self: Sized;
/// }
///
/// #[derive(GodotClass)]
/// #[class(init, dyn_trait = (GdDynTrait))]
/// struct MyDynStruct {
/// field: i64,
/// base: Base<RefCounted>,
/// }
///
/// impl GdDynTrait for MyDynStruct {
/// fn method(&self) -> GString {
/// return GString::from("I am dynamic!");
/// }
/// fn non_dispatchable_method() {
/// godot_print!("I can't be dispatched!");
/// }
/// }
///
///#[derive(GodotClass)]
/// #[class(init)]
/// struct OtherStruct {
/// base: Base<RefCounted>,
/// }
/// #[godot_api]
/// impl OtherStruct {
/// #[func]
/// fn some_method(&self, other: MyTraitObjectName) {
/// godot_print!("hello {}", other.method());
/// }
/// }
///
/// ```
pub fn attribute_dyn_trait(input_decl: venial::Item) -> ParseResult<TokenStream> {
let venial::Item::Trait(mut decl) = input_decl.clone() else {
bail!(
Expand Down
41 changes: 41 additions & 0 deletions godot-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,47 @@ pub fn godot_api(_meta: TokenStream, input: TokenStream) -> TokenStream {
translate(input, class::attribute_godot_api)
}

/// Proc-macro attribute to be used with `trait` blocks that allows to use user-defined `GodotClass` as Trait Objects.
///
/// ```no_run
/// # use godot::prelude::*;
///
/// #[dyn_trait(name=MyTraitObjectName, base=RefCounted)]
/// trait GdDynTrait {
/// fn method(&self) -> GString;
/// fn non_dispatchable_method() where Self: Sized;
/// }
///
/// #[derive(GodotClass)]
/// #[class(init, dyn_trait = (GdDynTrait))]
/// struct MyDynStruct {
/// field: i64,
/// base: Base<RefCounted>,
/// }
///
/// impl GdDynTrait for MyDynStruct {
/// fn method(&self) -> GString {
/// return GString::from("I am dynamic!");
/// }
/// fn non_dispatchable_method() {
/// godot_print!("I can't be dispatched!");
/// }
/// }
///
///#[derive(GodotClass)]
/// #[class(init)]
/// struct OtherStruct {
/// base: Base<RefCounted>,
/// }
/// #[godot_api]
/// impl OtherStruct {
/// #[func]
/// fn some_method(&self, other: MyTraitObjectName) {
/// godot_print!("hello {}", other.method());
/// }
/// }
///
/// ```
#[proc_macro_attribute]
pub fn dyn_trait(meta: TokenStream, input: TokenStream) -> TokenStream {
translate_meta("dyn_trait", meta, input, class::attribute_dyn_trait)
Expand Down

0 comments on commit 43aa173

Please sign in to comment.