ManifestMut

Trait ManifestMut 

Source
pub trait ManifestMut<A> {
    type Error;
    type State;

    // Required methods
    fn init_state(&self) -> Self::State;
    fn manifest_mut(
        &self,
        ast: &A,
        state: &mut Self::State,
    ) -> Result<impl Display, Self::Error>;

    // Provided methods
    fn write_fmt_mut<W: Write>(
        &self,
        ast: &A,
        state: &mut Self::State,
        writer: W,
    ) -> Result<(), FmtRenderError<Self::Error>> { ... }
    fn write_io_mut<W: Write>(
        &self,
        ast: &A,
        state: &mut Self::State,
        writer: W,
    ) -> Result<(), IORenderError<Self::Error>> { ... }
}
Expand description

A manifest which can display an Ast using mutable state.

If you do not need mutable state, implement Manifest instead. There is a blanket implementation of ManifestMut<A> for every implementation of Manifest<A>.

§Render rules

The mutable state is initialized with ManifestMut::init_state before the template is rendered. This state is subsequently passed to each invocation of ManifestMut::manifest_mut, and then dropped after the final invocation.

The calls to manifest_mut are in the order in which expressions appear in the template.

§Example

Here is an example which replaces each (empty) expression with the index at which it occurred in the template.

use mufmt::{ManifestMut, Template};

struct ExprCounter;

impl ManifestMut<()> for ExprCounter {
    type Error = std::convert::Infallible;

    type State = usize;

    fn init_state(&self) -> Self::State {
        0
    }

    fn manifest_mut(
        &self,
        _: &(),
        state: &mut Self::State,
    ) -> Result<impl std::fmt::Display, Self::Error> {
        let res = *state;
        *state += 1;
        Ok(res)
    }
}

let template = Template::<&str, ()>::compile("{}, then {} and {}").unwrap();
assert_eq!(template.render(&ExprCounter).unwrap(), "0, then 1 and 2");

Required Associated Types§

Source

type Error

An error produced while manifesting.

Source

type State

Associated mutable state, which lasts for the duration of a single template.

Required Methods§

Source

fn init_state(&self) -> Self::State

Initialize mutable state before rendering the template.

Source

fn manifest_mut( &self, ast: &A, state: &mut Self::State, ) -> Result<impl Display, Self::Error>

Convert the Ast to a type which can be displayed.

Provided Methods§

Source

fn write_fmt_mut<W: Write>( &self, ast: &A, state: &mut Self::State, writer: W, ) -> Result<(), FmtRenderError<Self::Error>>

Write the Ast into a fmt::Write implementation.

Source

fn write_io_mut<W: Write>( &self, ast: &A, state: &mut Self::State, writer: W, ) -> Result<(), IORenderError<Self::Error>>

Write the Ast into a io::Write implementation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<A, M: Manifest<A>> ManifestMut<A> for M

A blanket implementation of ManifestMut<A> for every Manifest<A> implementation.

In particular, the rendering methods such as Template::render and Oneshot::render (which require a ManifestMut) can also be called with a Manifest implementation.

Source§

type Error = <M as Manifest<A>>::Error

Source§

type State = ()