Skip to content

Commit

Permalink
Rename JoinHandle to AbortHandle to more closely match its behavior
Browse files Browse the repository at this point in the history
(and to align with tokio's conventions)
  • Loading branch information
jerel committed Jul 5, 2023
1 parent 4bbc2c5 commit 57b90ab
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 52 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,29 @@ _View the [example](https://github.com/jerel/membrane/tree/main/example) directo

In your crate's `lib.rs` add a `RUNTIME` static that will survive for the lifetime of the program. `RUNTIME` must hold an instance of `membrane::App<Runtime>` where `Runtime` has the `membrane::Interface` trait implemented for whichever async framework you wish to use. In our examples we use `tokio` to provide the runtime behavior, you are welcome to copy it:
``` rust
use membrane::runtime::{App, Interface, JoinHandle};
use membrane::runtime::{App, Interface, AbortHandle};

pub struct Runtime(tokio::runtime::Runtime);

impl Interface for Runtime {
fn spawn<F>(&self, future: F) -> JoinHandle
fn spawn<F>(&self, future: F) -> AbortHandle
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
let handle = self.0.spawn(future);
JoinHandle {
AbortHandle {
abort: Box::new(move || handle.abort()),
}
}

fn spawn_blocking<F, R>(&self, future: F) -> JoinHandle
fn spawn_blocking<F, R>(&self, future: F) -> AbortHandle
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let handle = self.0.spawn_blocking(future);
JoinHandle {
AbortHandle {
abort: Box::new(move || handle.abort()),
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/src/application/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn contact_async_emitter(user_id: String) -> impl Emitter<Result<data::Conta
});

let e = emitter.clone();
// drop the JoinHandle to detach the thread
// drop the AbortHandle to detach the thread
let _ = thread::spawn(move || {
e.on_done(|| {
println!("\n[contact_async_emitter] the finalizer has been called for the Emitter");
Expand Down Expand Up @@ -119,7 +119,7 @@ pub fn contact_async_stream_emitter(
.for_each(|contact| {
let stream = stream.clone();

// drop the JoinHandle to detach the thread
// drop the AbortHandle to detach the thread
let _ = thread::spawn(move || {
let id = thread::current().id();

Expand Down
18 changes: 7 additions & 11 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
mod application;
mod data;

use membrane::runtime::{App, Interface, JoinHandle};
use membrane::runtime::{App, Interface, AbortHandle};

pub struct Runtime(tokio::runtime::Runtime);

impl Interface for Runtime {
fn spawn<F>(&self, future: F) -> JoinHandle
fn spawn<F>(&self, future: F) -> AbortHandle
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
let handle = self.0.spawn(future);
JoinHandle {
abort: Box::new(move || handle.abort()),
}
let join_handle = self.0.spawn(future);
AbortHandle::new(move || join_handle.abort())
}

fn spawn_blocking<F, R>(&self, future: F) -> JoinHandle
fn spawn_blocking<F, R>(&self, future: F) -> AbortHandle
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let handle = self.0.spawn_blocking(future);
JoinHandle {
abort: Box::new(move || handle.abort()),
}
let join_handle = self.0.spawn_blocking(future);
AbortHandle::new(move || join_handle.abort())
}
}

Expand Down
25 changes: 18 additions & 7 deletions membrane/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ use once_cell::sync::OnceCell;
use std::future::Future;

pub trait Interface {
fn spawn<F>(&self, future: F) -> JoinHandle
fn spawn<F>(&self, future: F) -> AbortHandle
where
F: Future + Send + 'static,
F::Output: Send + 'static;

fn info_spawn<F>(&self, future: F, _info: Info) -> JoinHandle
fn info_spawn<F>(&self, future: F, _info: Info) -> AbortHandle
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.spawn(future)
}

fn spawn_blocking<F, R>(&self, future: F) -> JoinHandle
fn spawn_blocking<F, R>(&self, future: F) -> AbortHandle
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static;

fn info_spawn_blocking<F, R>(&self, future: F, _info: Info) -> JoinHandle
fn info_spawn_blocking<F, R>(&self, future: F, _info: Info) -> AbortHandle
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
Expand All @@ -29,13 +29,24 @@ pub trait Interface {
}
}

pub struct JoinHandle {
pub abort: Box<dyn Fn()>,
#[deprecated(since="0.9.5", note="please use `AbortHandle` instead, renamed to better match common tokio naming convention")]
pub type JoinHandle = AbortHandle;

pub struct AbortHandle {
#[deprecated(since="0.9.5", note="please use `AbortHandle::new()` instead")]
pub abort: Box<dyn Fn() + Send + 'static>,
}

impl JoinHandle {
impl AbortHandle {
pub fn new(abort: impl Fn() + Send + 'static) -> Self {
#[allow(deprecated)]
Self {
abort: Box::new(abort)
}
}

pub fn abort(&self) {
#[allow(deprecated)]
(self.abort)();
}
}
Expand Down
14 changes: 5 additions & 9 deletions membrane/tests/mock.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
use membrane::runtime::{App, Interface, JoinHandle};
use membrane::runtime::{App, Interface, AbortHandle};
use std::future::Future;

pub struct TestRuntime();

impl Interface for TestRuntime {
fn spawn<T>(&self, _future: T) -> JoinHandle
fn spawn<T>(&self, _future: T) -> AbortHandle
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
JoinHandle {
abort: Box::new(|| {}),
}
AbortHandle::new(|| {})
}

fn spawn_blocking<F, R>(&self, _future: F) -> JoinHandle
fn spawn_blocking<F, R>(&self, _future: F) -> AbortHandle
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
JoinHandle {
abort: Box::new(|| {}),
}
AbortHandle::new(|| {})
}
}

Expand Down
14 changes: 5 additions & 9 deletions membrane/tests/ui/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,26 @@ pub async fn one_success() -> Result<Vec<i32>, String> {
Ok(vec![10])
}

use membrane::runtime::{App, Interface, JoinHandle};
use membrane::runtime::{App, Interface, AbortHandle};
use membrane::{async_dart, sync_dart};
use std::future::Future;

struct TestRuntime();
impl Interface for TestRuntime {
fn spawn<T>(&self, future: T) -> JoinHandle
fn spawn<T>(&self, future: T) -> AbortHandle
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
JoinHandle {
abort: Box::new(|| {}),
}
AbortHandle::new(|| {})
}

fn spawn_blocking<F, R>(&self, future: F) -> JoinHandle
fn spawn_blocking<F, R>(&self, future: F) -> AbortHandle
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
JoinHandle {
abort: Box::new(|| {}),
}
AbortHandle::new(|| {})
}
}

Expand Down
14 changes: 5 additions & 9 deletions membrane/tests/ui/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,25 @@ pub fn one_success() -> impl Stream<Item = Result<i32, String>> {

use futures::Stream;
use membrane::async_dart;
use membrane::runtime::{App, Interface, JoinHandle};
use membrane::runtime::{App, Interface, AbortHandle};
use std::future::Future;

struct TestRuntime();
impl Interface for TestRuntime {
fn spawn<T>(&self, future: T) -> JoinHandle
fn spawn<T>(&self, future: T) -> AbortHandle
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
JoinHandle {
abort: Box::new(|| {}),
}
AbortHandle::new(|| {})
}

fn spawn_blocking<F, R>(&self, future: F) -> JoinHandle
fn spawn_blocking<F, R>(&self, future: F) -> AbortHandle
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
JoinHandle {
abort: Box::new(|| {}),
}
AbortHandle::new(|| {})
}
}

Expand Down

0 comments on commit 57b90ab

Please sign in to comment.