shpat/lib.rs
1//! A bunch of patterns that I often met while programming in Rust. I put them
2//! in a single crate, so other Rustaceans may also enjoy them.
3//!
4//! This crate has the following goals:
5//! - as lightweight as possible, no dependencies,
6//! - integrate patterns with each others.
7//!
8//! # Current patterns
9//!
10//! ### `apply`
11//!
12//! Some methods in Rust don't take ownership, and just take reference of an
13//! object. This may be nice in some situations, but this does not allow to
14//! chain methods. The `Apply` trait allows to bring back method chaining for
15//! methods which take mutable references.
16//!
17//! For example, `HashMap` can be created easily:
18//!
19//! ```rust
20//! use std::collections::HashMap;
21//!
22//! use shpat::prelude::*;
23//!
24//! let map = HashMap::new()
25//! .apply(|m| m.insert("manatee", 42))
26//! .apply(|m| m.insert("horse", 101));
27//! ```
28//!
29//! The `Apply` trait is implemented for every `Sized` type. It also provides
30//! `apply_keep`, which returns the value returned by the inner method and the
31//! object itself (which may be usefull when one wants to insert a value in a
32//! hash map, but want to keep returned value), and `apply_unwrap`, which will
33//! call `unwrap` on every `Unwrapable` returned value.
34//!
35//! ## `Unwrappable`
36//!
37//! The `Unwrappable` trait is an attempt to unify the behavior of types which
38//! represent a success or failure dichotomy, such as `Result` and `Option`.
39//! These type implement a method which returns the success value, and panics
40//! if it was a failure. These behaviours are unified with the `unwrap`
41//! function.
42//!
43//! This trait is implemented for both `Result` and `Option`. It is closely
44//! related to the `Try` trait from the standard library.
45
46#![forbid(missing_docs)]
47#![forbid(clippy::missing_docs_in_private_items)]
48#![forbid(clippy::missing_errors_doc)]
49
50mod apply;
51mod unwrappable;
52
53pub mod prelude;