lazy-into 0.1.0

Lazy conversion from one type to another
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 7.49 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.43 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • jsode64/lazy-into
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jsode64

Lazy Into

Lazy conversion from one type to another in Rust.

Converts from types T to U with a mapping function that is only called the first time it's needed.

Usage

This is useful when you have an expensive conversion that may not even be needed.


For example, if you have a CLI app that parses deep into JSON data that the user can see by typing "data", a LazyInto could be useful.

If the user never says "data", the search is not needed.

If the user says "data" multiple times, the search is only needed once.

Example Usage

fn main() {
    use lazy_into::LazyInto;

    let a = LazyInto::new(42, |n| n as f64 * 2.0);
    let b = LazyInto::new(21, TryInto::try_into);
    let mut c = LazyInto::new(-1, |n| n.to_string());

    assert_eq!(a.get(), &84.0);
    assert_eq!(*a, 84.0);
    assert_eq!(*b, Ok(21u32));

    assert_eq!(*c, "-1");
    c.get_mut().push_str("2345");
    assert_eq!(*c, "-12345");
}