Skip to content

Commit

Permalink
Created utility uninhabited type
Browse files Browse the repository at this point in the history
In this commit I added a useful utility type, named Void, that encapsulates the
doable but annoying job of creating an uninhabited type. As well, a function on
that type, named absurd, was created which is useful for ignoring the result of
matching on that type. No unit tests were created because it is not possible to
create an instance of this type to test the usage of.

This type is useful because it is like NonCopyable in that it can be used to
create a type with special characteristics without special bloat. For instance,
instead of typing pub struct PhantomType { priv contents : () } for each void
type one may want to use one can simply type pub struct PhantomType (Void);.
This type make such special cases much easier to write.
  • Loading branch information
Steven Stewart-Gallus committed May 9, 2013
1 parent d82d987 commit 72c9aab
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/libcore/util.rs
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -74,6 +74,20 @@ impl Drop for NonCopyable {

pub fn NonCopyable() -> NonCopyable { NonCopyable { i: () } }


/// A type with no inhabitants
pub enum Void { }

pub impl Void {
/// A utility function for ignoring this uninhabited type
fn uninhabited(&self) -> ! {
match *self {
// Nothing to match on
}
}
}


/**
A utility function for indicating unreachable code. It will fail if
executed. This is occasionally useful to put after loops that never
Expand Down

5 comments on commit 72c9aab

@bors
Copy link
Contributor

@bors bors commented on 72c9aab May 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at mstewartgallus@72c9aab

@bors
Copy link
Contributor

@bors bors commented on 72c9aab May 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sstewartgallus/rust/incoming = 72c9aab into auto

@bors
Copy link
Contributor

@bors bors commented on 72c9aab May 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sstewartgallus/rust/incoming = 72c9aab merged ok, testing candidate = 8d1a09c

@bors
Copy link
Contributor

@bors bors commented on 72c9aab May 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 72c9aab May 13, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 8d1a09c

Please sign in to comment.