Skip to content

Commit 9aeb36b

Browse files
authored
Document precondition for RangedStyleBuilder::push_default (#439)
PR mostly exists to test new branch protection, but is useful on its own. As a note for review, this code is entirely `pub(crate)`.
1 parent 886b271 commit 9aeb36b

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

parley/src/resolve/range.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ impl<B: Brush> Default for RangedStyleBuilder<B> {
2222
Self {
2323
properties: vec![],
2424
root_style: ResolvedStyle::default(),
25-
len: !0,
25+
// We use `usize::MAX` as a sentinel that `begin` hasn't been called.
26+
// This is required (rather than requiring the root style in the constructor)
27+
// as we want to support re-using this value.
28+
len: usize::MAX,
2629
}
2730
}
2831
}
@@ -38,21 +41,35 @@ impl<B: Brush> RangedStyleBuilder<B> {
3841
}
3942

4043
/// Change a property of the root style, which covers the full range of text.
44+
///
45+
/// # Panics
46+
///
47+
/// If [`begin`](Self::begin) has not been called before using this method.
4148
pub(crate) fn push_default(&mut self, property: ResolvedProperty<B>) {
42-
assert!(self.len != !0);
49+
assert!(
50+
self.len != usize::MAX,
51+
"Internal error: Must call `begin` before setting properties on a `RangedStyleBuilder`."
52+
);
4353
self.root_style.apply(property);
4454
}
4555

4656
/// Override a property for the specified range of text.
57+
///
58+
/// # Panics
59+
///
60+
/// If [`begin`](Self::begin) has not been called before using this method.
4761
pub(crate) fn push(&mut self, property: ResolvedProperty<B>, range: impl RangeBounds<usize>) {
48-
assert!(self.len != !0);
62+
assert!(
63+
self.len != usize::MAX,
64+
"Internal error: Must call `begin` before setting properties on a `RangedStyleBuilder`."
65+
);
4966
let range = resolve_range(range, self.len);
5067
self.properties.push(RangedProperty { property, range });
5168
}
5269

5370
/// Computes the sequence of ranged styles.
5471
pub(crate) fn finish(&mut self, styles: &mut Vec<RangedStyle<B>>) {
55-
if self.len == !0 {
72+
if self.len == usize::MAX {
5673
self.properties.clear();
5774
self.root_style = ResolvedStyle::default();
5875
return;
@@ -127,7 +144,7 @@ impl<B: Brush> RangedStyleBuilder<B> {
127144

128145
self.properties.clear();
129146
self.root_style = ResolvedStyle::default();
130-
self.len = !0;
147+
self.len = usize::MAX;
131148
}
132149
}
133150

0 commit comments

Comments
 (0)