rustdoc::formats::renderer

Trait FormatRenderer

Source
pub(crate) trait FormatRenderer<'tcx>: Sized {
    type ModuleData;

    const RUN_ON_MODULE: bool;

    // Required methods
    fn descr() -> &'static str;
    fn init(
        krate: Crate,
        options: RenderOptions,
        cache: Cache,
        tcx: TyCtxt<'tcx>,
    ) -> Result<(Self, Crate), Error>;
    fn save_module_data(&mut self) -> Self::ModuleData;
    fn restore_module_data(&mut self, info: Self::ModuleData);
    fn item(&mut self, item: Item) -> Result<(), Error>;
    fn mod_item_in(&mut self, item: &Item) -> Result<(), Error>;
    fn after_krate(&mut self) -> Result<(), Error>;
    fn cache(&self) -> &Cache;

    // Provided method
    fn mod_item_out(&mut self) -> Result<(), Error> { ... }
}
Expand description

Allows for different backends to rustdoc to be used with the run_format() function. Each backend renderer has hooks for initialization, documenting an item, entering and exiting a module, and cleanup/finalizing output.

Required Associated Constants§

Source

const RUN_ON_MODULE: bool

Whether to call item recursively for modules

This is true for html, and false for json. See #80664

Required Associated Types§

Source

type ModuleData

This associated type is the type where the current module information is stored.

For each module, we go through their items by calling for each item:

  1. save_module_data
  2. item
  3. restore_module_data

This is because the item method might update information in self (for example if the child is a module). To prevent it from impacting the other children of the current module, we need to reset the information between each call to item by using restore_module_data.

Required Methods§

Source

fn descr() -> &'static str

Gives a description of the renderer. Used for performance profiling.

Source

fn init( krate: Crate, options: RenderOptions, cache: Cache, tcx: TyCtxt<'tcx>, ) -> Result<(Self, Crate), Error>

Sets up any state required for the renderer. When this is called the cache has already been populated.

Source

fn save_module_data(&mut self) -> Self::ModuleData

This method is called right before call Self::item. This method returns a type containing information that needs to be reset after the Self::item method has been called with the Self::restore_module_data method.

In short it goes like this:

let reset_data = renderer.save_module_data();
renderer.item(item)?;
renderer.restore_module_data(reset_data);
Source

fn restore_module_data(&mut self, info: Self::ModuleData)

Used to reset current module’s information.

Source

fn item(&mut self, item: Item) -> Result<(), Error>

Renders a single non-module item. This means no recursive sub-item rendering is required.

Source

fn mod_item_in(&mut self, item: &Item) -> Result<(), Error>

Renders a module (should not handle recursing into children).

Source

fn after_krate(&mut self) -> Result<(), Error>

Post processing hook for cleanup and dumping output to files.

Source

fn cache(&self) -> &Cache

Provided Methods§

Source

fn mod_item_out(&mut self) -> Result<(), Error>

Runs after recursively rendering all sub-items of a module.

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<'tcx> FormatRenderer<'tcx> for Context<'tcx>

Generates the documentation for crate into the directory dst

Source§

impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx>