-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from xgroleau/feat/drop-bomb-cancellation
feat: Added drop bomb to panic in case of request cancellation
- Loading branch information
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! Example where cancellation will cause a panic | ||
#![macro_use] | ||
#![feature(type_alias_impl_trait)] | ||
#![feature(async_fn_in_trait)] | ||
#![allow(incomplete_features)] | ||
|
||
use { | ||
ector::*, | ||
embassy_time::{Duration, Timer}, | ||
futures::future::{join, select}, | ||
}; | ||
|
||
async fn test(addr: DynamicAddress<Request<&'static str, &'static str>>) { | ||
let req = core::pin::pin!(addr.request("Hello")); | ||
let timeout = Timer::after(Duration::from_secs(1)); | ||
select(req, timeout).await; | ||
println!("Timeout"); | ||
} | ||
|
||
#[embassy_executor::main] | ||
async fn main(_s: embassy_executor::Spawner) { | ||
// Example of request response | ||
static SERVER: ActorContext<Server> = ActorContext::new(); | ||
|
||
let address = SERVER.dyn_address(); | ||
let server = SERVER.mount(Server); | ||
let test = test(address); | ||
join(server, test).await; | ||
} | ||
|
||
pub struct Server; | ||
|
||
impl Actor for Server { | ||
type Message = Request<&'static str, &'static str>; | ||
|
||
async fn on_mount<M>( | ||
&mut self, | ||
_: DynamicAddress<Request<&'static str, &'static str>>, | ||
mut inbox: M, | ||
) -> ! | ||
where | ||
M: Inbox<Self::Message>, | ||
{ | ||
println!("Server started!"); | ||
|
||
loop { | ||
// We don't reply, since we cancel | ||
inbox.next().await; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Original reference https://github.com/embassy-rs/embassy/blob/e1ed492577d2151b4cc8ef995536dd045d2db087/embassy-hal-internal/src/drop.rs#L32 | ||
/// Panics if it is improperly disposed of. | ||
/// | ||
/// This is to forbid cancelling a future/request. | ||
/// | ||
/// To properly dispose, call the [defuse](Self::defuse) method before this object is dropped. | ||
#[must_use = "to delay the drop bomb invokation to the end of the scope"] | ||
pub struct DropBomb(()); | ||
impl DropBomb { | ||
pub fn new() -> Self { | ||
Self(()) | ||
} | ||
|
||
/// Defuses the bomb, rendering it safe to drop. | ||
pub fn defuse(self) { | ||
core::mem::forget(self) | ||
} | ||
} | ||
|
||
impl Drop for DropBomb { | ||
fn drop(&mut self) { | ||
panic!("Dropped before the request completed. You cannot cancel an ongoing request") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
pub(crate) mod fmt; | ||
|
||
mod actor; | ||
mod drop; | ||
pub use {actor::*, ector_macros::*}; | ||
|
||
// Reexport mutex types | ||
|