Skip to content

Commit

Permalink
added feature to alias void to infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankurte committed Jun 10, 2020
1 parent a6e0612 commit 277b487
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ language: rust
sudo: false

rust:
- 1.0.0
- 1.34.0
- stable
- beta
- nightly

script:
- cargo build
- cargo test
- if [ "$TRAVIS_RUST_VERSION" != "1.0.0" ]; then cargo test --no-default-features; fi
- cargo test --no-default-features
- cargo test --features=infallible
- cargo test --no-default-features --features=infallible
- cargo bench --no-run
- cargo doc

Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ repository = "https://github.com/reem/rust-void.git"
description = "The uninhabited void type for use in statically impossible cases."
readme = "README.md"
license = "MIT / Apache-2.0"
edition = "2018"

[features]
std = []
infallible = []

default = ["std"]
std = []

81 changes: 40 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,61 @@
//! This crate also comes ready with several extension traits for Result that add
//! extra functionality to `Result<T, Void>` and `Result<Void, E>`.
//!
//! This can be aliased to core::convert::Infallible using the crates `infallible` feature.
//!

#[cfg(not(feature = "std"))]
mod coreprovider {
extern crate core;
pub use core::{fmt, cmp};
}
/// The empty type for cases which can't occur (aliased to core::convert::Infallible)
#[cfg(feature = "infallible")]
pub type Void = core::convert::Infallible;

#[cfg(feature = "std")]
mod coreprovider {
pub use std::{fmt, cmp, error};
}
#[cfg(not(feature = "infallible"))]
pub use void::Void;

use coreprovider::*;
#[cfg(not(feature = "infallible"))]
mod void {
/// The empty type for cases which can't occur.
#[derive(Copy)]
pub enum Void { }

/// The empty type for cases which can't occur.
#[derive(Copy)]
pub enum Void { }

impl Clone for Void {
fn clone(&self) -> Void {
unreachable(*self)
impl Clone for Void {
fn clone(&self) -> Void {
super::unreachable(*self)
}
}
}

impl fmt::Debug for Void {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
unreachable(*self)
impl core::fmt::Debug for Void {
fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
super::unreachable(*self)
}
}
}

impl fmt::Display for Void {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
unreachable(*self)
impl core::fmt::Display for Void {
fn fmt(&self, _: &mut core::fmt::Formatter) -> core::fmt::Result {
super::unreachable(*self)
}
}
}

impl<T> cmp::PartialEq<T> for Void {
fn eq(&self, _: &T) -> bool {
unreachable(*self)
impl<T> core::cmp::PartialEq<T> for Void {
fn eq(&self, _: &T) -> bool {
super::unreachable(*self)
}
}
}

impl<T> cmp::PartialOrd<T> for Void {
fn partial_cmp(&self, _: &T) -> Option<cmp::Ordering> {
unreachable(*self)
impl<T> core::cmp::PartialOrd<T> for Void {
fn partial_cmp(&self, _: &T) -> Option<core::cmp::Ordering> {
super::unreachable(*self)
}
}
}

#[cfg(feature = "std")]
impl error::Error for Void {
fn description(&self) -> &str {
unreachable(*self)
}
#[cfg(feature = "std")]
impl std::error::Error for Void {
fn description(&self) -> &str {
super::unreachable(*self)
}

fn cause(&self) -> Option<&error::Error> {
unreachable(*self)
fn cause(&self) -> Option<&(dyn std::error::Error)> {
super::unreachable(*self)
}
}
}

Expand Down

0 comments on commit 277b487

Please sign in to comment.