unempty/
lib.rs

1//! Non-empty data structures for Rust.
2//!
3//! Other crates are more focused for a single data structure (e.g. Vec or HashMap).
4//! The goal of this crate is to contain non-empty implementations for all commonly used data structures.
5//!
6//! Of course, "all" is quite a lot. I intend to add to this as I need non-empty data structures.
7//! If you need one, PRs are extremely welcome!
8//!
9//! # Path based imports
10//!
11//! Data structures in this crate use the same name as the data structure they're wrapping.
12//! It is highly recommended to use the full path name of the data structures instead of shadowing others.
13//!
14//! For example, instead of:
15//! ```
16//! use unempty::Vec;
17//! let v = Vec::new(());
18//! ```
19//!
20//! Please **strongly** consider:
21//! ```
22//! let v = unempty::Vec::new(());
23//! ```
24//!
25//! This reduces confusion for people reading your source code later,
26//! _and_ improves the ability for your program to interact with both non-empty and standard data structures.
27//!
28//! # Why
29//!
30//! Expressing constraints in the type system is powerful, and non-empty data structures are no exception.
31//!
32//! A data structure that is impossible to be empty frees developers from needing to check for the empty case,
33//! and allows them to express an expectation in their types rather than solely at runtime or in their documentation.
34//!
35//! This also enables some convenience features or even performance improvements.
36//! For example, an `unempty::Vec` can always fulfill a call to `first` or `last`, so these don't need to be `Option`.
37//!
38//! # Esoteric support
39//!
40//! ## Unsafe
41//!
42//! Generally, this library doesn't re-implement the unsafe portions of the underlying data structure.
43//! This is mostly because I haven't needed to use them. If you need them, please open a PR!
44//!
45//! ## Nightly or Beta
46//!
47//! Generally, this library doesn't re-implement the nightly or beta portions of the underlying data structure.
48//! This is mostly because I haven't needed to use them. If you need them, please open a PR!
49
50#![deny(clippy::unwrap_used)]
51#![deny(missing_docs)]
52#![warn(rust_2018_idioms)]
53
54#[cfg(not(any(feature = "std")))]
55compile_error!("The `std` feature is currently required. Adding support for `no-std` is backwards compatible! If you need this, a PR is extremely welcome!");
56
57mod capacity;
58mod structures;
59
60pub use capacity::*;
61pub use structures::*;
62use thiserror::Error;
63
64/// Errors encountered when converting from a source data structure to an `unempty` data structure.
65#[derive(Error, Debug)]
66pub enum TryFromError {
67    /// The source data structure is empty.
68    #[error("The source data structure is empty")]
69    SourceEmpty,
70}