Futures-rs: Receiver::Error should be something like Never or Nothing

Created on 15 May 2017  路  12Comments  路  Source: rust-lang/futures-rs

enum Nothing {}

impl Stream for Receiver<T> {
    type Error = Nothing;
}

To clearly state that poll never fails.

C-feature-request

Most helpful comment

When it's eventually stable, the never type (!) would be ideal for this.

All 12 comments

BTW, tokio_core::reactor::Handle::spawn future parameter Error should probably be Nothing too.

... and Executor impl for CpuPool too.

Currently it has signature

impl<F> Executor<F> for CpuPool
    where F: Future<Item = (), Error = ()> + Send + 'static,

Which gives user false sense that it is OK to return error. While it is not, error must be handled inside the future.

FWIW Elm uses Never as the name for this type, to signify that a certain task for instance can "never" fail. I always thought that particular name worked quite well.

When it's eventually stable, the never type (!) would be ideal for this.

I wrote in https://github.com/alexcrichton/futures-rs/issues/554:


Right now there's a number of locations in this library where () is used as an error type

I've also heard of this being common enough throughout the ecosystem that I think we should provide a solution to this! I think we'll want to not "just add a Void" type but also make sure it's at least reasonably ergonomic to use. For example I think we should add:

enum Void {}
Void::into(self) -> T
Future<Item=T, Error=Void>::poll_ok(&mut self) -> Async<T>
Future<Item=T, Error=Void>::wait_ok(self) -> T

And maybe more! I'm curious to hear what others think about adding such APIs.

I wouldn't mind trying to game this out before an 0.2 release. We can't change existing types but we could add the type for external libraries to use, as well as the extra goodies on the Future trait itself!

Is there no hope of the never type being stabilized in a time frame that would make it reasonable to just wait on this issue rather than introduce a futures-specific void type?

AFAIK yeah, the stabilization story for ! is quite long-term still

@alexcrichton Are you saying you would consider a PR adding Void to this crate in order for dependent libraries to use, plus a few utilities (converting Future<Error=Void> to any Future<Error=A> comes to mind)? If so, I'd be interested in picking this up.

Certainly! Unfortunately we can't land a breaking change right now so we can't change the type Error = ();, but we could start paving the road for future implementations!

@jimmycuadra I havent looked to deep into ! but shouldn't it be possible to use the Never type (which by now is in the tokio 0.2 branch) and once it's stable change Never to wrap ! (i.e. struct Never(!)). I would
guess that should give the same benefit's then directly using ! while keeping backward compability intact (e.g. someone might implement a trait on both Never and ! which "somehow" conflicts with implementing it on !)

~@alexcrichton does that mean it will change for the v0.2 release or that it will stay () and just new or otherwise changed API's will get the change?.~

I was looking at a not up-to-date version of v0.2, the changes are there by now.
(so this Issue can be closed?)

This has been done for 0.2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xrl picture xrl  路  3Comments

jimblandy picture jimblandy  路  5Comments

hh9527 picture hh9527  路  4Comments

seanmonstar picture seanmonstar  路  4Comments

FSMaxB-dooshop picture FSMaxB-dooshop  路  4Comments