1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use ;
/// A measurement of the badness of a particular printing layout.
///
/// [`Doc`](crate::Doc) is generic on the `Cost` type it can be used with, since
/// a document can contain [`cost`](crate::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`](crate::text)/[`newline`](crate::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`](crate::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](CostFactory#requirements-for-cost-factories) for the functions
/// on the [`CostFactory`].
/// Trait for types that can assign costs to particular layouts of a document.
///
/// A custom cost factory can be used with
/// [`validate_with_cost`](crate::Doc::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`](crate::Doc::validate) will be sufficient.
///
/// ## Requirements for cost factories
///
/// In addition to the [requirements](Cost#requirements-for-costs) 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.
/// 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](crate::Doc) will use this cost factory
/// with a page width of 80.
/// * Formatting a [Doc](crate::Doc) using one of [Rust's formatting
/// macros](std::fmt#related-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`](crate::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`](crate::Doc::validate_with_cost).
/// The default cost type used for documents.
///
/// It is composed of two values: the badness and the number of newlines. The
/// badness quantifies how much the layout overflows the desired page width.
/// When this cost is used, the badness will be minimized first, then the number
/// of newlines will be minimized from there.
///
/// This cost is generally used with the [`DefaultCostFactory`], but it is valid
/// to use it with a custom cost factory if it serves your needs.
;