Skip to main content

Crate ps_util

Crate ps_util 

Source
Expand description

Generally helpful utility functions and traits.

This crate provides three groups of utilities:

  • Array: a trait implemented for every AsRef<[T]> type that offers JavaScript-inspired array methods (map, filter, find, reduce, join, and many more), along with binary-search-based *_equal_in_sorted_by methods for sorted slices.
  • subarray, subarray_checked, and subarray_unchecked: functions returning a fixed-size array reference &[T; S] into a slice.
  • ToResult and ResConv: ergonomic conversions for Result and Option values.

§Examples

use ps_util::{Array, ToResult};

let arr = [1, 2, 3, 4];

assert_eq!(arr.filter(|x| x % 2 == 0), vec![2, 4]);
assert_eq!(arr.join(" + "), "1 + 2 + 3 + 4");
assert_eq!(arr.find_index_equal_in_sorted_by(|x| x.cmp(&3)), Some(2));

assert_eq!(7.ok::<()>(), Ok(7));

Traits§

Array
ResConv
Conversions between Result and Option with optional value transformation.
ToResult
Convenience constructors for Result and Option values.

Functions§

subarray
Get a subarray of length S at index.
subarray_checked
Returns a reference to a contiguous subarray of length S starting at index.
subarray_unchecked
Get a subarray of length S at index without bounds checking.