Make Instance cloneable

This commit is contained in:
Joscha 2023-03-04 00:06:51 +01:00
parent 5005e56881
commit 373a98c26c
2 changed files with 6 additions and 5 deletions

View file

@ -23,6 +23,7 @@ Procedure when bumping the version number:
- `bot::command::ClapCommand::execute` now returns a `Result<bool, E>` instead of a `Result<(), E>` - `bot::command::ClapCommand::execute` now returns a `Result<bool, E>` instead of a `Result<(), E>`
- `bot::command::Command::execute` now returns a `Result<bool, E>` instead of a `Result<(), E>` - `bot::command::Command::execute` now returns a `Result<bool, E>` instead of a `Result<(), E>`
- `bot::commands::Commands::handle_packet` now returns a `Result<bool, E>` instead of a `Result<(), E>` - `bot::commands::Commands::handle_packet` now returns a `Result<bool, E>` instead of a `Result<(), E>`
- `bot::instance::Instance` now implements `Clone`
### Fixed ### Fixed
- `phone` and `mobile` emoji - `phone` and `mobile` emoji

View file

@ -273,13 +273,13 @@ enum RunError {
/// either case, the last event the instance sends will be an /// either case, the last event the instance sends will be an
/// [`Event::Stopped`]. If it is not stopped using one of these two ways, it /// [`Event::Stopped`]. If it is not stopped using one of these two ways, it
/// will continue to run and reconnect indefinitely. /// will continue to run and reconnect indefinitely.
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Instance { pub struct Instance {
config: InstanceConfig, config: InstanceConfig,
request_tx: mpsc::UnboundedSender<Request>, request_tx: mpsc::UnboundedSender<Request>,
// In theory, request_tx should be sufficient as canary, but I'm not sure // In theory, request_tx should be sufficient as canary, but I'm not sure
// exactly how to check it during the reconnect timeout. // exactly how to check it during the reconnect timeout.
_canary_tx: oneshot::Sender<Infallible>, _canary_tx: mpsc::UnboundedSender<Infallible>,
} }
impl Instance { impl Instance {
@ -312,7 +312,7 @@ impl Instance {
idebug!(config, "Created with config {config:?}"); idebug!(config, "Created with config {config:?}");
let (request_tx, request_rx) = mpsc::unbounded_channel(); let (request_tx, request_rx) = mpsc::unbounded_channel();
let (canary_tx, canary_rx) = oneshot::channel(); let (canary_tx, canary_rx) = mpsc::unbounded_channel();
tokio::spawn(Self::run::<F>( tokio::spawn(Self::run::<F>(
config.clone(), config.clone(),
@ -360,11 +360,11 @@ impl Instance {
config: InstanceConfig, config: InstanceConfig,
on_event: F, on_event: F,
request_rx: mpsc::UnboundedReceiver<Request>, request_rx: mpsc::UnboundedReceiver<Request>,
canary_rx: oneshot::Receiver<Infallible>, mut canary_rx: mpsc::UnboundedReceiver<Infallible>,
) { ) {
select! { select! {
_ = Self::stay_connected(&config, &on_event, request_rx) => (), _ = Self::stay_connected(&config, &on_event, request_rx) => (),
_ = canary_rx => { idebug!(config, "Instance dropped"); }, _ = canary_rx.recv() => { idebug!(config, "Instance dropped"); },
} }
on_event(Event::Stopped(config)) on_event(Event::Stopped(config))
} }