ref_filter_map 1.0.1

Like `std::cell::{Ref,RefMut}::map`, but for optional components.
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented3 out of 3 items with examples
  • Size
  • Source code size: 3.79 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.09 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • SimonSapin/rust-std-candidates
    88 12 2
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • SimonSapin

The Ref and RefMut types in std::cell each have a map method that create a new Ref (RefMut) that borrows something (a sub-component) inside of a RefCell.

When that component may or may not be there, you may find yourself checking for its precense twice:

# use std::cell::{RefCell, Ref};
# use std::collections::HashMap;
fn borrow_get<'a>(hashmap: &'a RefCell<HashMap<String, String>>, key: &str)
                  -> Option<Ref<'a, String>> {
    let hashmap = hashmap.borrow();
    if hashmap.contains_key(key) {  // Duplicated hash table lookup.
        Some(Ref::map(hashmap, |hashmap| {
            &hashmap[key]  // panic!() for missing key unlikely to be optimized away
        }))
    } else {
        None
    }
}

This crate define ref_filter_map and ref_mut_filter_map functions that are a lot like Ref::map and RefMut::map, but return Option and take closures that return Option.

Internally they use a raw pointer and some unsafe code, but the API they provide is believed to be safe.

This was once part of std::cell but has been deprecated there since it makes Option too much of a special case.

https://github.com/rust-lang/rust/pull/25747 https://github.com/rust-lang/rust/issues/27746