pub trait ManifestMut<A> {
type Error;
type State<'s>;
// 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<'a> = 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§
Required Methods§
Sourcefn init_state(&self) -> Self::State<'_>
fn init_state(&self) -> Self::State<'_>
Initialize mutable state before rendering the template.
Provided Methods§
Sourcefn write_fmt_mut<W: Write>(
&self,
ast: &A,
state: &mut Self::State<'_>,
writer: W,
) -> Result<(), FmtRenderError<Self::Error>>
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.
Sourcefn write_io_mut<W: Write>(
&self,
ast: &A,
state: &mut Self::State<'_>,
writer: W,
) -> Result<(), IORenderError<Self::Error>>
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.
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.