Skip to content
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

Drop dependency for failure crate, and make NodeId::remove() infallible #39

Merged
merged 4 commits into from Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -24,7 +24,6 @@ par_iter = ["rayon"]
std = []

[dependencies]
failure = { version = "0.1.5", features = ["derive"] }
rayon = { version = "1.0.3", optional = true }
serde = { version = "1.0.90", features = ["derive"], optional = true }

Expand Down
3 changes: 1 addition & 2 deletions examples/parallel_iteration.rs
@@ -1,8 +1,7 @@
use failure::Fallible;
use indextree::*;
use rayon::prelude::*;

pub fn main() -> Fallible<()> {
pub fn main() -> Result<(), NodeError> {
// Create a new arena
let arena = &mut Arena::new();

Expand Down
9 changes: 3 additions & 6 deletions src/arena.rs
Expand Up @@ -82,9 +82,8 @@ impl<T> Arena<T> {
/// let _bar = arena.new_node("bar");
/// assert_eq!(arena.count(), 2);
///
/// let _ = foo.remove(&mut arena)?;
/// foo.remove(&mut arena);
/// assert_eq!(arena.count(), 2);
/// # Ok::<(), failure::Error>(())
/// ```
pub fn count(&self) -> usize {
self.nodes.len()
Expand All @@ -102,9 +101,8 @@ impl<T> Arena<T> {
/// let foo = arena.new_node("foo");
/// assert!(!arena.is_empty());
///
/// foo.remove(&mut arena)?;
/// foo.remove(&mut arena);
/// assert!(!arena.is_empty());
/// # Ok::<(), failure::Error>(())
/// ```
pub fn is_empty(&self) -> bool {
self.count() == 0
Expand Down Expand Up @@ -190,13 +188,12 @@ impl<T> Arena<T> {
/// let mut arena = Arena::new();
/// let _foo = arena.new_node("foo");
/// let bar = arena.new_node("bar");
/// bar.remove(&mut arena)?;
/// bar.remove(&mut arena);
///
/// let mut iter = arena.iter();
/// assert_eq!(iter.next().map(|node| (node.data, node.is_removed())), Some(("foo", false)));
/// assert_eq!(iter.next().map(|node| (node.data, node.is_removed())), Some(("bar", true)));
/// assert_eq!(iter.next().map(|node| (node.data, node.is_removed())), None);
/// # Ok::<(), failure::Error>(())
/// ```
///
/// [`is_removed()`]: struct.Node.html#method.is_removed
Expand Down
79 changes: 27 additions & 52 deletions src/error.rs
Expand Up @@ -3,71 +3,46 @@
#[cfg(not(feature = "std"))]
use core::fmt;

use failure::Fail;

#[cfg(feature = "std")]
use std::{error, fmt};

#[derive(Debug, Fail)]
#[derive(Debug, Clone, Copy)]
/// Possible node failures.
pub enum NodeError {
#[fail(display = "Can not append a node to itself")]
/// Attempt to append a node to itself.
AppendSelf,

#[fail(display = "Can not prepend a node to itself")]
/// Attempt to prepend a node to itself.
PrependSelf,

#[fail(display = "Can not insert a node before itself")]
/// Attempt to insert a node before itself.
InsertBeforeSelf,

#[fail(display = "Can not insert a node after itself")]
/// Attempt to insert a node after itself.
InsertAfterSelf,
// See <https://github.com/rust-lang/rfcs/blob/master/text/2008-non-exhaustive.md#how-we-do-this-today>.
#[doc(hidden)]
__Nonexhaustive,
}

// Deprecated, no longer used.
#[fail(display = "First child is already set")]
FirstChildAlreadySet,

// Deprecated, no longer used.
#[fail(display = "Previous sibling is already set")]
PreviousSiblingAlreadySet,

// Deprecated, no longer used.
#[fail(display = "Next sibling is already set")]
NextSiblingAlreadySet,

// Deprecated, no longer used.
#[fail(display = "Previous sibling not equal current node")]
PreviousSiblingNotSelf,

// Deprecated, no longer used.
#[fail(display = "Next sibling not equal current node")]
NextSiblingNotSelf,

// Deprecated, no longer used.
#[fail(display = "First child not equal current node")]
FirstChildNotSelf,

// Deprecated, no longer used.
#[fail(display = "Last child not equal current node")]
LastChildNotSelf,

// Deprecated, no longer used.
#[fail(display = "Previous sibling is not set")]
PreviousSiblingNotSet,

// Deprecated, no longer used.
#[fail(display = "Next sibling is not set")]
NextSiblingNotSet,

// Deprecated, no longer used.
#[fail(display = "First child is not set")]
FirstChildNotSet,
impl NodeError {
fn as_str(self) -> &'static str {
match self {
NodeError::AppendSelf => "Can not append a node to itself",
NodeError::PrependSelf => "Can not prepend a node to itself",
NodeError::InsertBeforeSelf => "Can not insert a node before itself",
NodeError::InsertAfterSelf => "Can not insert a node after itself",
NodeError::__Nonexhaustive => panic!("`__Nonexhaustive` should not be used"),
}
}
}

// Deprecated, no longer used.
#[fail(display = "Last child is not set")]
LastChildNotSet,
impl fmt::Display for NodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}

#[cfg(feature = "std")]
impl error::Error for NodeError {}

/// An error type that represents the given structure or argument is
/// inconsistent or invalid.
// Intended for internal use.
Expand Down