Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider having remote.spawn return a future w/ the result #77

Closed
carllerche opened this issue Oct 24, 2016 · 2 comments
Closed

Consider having remote.spawn return a future w/ the result #77

carllerche opened this issue Oct 24, 2016 · 2 comments

Comments

@carllerche
Copy link
Member

carllerche commented Oct 24, 2016

I think that is will be common to want to get the result of a of a spawned task. We should consder having remote.spawn (and other task spawning fns) return a future w/ the result.

The question will then be what happens if the returned future is dropped instead of consumed. As per this comment I would suggest that, by default, dropping the result future cancels the task, unless a result.detach() is called.

There should also be a "spawn" variant that doesn't return a future in order to avoid the allocation. I would suggest remote.fire(_fn_)

Related: #75, #57

@ArtemGr
Copy link

ArtemGr commented Mar 29, 2017

I wonder if a function I wrote recently is related to this issue:

/// With a shared reactor drives the future `f` to completion.
///
/// NB: This function is only useful if you need to get the results of the execution.  
/// If the results are not necessary then a future can be scheduled directly on the reactor:
///
///     CORE.spawn (|_| f);
pub fn drive<F, R, E> (f: F) -> Receiver<Result<R, E>> where F: Future<Item=R, Error=E> + Send + 'static, R: Send + 'static, E: Send + 'static {
  let (sx, rx) = oneshot::channel();
  CORE.spawn (move |_handle| {
    f.then (move |fr: Result<R, E>| -> Result<(),()> {
      let _ = sx.send (fr);
      Ok(())})});
  rx}

/// Drives the future to completion. Logs any errors.
///
/// NB: If the future returns `(),()` then use the `CORE.spawn (|_| f)` directly.
pub fn later<F, R, E> (f: F) where F: Future<Item=R, Error=E> + Send + 'static, R: Send + 'static, E: fmt::Display + Send + 'static {
  CORE.spawn (|_handle| f.then (move |fr| -> Result<(),()> {
    if let Err (err) = fr {log! ("later] Error: {}", err)}
    Ok(())}))}

@alexcrichton
Copy link
Contributor

I believe this will be obsoleted by rust-lang/futures-rs#455, so closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants