diff --git a/trait-variant/examples/variant.rs b/trait-variant/examples/variant.rs index 04bcc6a..48c9c33 100644 --- a/trait-variant/examples/variant.rs +++ b/trait-variant/examples/variant.rs @@ -20,12 +20,20 @@ pub trait LocalIntFactory { fn stream(&self) -> impl Iterator; fn call(&self) -> u32; fn another_async(&self, input: Result<(), &str>) -> Self::MyFut<'_>; + async fn default_method(&self) -> u32 { + 1 + } + fn sync_default_method(&self) -> u32 { + 1 + } } #[allow(dead_code)] fn spawn_task(factory: impl IntFactory + 'static) { tokio::spawn(async move { let _int = factory.make(1, "foo").await; + let _default_int = factory.default_method().await; + let _sync_default_int = factory.sync_default_method(); }); } diff --git a/trait-variant/src/variant.rs b/trait-variant/src/variant.rs index f7f0d27..3fb1933 100644 --- a/trait-variant/src/variant.rs +++ b/trait-variant/src/variant.rs @@ -150,12 +150,25 @@ fn transform_item(item: &TraitItem, bounds: &Vec) -> TraitItem { ReturnType::Default => return item.clone(), } }; + + let default_impl = fn_item.default.as_ref().map(|default_impl| { + syn::parse2(quote! { + { + async move { + #default_impl + } + } + }) + .unwrap() + }); + TraitItem::Fn(TraitItemFn { sig: Signature { asyncness: None, output: ReturnType::Type(arrow, Box::new(output)), ..sig.clone() }, + default: default_impl, ..fn_item.clone() }) }