Skip to content

Commit

Permalink
feat: support default implement method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherlock-Holo committed Feb 4, 2024
1 parent f1e171e commit 36496b1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions trait-variant/examples/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ pub trait LocalIntFactory {
fn stream(&self) -> impl Iterator<Item = i32>;
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();
});
}

Expand Down
13 changes: 13 additions & 0 deletions trait-variant/src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,25 @@ fn transform_item(item: &TraitItem, bounds: &Vec<TypeParamBound>) -> 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()
})
}
Expand Down

0 comments on commit 36496b1

Please sign in to comment.