Skip to content

Commit

Permalink
Lazily mutate self when parsing
Browse files Browse the repository at this point in the history
This ensures that a partial parse does not change anything.
  • Loading branch information
jhpratt committed Oct 18, 2021
1 parent 1af1448 commit f850171
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/parsing/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,21 @@ impl Parsed {

/// Parse a sequence of [`FormatItem`]s, mutating the struct. The remaining input is returned as
/// the `Ok` value.
///
/// This method will fail if any of the contained [`FormatItem`]s fail to parse. `self` will not
/// be mutated in this instance.
pub fn parse_items<'a>(
&mut self,
mut input: &'a [u8],
items: &[FormatItem<'_>],
) -> Result<&'a [u8], error::ParseFromDescription> {
// Make a copy that we can mutate. It will only be set to the user's copy if everything
// succeeds.
let mut this = *self;
for item in items {
input = self.parse_item(input, item)?;
input = this.parse_item(input, item)?;
}
*self = this;
Ok(input)
}

Expand Down

0 comments on commit f850171

Please sign in to comment.