use cano_macros::task;
#[task]
trait WithDefault {
async fn compute(&self) -> u32 {
self.inner().await * 2
}
async fn inner(&self) -> u32;
}
struct Base;
#[task]
impl WithDefault for Base {
async fn inner(&self) -> u32 {
21
}
}
struct Custom;
#[task]
impl WithDefault for Custom {
async fn inner(&self) -> u32 {
0
}
async fn compute(&self) -> u32 {
100
}
}
#[tokio::test]
async fn test_default_body_used_when_not_overridden() {
let b = Base;
assert_eq!(b.compute().await, 42);
}
#[tokio::test]
async fn test_default_body_overridden() {
let c = Custom;
assert_eq!(c.compute().await, 100);
}
#[tokio::test]
async fn test_default_body_through_dyn() {
let b: Box<dyn WithDefault + Send + Sync> = Box::new(Base);
assert_eq!(b.compute().await, 42);
let c: Box<dyn WithDefault + Send + Sync> = Box::new(Custom);
assert_eq!(c.compute().await, 100);
}