pub trait MappableFrame {
    type Frame<X>;

    // Required method
    fn map_frame<A, B>(
        input: Self::Frame<A>,
        f: impl FnMut(A) -> B
    ) -> Self::Frame<B>;
}
Expand description

A single ‘frame’ containing values that can be mapped over via map_frame.

Motivation

Generally speaking, you won’t use this trait yourself. It’s used by the internal plumbing of crate::Collapsible and crate::Expandable to implement recursive traversals.

Implementing this trait

This trait is usually implemented for some marker token, because rust does not allow for implementing a trait for a partially applied type. That is, we can implement a trait for Option<usize> but we can’t implement a trait for just Option, because Option is a partially applied type.

For this reason, a common convention is to implement this trait using the uninhabited PartiallyApplied enum marker, eg

enum MyOption<A> {
    Some(A),
    None,
}

impl MappableFrame for MyOption<PartiallyApplied> {
    type Frame<X> = MyOption<X>;

    fn map_frame<A, B>(input: Self::Frame<A>, mut f: impl FnMut(A) -> B) -> Self::Frame<B> {
        match input {
            MyOption::Some(x) => MyOption::Some(f(x)),
            MyOption::None => MyOption::None,
        }
    }
}

Use

Here’s what mapping over a MyOption frame looks like in action:

let frame = MyOption::Some(1);
let mapped_frame = MyOption::<PartiallyApplied>::map_frame(frame, |n| n + 10);

assert_eq!(mapped_frame, MyOption::Some(11));

Required Associated Types§

source

type Frame<X>

the frame type that is mapped over by map_frame

Required Methods§

source

fn map_frame<A, B>( input: Self::Frame<A>, f: impl FnMut(A) -> B ) -> Self::Frame<B>

Apply some function f to each element inside a frame

Object Safety§

This trait is not object safe.

Implementors§