Cost

Trait Cost 

Source
pub trait Cost:
    Ord
    + Add
    + Clone
    + Debug { }
Expand description

A measurement of the badness of a particular printing layout.

Doc is generic on the Cost type it can be used with, since a document can contain cost nodes that increase the cost of certain paths.

When a document is being printed, costs are assigned by a CostFactory. This determines the badness of text/newline nodes based on their location in the layout. The printer will then try to minimize this cost overall.

A DefaultCost and DefaultCostFactory is provided, and Doc will default to using the DefaultCost if no generic parameter is given to it.

§Requirements for costs

A Cost must satisfy several properties for the printer to be able to use it correctly:

  • a <= b must be a total order. This requirement is imposed by the Ord trait.
  • a + b (via the Add trait) must be commutative and associative.
  • If a <= b and c <= d, then a + c <= b + d.

There are additional requirements for the functions on the CostFactory.

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§