Skip to main content

Context

Struct Context 

Source
pub struct Context {
    pub mappers: HashMap<TypeId, Box<Mapper>>,
}
Expand description

Context for custom value transformations.

A context holds a collection of mappers keyed by the TypeId of the input type. When displaying a tree, the context is used to apply transformations to values before they are rendered.

§Example

use tree_display::Context;

let context = Context::new()
    .map(|s: &String| format!("'{}'", s))
    .map(|n: &i32| format!("{}", n * 2));

Fields§

§mappers: HashMap<TypeId, Box<Mapper>>

Map from TypeId to a mapper function for that type.

Implementations§

Source§

impl Context

Source

pub fn new() -> Self

Creates a new empty context with no mappers.

Source

pub fn map<T: 'static, R: Content + 'static>( self, f: impl Fn(&T) -> R + 'static, ) -> Self

Registers a mapper for a specific type.

The mapper function takes a reference to a value and returns another value that implements Content. The transformed value will be used when displaying values of type T in the tree.

To apply a mapper to a specific field in a struct or enum, use the #[tree(map)] attribute on that field when deriving TreeDisplay.

§Type Parameters
  • T: The input type to map from
  • R: The output type that implements Content
§Example
use tree_display::context::Context;

let context = Context::new()
    .map(|s: &String| format!("'{}'", s))
    .map(|n: &i32| format!("{}", n * 2));

Trait Implementations§

Source§

impl Default for Context

Source§

fn default() -> Context

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

Source§

type Proj<U: 'src> = U

Source§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

Source§

impl<T, S> SpanWrap<S> for T
where S: WrappingSpan<T>,

Source§

fn with_span(self, span: S) -> <S as WrappingSpan<Self>>::Spanned

Invokes WrappingSpan::make_wrapped to wrap an AST node in a span.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.