Skip to content

Never vs. PhantomData #32

Closed
Closed
@cramertj

Description

@cramertj

An RFC was recently approved for the ! (Never) type, which represents the type of a value that can never exist.

Several structs in of futures-rs use PhantomData to mark the type of values that can't exist, such as the Error type of Finished, the Item type of Failed, and the Item and Error types of Empty. I believe that, once this PR lands (and then makes its way to stable), these structs should be rewritten to use the Never type. Empty, for example, could be written as follows:

pub struct Empty {}

pub fn empty() -> Empty { // Is this even necessary any more?
    Empty {}
}

impl Future for Empty {
    type Item = !;
    type Error = !;

    fn poll(&mut self, _: &mut Task) -> Poll<Self::Item, Self::Error> {
        Poll::NotReady
    }

    fn schedule(&mut self, task: &mut Task) {
        drop(task);
    }
}

Unfortunately, this feature has yet to land on nightly, let alone stable. Therefore, a transition to Never is likely a ways off. As this library is already getting lots of attention, I believe there should be a plan for how and if to structure the library so that a future (hehe 😃 ) transition to Never is possible.

In order to be more consistent with terminology, I also think that Empty should be renamed to Never. ! is called Never rather than Empty or Void as a result of the discussion at the end of the Never PR. The gist of it is that Empty isn't a future for an Empty value, it's a future for a value that can Never exist (i.e. it Never completes).

P.S. Great work on this library, @alexcrichton & company! I'm excited to use this in my future Rust projects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions