Fix Commands not being Send + Sync

This commit is contained in:
Joscha 2024-12-27 19:05:35 +01:00
parent 4160d69a2b
commit 818b8748a1

View file

@ -8,13 +8,15 @@ use std::future::Future;
use async_trait::async_trait;
use euphoxide::{
api::{Data, Message, MessageId, ParsedPacket, Send, SendEvent, SendReply},
api::{self, Data, Message, MessageId, ParsedPacket, SendEvent, SendReply},
client::{
conn::ClientConnHandle,
state::{Joined, State},
},
};
use crate::{bot::BotEvent, instance::InstanceEvent};
#[non_exhaustive]
pub struct Context {
pub conn: ClientConnHandle,
@ -27,7 +29,7 @@ impl Context {
content: S,
) -> euphoxide::Result<impl Future<Output = euphoxide::Result<SendReply>>> {
self.conn
.send(Send {
.send(api::Send {
content: content.to_string(),
parent: None,
})
@ -45,7 +47,7 @@ impl Context {
content: S,
) -> euphoxide::Result<impl Future<Output = euphoxide::Result<SendReply>>> {
self.conn
.send(Send {
.send(api::Send {
content: content.to_string(),
parent: Some(parent),
})
@ -121,7 +123,7 @@ pub trait Command<B, E> {
}
pub struct Commands<B, E = euphoxide::Error> {
commands: Vec<Box<dyn Command<B, E>>>,
commands: Vec<Box<dyn Command<B, E> + Sync + Send>>,
}
impl<B, E> Commands<B, E> {
@ -129,11 +131,11 @@ impl<B, E> Commands<B, E> {
Self { commands: vec![] }
}
pub fn add(&mut self, command: impl Command<B, E> + 'static) {
pub fn add(&mut self, command: impl Command<B, E> + Sync + Send + 'static) {
self.commands.push(Box::new(command));
}
pub fn then(mut self, command: impl Command<B, E> + 'static) -> Self {
pub fn then(mut self, command: impl Command<B, E> + Sync + Send + 'static) -> Self {
self.add(command);
self
}
@ -142,7 +144,7 @@ impl<B, E> Commands<B, E> {
self.commands.iter().map(|c| c.info(ctx)).collect()
}
pub async fn execute(
pub async fn on_packet(
&self,
conn: ClientConnHandle,
state: State,
@ -168,6 +170,38 @@ impl<B, E> Commands<B, E> {
Ok(Propagate::Yes)
}
pub async fn on_instance_event(
&self,
event: InstanceEvent,
bot: &mut B,
) -> Result<Propagate, E> {
if let InstanceEvent::Packet {
conn,
state,
packet,
..
} = event
{
self.on_packet(conn, state, packet, bot).await
} else {
Ok(Propagate::Yes)
}
}
pub async fn on_bot_event(&self, event: BotEvent, bot: &mut B) -> Result<Propagate, E> {
if let BotEvent::Packet {
conn,
state,
packet,
..
} = event
{
self.on_packet(conn, state, packet, bot).await
} else {
Ok(Propagate::Yes)
}
}
}
// Has fewer restrictions on generic types than #[derive(Default)].