pub fn mapped_partial_getter<S, A, E, GET>(
get_fn: GET,
) -> PartialGetterImpl<S, A, impl PartialGetter<S, A, GetterError = E>>Expand description
Creates a new PartialGetter with the provided getter function.
§Type Parameters
S: The source type of the opticA: The target type of the opticE: The error type returned when the focus fails
§Arguments
get_fn— A function that faillibly retrieves the focus valueAfrom the sourceS.
§Returns
A new PartialGetterImpl instance that can be used as a PartialGetter<S, A>.
§Examples
use optics::{mapped_partial_getter, HasGetter};
enum IpAddress { Ipv4(String), Ipv6(String) }
let ipv4_partial_getter = mapped_partial_getter(
|s: &IpAddress| if let IpAddress::Ipv4(ip) = s { Ok(ip.clone()) } else { Err(()) }
);
let addr = IpAddress::Ipv4("8.8.4.4".to_string());
assert_eq!(ipv4_partial_getter.try_get(&addr), Ok("8.8.4.4".to_string()));