charon_lib::pretty::formatter

Trait Formatter

source
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 implement Formatter 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 the Formatter 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 appropriate fmt_with_ctx functions. The advantage of using auxiliary fmt_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§

source

fn format_object(&self, x: T) -> String

Implementations on Foreign Types§

source§

impl<C, T> Formatter<T> for &C
where C: Formatter<T>,

source§

fn format_object(&self, x: T) -> String

Implementors§

source§

impl<'a> Formatter<&ImplElem> for FmtCtx<'a>

source§

impl<'a> Formatter<&FunDecl> for FmtCtx<'a>

source§

impl<'a> Formatter<&GExprBody<Block>> for FmtCtx<'a>

source§

impl<'a> Formatter<&GExprBody<Vector<BlockId, BlockData>>> for FmtCtx<'a>

source§

impl<'a> Formatter<&GlobalDecl> for FmtCtx<'a>

source§

impl<'a> Formatter<&TraitDecl> for FmtCtx<'a>

source§

impl<'a> Formatter<&TraitImpl> for FmtCtx<'a>

source§

impl<'a> Formatter<&Block> for FmtCtx<'a>

source§

impl<'a> Formatter<&FunSig> for FmtCtx<'a>

source§

impl<'a> Formatter<&RegionVar> for FmtCtx<'a>

source§

impl<'a> Formatter<&Ty> for FmtCtx<'a>

source§

impl<'a> Formatter<&TypeDecl> for FmtCtx<'a>

source§

impl<'a> Formatter<&Vector<BlockId, BlockData>> for FmtCtx<'a>

source§

impl<'a> Formatter<(TypeDeclId, Option<VariantId>, FieldId)> for FmtCtx<'a>

For struct/enum values: retrieve a field name

source§

impl<'a> Formatter<(TypeDeclId, VariantId)> for FmtCtx<'a>

For enum values: List::Cons

source§

impl<'a> Formatter<(DeBruijnId, RegionId)> for FmtCtx<'a>

source§

impl<'a> Formatter<AnyTransId> for FmtCtx<'a>

source§

impl<'a> Formatter<BodyId> for FmtCtx<'a>

source§

impl<'a> Formatter<FunDeclId> for FmtCtx<'a>

source§

impl<'a> Formatter<GlobalDeclId> for FmtCtx<'a>

source§

impl<'a> Formatter<TraitDeclId> for FmtCtx<'a>

source§

impl<'a> Formatter<TraitImplId> for FmtCtx<'a>

source§

impl<'a> Formatter<TypeDeclId> for FmtCtx<'a>

source§

impl<'a> Formatter<ConstGenericVarId> for FmtCtx<'a>

source§

impl<'a> Formatter<TraitClauseId> for FmtCtx<'a>

source§

impl<'a> Formatter<TypeVarId> for FmtCtx<'a>

source§

impl<'a> Formatter<VarId> for FmtCtx<'a>