CostFactory

Trait CostFactory 

Source
pub trait CostFactory: Debug {
    type CostType: Cost<Output = Self::CostType>;

    // Required methods
    fn text(&self, c: usize, l: usize) -> Self::CostType;
    fn newline(&self, i: usize) -> Self::CostType;
    fn limit(&self) -> usize;
}
Expand description

Trait for types that can assign costs to particular layouts of a document.

A custom cost factory can be used with validate_with_cost to customize how the costs of different layouts are determined.

In most cases, the DefaultCostFactory that is used by to_string() and validate will be sufficient.

§Requirements for cost factories

In addition to the requirements imposed on the Cost type itself, a cost factory’s operation must satisfy certain properties for the printer to behave correctly:

  • If c1 <= c2, then text(c1, l) <= text(c2, l): the cost of adding the same length of text must not decrease as the column moves to the right.
  • If i1 <= i2, then newline(i1) <= newline(i2): the cost of adding a newline must not decrease as the indentation level increases.
  • text(c, l1) + text(c + l1, l2) == text(c, l1 + l2): splitting or combining text at the same position must not affect its cost.

Required Associated Types§

Source

type CostType: Cost<Output = Self::CostType>

The type of cost values that the factory will assign.

Required Methods§

Source

fn text(&self, c: usize, l: usize) -> Self::CostType

Determine the cost of adding a slice of text to the document.

The text being added has length l and begins at column c.

Source

fn newline(&self, i: usize) -> Self::CostType

Determine the cost of adding a newline to the document.

There will be i spaces of indentation included after the newline.

Source

fn limit(&self) -> usize

The computation width limit.

If a layout the printer is attempting will exceed this width for a line, the printer will consider that layout “tainted”. A tainted layout will not be rejected outright. Instead, it will be set aside in case there are no valid untainted layouts. In that case, a tainted layout might be chosen, but it is not guaranteed to be the optimal one.

Implementors§