mapped_partial_getter

Function mapped_partial_getter 

Source
pub fn mapped_partial_getter<S, A, E, GET>(
    get_fn: GET,
) -> PartialGetterImpl<S, A, impl PartialGetter<S, A, GetterError = E>>
where GET: Fn(&S) -> Result<A, E>,
Expand description

Creates a new PartialGetter with the provided getter function.

§Type Parameters

  • S: The source type of the optic
  • A: The target type of the optic
  • E: The error type returned when the focus fails

§Arguments

  • get_fn — A function that faillibly retrieves the focus value A from the source S.

§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()));