Function script::dom::bindings::cell::ref_filter_map

source ·
pub fn ref_filter_map<T, U, F>(orig: Ref<'_, T>, f: F) -> Option<Ref<'_, U>>
where F: FnOnce(&T) -> Option<&U>, T: ?Sized, U: ?Sized,
Expand description

Make a new Ref for a optional component of the borrowed data, e.g. an enum variant.

The RefCell is already immutably borrowed, so this cannot fail.

This is an associated function that needs to be used as Ref::filter_map(...). A method would interfere with methods of the same name on the contents of a RefCell used through Deref.

§Example

use std::cell::{RefCell, Ref};
use ref_filter_map::ref_filter_map;

let c = RefCell::new(Ok(5));
let b1: Ref<Result<u32, ()>> = c.borrow();
let b2: Ref<u32> = ref_filter_map(b1, |o| o.as_ref().ok()).unwrap();
assert_eq!(*b2, 5)