pub trait Formatter<T> {
// Required method
fn format_object(&self, x: T) -> String;
}
Expand description
Formatter
is a trait for converting objects to string.
We need it because pretty-printing data structures often requires some
context. For instance, because values use value ids to point to other values,
we need a context to give us the mapping from value ids to values when pretty
printing. As the EvalContext
data structure handles such a mapping, we
implement the Formatter<ValueId>
trait for it.
Our way of implementing pretty-printing for data-structures while factorizing the code is as follows:
- for every data structure which requires formatting, we implement a
function
fn fmt_with_ctx(&self, ctx: T) -> String
, with proper trait constraints on the context type T. We implement this kind of functions for values, types, places, operands, rvalues, statements, etc, and the formatting trait constraints often require the context to implementFormatter
for a various set of indices (type variable index, variable index, type definition index, etc.). - later on, whenever we have a suitable context type (like
EvalContext
), we implement theFormatter
trait for all the index types we need, and then can easily implement it for all the above data-structures (values, types, places, etc.) by calling the appropriatefmt_with_ctx
functions. The advantage of using auxiliaryfmt_with_ctx
functions is that we can easily reuse those functions to implement pretty-printing with different contexts, without duplicating the “non-trivial” code.
Required Methods§
fn format_object(&self, x: T) -> String
Implementations on Foreign Types§
source§impl<C, T> Formatter<T> for &Cwhere
C: Formatter<T>,
impl<C, T> Formatter<T> for &Cwhere
C: Formatter<T>,
fn format_object(&self, x: T) -> String
Implementors§
impl<'a> Formatter<&ImplElem> for FmtCtx<'a>
impl<'a> Formatter<&FunDecl> for FmtCtx<'a>
impl<'a> Formatter<&GExprBody<Block>> for FmtCtx<'a>
impl<'a> Formatter<&GExprBody<Vector<BlockId, BlockData>>> for FmtCtx<'a>
impl<'a> Formatter<&GlobalDecl> for FmtCtx<'a>
impl<'a> Formatter<&TraitDecl> for FmtCtx<'a>
impl<'a> Formatter<&TraitImpl> for FmtCtx<'a>
impl<'a> Formatter<&Block> for FmtCtx<'a>
impl<'a> Formatter<&FunSig> for FmtCtx<'a>
impl<'a> Formatter<&RegionVar> for FmtCtx<'a>
impl<'a> Formatter<&Ty> for FmtCtx<'a>
impl<'a> Formatter<&TypeDecl> for FmtCtx<'a>
impl<'a> Formatter<&Vector<BlockId, BlockData>> for FmtCtx<'a>
impl<'a> Formatter<(DeBruijnId, RegionId)> for FmtCtx<'a>
impl<'a> Formatter<(TypeDeclId, Option<VariantId>, FieldId)> for FmtCtx<'a>
For struct/enum values: retrieve a field name
impl<'a> Formatter<(TypeDeclId, VariantId)> for FmtCtx<'a>
For enum values: List::Cons