Skip to content

Use core::convert::Infallible instead of a custom NoError struct #39

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/unify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
//! When you have keys with non-trivial values, you must also define
//! how those values can be merged. As part of doing this, you can
//! define the "error" type to return on error; if errors are not
//! possible, use `NoError` (an uninstantiable struct). Using this
//! type also unlocks various more ergonomic methods (e.g., `union()`
//! in place of `unify_var_var()`).
//! possible, use [`Infallible`](core::convert::Infallible)
//! an uninstantiable enum). Using this type also unlocks various more
//! ergonomic methods (e.g., `union()` in place of `unify_var_var()`).
//!
//! The best way to see how it is used is to read the `tests.rs` file;
//! search for e.g. `UnitKey`.

use std::convert::Infallible;
use std::fmt::Debug;
use std::marker;
use std::ops::Range;
Expand Down Expand Up @@ -109,8 +110,8 @@ pub trait UnifyKey: Copy + Clone + Debug + PartialEq {
/// equal.
pub trait UnifyValue: Clone + Debug {
/// Defines the type to return when merging of two values fails.
/// If merging is infallible, use the special struct `NoError`
/// found in this crate, which unlocks various more convenient
/// If merging is infallible, use the special struct [`Infallible`]
/// which unlocks various more convenient
/// methods on the unification table.
type Error;

Expand Down Expand Up @@ -144,13 +145,6 @@ impl<T: EqUnifyValue> UnifyValue for T {
}
}

/// A struct which can never be instantiated. Used
/// for the error type for infallible cases.
#[derive(Debug)]
pub struct NoError {
_dummy: (),
}

/// Value of a unification key. We implement Tarjan's union-find
/// algorithm: when two keys are unified, one of them is converted
/// into a "redirect" pointing at the other. These redirects form a
Expand Down Expand Up @@ -471,24 +465,24 @@ where
V: UnifyValue,
{
/// Unions two keys without the possibility of failure; only
/// applicable when unify values use `NoError` as their error
/// applicable when unify values use [`Infallible`] as their error
/// type.
pub fn union<K1, K2>(&mut self, a_id: K1, b_id: K2)
where
K1: Into<K>,
K2: Into<K>,
V: UnifyValue<Error = NoError>,
V: UnifyValue<Error = Infallible>,
{
self.unify_var_var(a_id, b_id).unwrap();
}

/// Unions a key and a value without the possibility of failure;
/// only applicable when unify values use `NoError` as their error
/// only applicable when unify values use [`Infallible`] as their error
/// type.
pub fn union_value<K1>(&mut self, id: K1, value: V)
where
K1: Into<K>,
V: UnifyValue<Error = NoError>,
V: UnifyValue<Error = Infallible>,
{
self.unify_var_value(id, value).unwrap();
}
Expand Down Expand Up @@ -571,9 +565,9 @@ where
///////////////////////////////////////////////////////////////////////////

impl UnifyValue for () {
type Error = NoError;
type Error = Infallible;

fn unify_values(_: &(), _: &()) -> Result<(), NoError> {
fn unify_values(_: &(), _: &()) -> Result<(), Infallible> {
Ok(())
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/unify/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use self::test::Bencher;
use std::cmp;
#[cfg(feature = "persistent")]
use unify::Persistent;
use unify::{EqUnifyValue, InPlace, InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
use unify::{EqUnifyValue, InPlace, InPlaceUnificationTable, Infallible, UnifyKey, UnifyValue};
use unify::{UnificationStore, UnificationTable};

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -397,9 +397,9 @@ impl UnifyKey for OrderedKey {
}

impl UnifyValue for OrderedRank {
type Error = NoError;
type Error = Infallible;

fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Infallible> {
Ok(OrderedRank(cmp::max(value1.0, value2.0)))
}
}
Expand Down