pub trait ToTokens {
type Printer: Printer;
// Required method
fn write(&self, printer: &mut Self::Printer);
// Provided method
fn to_string_formatted(&self) -> String
where Self::Printer: Default { ... }
}Expand description
Trait for converting AST nodes back to text.
ToTokens is the inverse of Parse - it converts parsed structures
back into their textual representation. This enables:
- Code formatting / pretty-printing
- Source-to-source transformations
- Round-trip testing (parse → modify → print)
§Associated Types
Printer: The printer implementation for formatting output
§Required Methods
write(&self, printer): Write this value to the printer
§Provided Methods
to_string_formatted(): Convenience method for getting a String
§Example
ⓘ
use synkit::{ToTokens, Printer};
struct BinaryExpr {
left: Box<Expr>,
op: &'static str,
right: Box<Expr>,
}
impl ToTokens for BinaryExpr {
type Printer = MyPrinter;
fn write(&self, p: &mut Self::Printer) {
self.left.write(p);
p.write(" ");
p.write(self.op);
p.write(" ");
self.right.write(p);
}
}§Blanket Implementations
Option<T>: Writes nothing forNone, delegates forSomeBox<T>: Delegates to inner valueVec<T>: Writes each element in sequence&T: Delegates to referenced value