#pair #values #cached #left #string #once #convertible

cached-pair

A simple pair of values, where the one can be convertible to the other

8 releases (breaking)

new 0.7.0 Jan 18, 2025
0.6.1 Jan 17, 2025
0.5.0 Jan 3, 2025
0.4.0 Jan 3, 2025
0.1.0 Jan 2, 2025

#102 in Caching

Download history 424/week @ 2024-12-29 90/week @ 2025-01-05 65/week @ 2025-01-12

579 downloads per month

Custom license

20KB
334 lines

Crates.io Version docs.rs

cached-pair

A simple library for caching pairs of values. Similar to EitherOrBoth in the itertools crate, but with an extra assumption that the both values are convertible to each other, but the conversion is non-trivial.

For example, you can use this library to hold a pair of a binary Vec<u8> and its base64 encoded String.

Example

use cached_pair::Pair;

// Initialize a pair with a binary value (`Vec<u8>`) on the left.
let pair: Pair<Vec<u8>, String> = Pair::from_left(vec![1, 2, 3]);

// Access the left value.
assert_eq!(pair.left_opt(), Some(&vec![1, 2, 3]));

// Access the right value, with giving a conversion function.
assert_eq!(pair.right_with(|v| base64::encode(v)), "AQID");

// Once you access the right value, it will be cached and be accessible without the conversion function.
assert_eq!(pair.right_opt(), Some(&"AQID".to_string()));

Dependencies

~490KB