Crates.io | anonymous-trait |
lib.rs | anonymous-trait |
version | 0.1.3 |
source | src |
created_at | 2023-12-21 06:55:29.058412 |
updated_at | 2024-01-10 09:12:15.998613 |
description | Anonymous trait implementation with capturing the environment |
homepage | |
repository | https://github.com/ryo33/anonymous-trait |
max_upload_size | |
id | 1076523 |
size | 42,370 |
Anonymous trait implementation with capturing the environment
trait Cat {
fn meow(&self) -> String;
fn set_name(&mut self, new: String);
async fn meow_async(&self) -> String;
}
#[tokio::main]
async fn main() {
let name = "mock";
#[anonymous_trait::anonymous_trait(let mut cat_mock = "default".into())]
impl Cat for String {
fn meow(&self) -> String {
name.to_string()
}
fn set_name(&mut self, new: String) {
*self = new;
}
async fn meow_async(&self) -> String {
"meow".to_string()
}
}
run(&mut cat_mock).await;
}
async fn run(cat: &mut impl Cat) {
println!("meow: {}, expected: mock", cat.meow());
cat.set_name("hi".to_string());
println!("meow_async: {}, expected: meow", cat.meow_async().await);
}