DefaultCostFactory

Struct DefaultCostFactory 

Source
pub struct DefaultCostFactory { /* private fields */ }
Expand description

The default strategy for assigning costs to layouts.

This cost factory uses DefaultCost as its cost type. Its documentation has more information about the components that make up the cost.

The default cost factory has two configurable parameters:

  • page_width: Required. The desired width of the document. Layouts that exceed this width will have a cost imposed based on how much they exceed it. The printer will choose a layout that fits within this width unless it is not possible to do so.
  • computation_width: Optional. A more extreme width constraint. Documents that extend beyond this width will be “tainted”, and will not be explored further by the printer until it exhausts all untainted options. This constraint improves the performance of the printer. If not provided, it will default to 1.2 * page_width.

This cost factory is used in the following situations:

  • Calling to_string() on a Doc will use this cost factory with a page width of 80.
  • Formatting a Doc using one of Rust’s formatting macros will use this cost factory. If a width parameter is given to the macro, it will be used as the page width. Otherwise, the default 80 characters will be used
  • Calling Doc::validate will use this cost factory with the given page width.

None of these allow overriding the computation width, as it isn’t usually necessary. If you want to use a custom computation width, you can use Self::new and Doc::validate_with_cost.

Implementations§

Source§

impl DefaultCostFactory

Source

pub fn new(page_width: usize, computation_width: Option<usize>) -> Self

Create a new cost factory with the given parameters.

Trait Implementations§

Source§

impl CostFactory for DefaultCostFactory

Source§

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

Adds 1 to the second component of the cost.

The indentation level is ignored in this cost factory.

Source§

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

Adds to the first component of the cost if the page width is exceeded.

If the text will not exceed the page width, no cost is imposed.

Source§

type CostType = DefaultCost

The type of cost values that the factory will assign.
Source§

fn limit(&self) -> usize

The computation width limit. Read more
Source§

impl Debug for DefaultCostFactory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.