use cano_macros::task;
#[task]
trait Greet {
async fn hello(&self) -> Result<String, ()>;
}
struct World;
#[task]
impl Greet for World {
async fn hello(&self) -> Result<String, ()> {
Ok("hello".to_string())
}
}
#[task]
trait Echo {
async fn echo(&self, msg: &str) -> String;
}
struct Parrot;
#[task]
impl Echo for Parrot {
async fn echo(&self, msg: &str) -> String {
msg.to_string()
}
}
fn assert_send<T: Send>(_: T) {}
#[tokio::test]
async fn test_basic_required_method() {
let w = World;
let result = w.hello().await.unwrap();
assert_eq!(result, "hello");
}
#[tokio::test]
async fn test_basic_required_method_through_dyn() {
let w: Box<dyn Greet + Send + Sync> = Box::new(World);
let result = w.hello().await.unwrap();
assert_eq!(result, "hello");
}
#[tokio::test]
async fn test_echo_with_ref_arg() {
let p = Parrot;
assert_eq!(p.echo("cano").await, "cano");
}
#[tokio::test]
async fn test_echo_through_dyn() {
let p: Box<dyn Echo + Send + Sync> = Box::new(Parrot);
assert_eq!(p.echo("cano").await, "cano");
}
#[tokio::test]
async fn test_boxed_future_is_send() {
let w = World;
let fut = w.hello();
assert_send(fut);
}