Skip to main content

ps_util/
lib.rs

1//! Generally helpful utility functions and traits.
2//!
3//! This crate provides three groups of utilities:
4//!
5//! - [`Array`]: a trait implemented for every `AsRef<[T]>` type that offers
6//!   JavaScript-inspired array methods ([`map`](Array::map),
7//!   [`filter`](Array::filter), [`find`](Array::find),
8//!   [`reduce`](Array::reduce), [`join`](Array::join), and many more),
9//!   along with binary-search-based `*_equal_in_sorted_by` methods
10//!   for sorted slices.
11//! - [`subarray`], [`subarray_checked`], and [`subarray_unchecked`]:
12//!   functions returning a fixed-size array reference `&[T; S]` into a slice.
13//! - [`ToResult`] and [`ResConv`]: ergonomic conversions for `Result`
14//!   and `Option` values.
15//!
16//! # Examples
17//!
18//! ```
19//! use ps_util::{Array, ToResult};
20//!
21//! let arr = [1, 2, 3, 4];
22//!
23//! assert_eq!(arr.filter(|x| x % 2 == 0), vec![2, 4]);
24//! assert_eq!(arr.join(" + "), "1 + 2 + 3 + 4");
25//! assert_eq!(arr.find_index_equal_in_sorted_by(|x| x.cmp(&3)), Some(2));
26//!
27//! assert_eq!(7.ok::<()>(), Ok(7));
28//! ```
29
30mod array;
31mod conversions;
32mod subarray;
33mod subarray_checked;
34mod subarray_unchecked;
35
36#[cfg(test)]
37mod tests;
38
39pub use array::*;
40pub use conversions::*;
41pub use subarray::*;
42pub use subarray_checked::*;
43pub use subarray_unchecked::*;