Closed5
future created by async block is not `Send` に立ち向かう
Rust なんもわからん
#[async_trait]
pub trait ActivityController {
async fn set_activity(&self, activity: Activity);
}
pub async fn set_help_message_to_activity(ctx: Box<dyn ActivityController>) {
ctx.set_activity(Activity::playing("hoge"))
.await;
}
こんなコードを書いて
#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
let cont = Ctx::new(ctx);
usecase::set_help_message_to_activity(Box::new(cont)).await
}
}
こんな感じで呼び出してたら
future cannot be sent between threads safely
future created by async block is not `Send`
help: the trait `std::marker::Send` is not implemented for `dyn handler::usecase::ActivityController`
note: required for the cast to the object type `dyn std::future::Future<Output = ()> + std::marker::Send`rustc
mod.rs(27, 55): future created by async block is not `Send`
mod.rs(30, 9): first, await occurs here, with `Box::new(cont)` maybe used later...
mod.rs(30, 47): has type `std::boxed::Box<dyn handler::usecase::ActivityController>` which is not `Send`
すごい怒られた
Sync で Send だ!って書いてる
#[async_trait]
pub trait ActivityController {
async fn set_activity(&self, activity: Activity);
}
- pub async fn set_help_message_to_activity(ctx: Box<dyn ActivityController>) {
+ pub async fn set_help_message_to_activity(ctx: Box<dyn ActivityController + Send + Sync>) {
ctx.set_activity(Activity::playing("hoge"))
.await;
}
こんな感じで書き直したらなおった
なぜかはよくわからない
rust なんもわからん
このスクラップは2021/09/12にクローズされました