Feature/add spawn with (#516)

* Adds send bound to arbiter exec

* Adds spawn with function


---------

Co-authored-by: James Bell <jamesbell@microsoft.com>
This commit is contained in:
jamescarterbell 2025-03-11 15:31:20 -04:00 committed by GitHub
parent 9c78181c7b
commit dcc08b72d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View file

@ -1,6 +1,8 @@
# Changes
## [0.4.26] - 2025-03-12
## [0.4.27] - 2025-03-14
* Add Arbiter::spawn_with()
* Add "neon" runtime support
@ -8,6 +10,10 @@
* Drop async-std support
## [0.4.26] - 2025-03-12
* Add Arbiter::spawn_with()
## [0.4.25] - 2025-03-11
* Adds Send bound to arbiter exec (#514)

View file

@ -154,6 +154,27 @@ impl Arbiter {
.try_send(ArbiterCommand::Execute(Box::pin(future)));
}
/// Send a function to the Arbiter's thread and spawns it's resulting future.
/// This can be used to spawn non-send futures on the arbiter thread.
pub fn spawn_with<F, R, O>(&self, f: F) -> impl Future<Output = Result<O, oneshot::RecvError>> + Send + 'static
where
F: FnOnce() -> R + Send + 'static,
R: Future<Output = O> + 'static,
O: Send + 'static
{
let (tx, rx) = oneshot::channel();
let _ = self
.sender
.try_send(ArbiterCommand::ExecuteFn(Box::new(move || {
let fut = f();
let fut = Box::pin(async {
let _ = tx.send(fut.await);
});
crate::spawn(fut);
})));
rx
}
#[rustfmt::skip]
/// Send a function to the Arbiter's thread. This function will be executed asynchronously.
/// A future is created, and when resolved will contain the result of the function sent