From dc8d5d2f1eadb19d0351fa214400d7ec7ba31a20 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 2 Oct 2020 20:22:08 +0200 Subject: [PATCH] =?UTF-8?q?Small=20improvements=20=F0=9F=A7=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/align.rs | 2 +- src/parse/parser.rs | 6 ++++-- src/parse/resolve.rs | 4 ++-- src/parse/scanner.rs | 4 ---- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/library/align.rs b/src/library/align.rs index 350724f7a..d36108a48 100644 --- a/src/library/align.rs +++ b/src/library/align.rs @@ -35,7 +35,7 @@ pub async fn align(_: Span, mut args: DictValue, ctx: LayoutContext<'_>) -> Pass // if the alignment is `center` for a positional argument. Then we set // `deferred_center` to true and handle the situation once we know more. if let Some(axis) = axis { - if align.v.axis().map(|a| a != axis).unwrap_or(false) { + if align.v.axis().map_or(false, |a| a != axis) { error!( @f, align.span, "invalid alignment {} for {} axis", align.v, axis, diff --git a/src/parse/parser.rs b/src/parse/parser.rs index d34730c8c..041a61bc1 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -228,7 +228,7 @@ impl<'s> Parser<'s> { /// /// Returns `false` if there is no next token. pub fn check(&mut self, f: impl FnOnce(Token<'s>) -> bool) -> bool { - self.peek().map(f).unwrap_or(false) + self.peek().map_or(false, f) } /// Whether the end of the source string or group is reached. @@ -278,7 +278,9 @@ impl<'s> Parser<'s> { /// Set the position to the tokenizer's position and take the peeked token. fn bump(&mut self) -> Option> { self.pos = self.tokens.pos(); - self.peeked.take() + let token = self.peeked; + self.peeked = None; + token } } diff --git a/src/parse/resolve.rs b/src/parse/resolve.rs index 1b289b1e0..df5a5d419 100644 --- a/src/parse/resolve.rs +++ b/src/parse/resolve.rs @@ -89,12 +89,12 @@ fn trim_and_split_raw(raw: &str) -> (Vec, bool) { let is_whitespace = |line: &String| line.chars().all(char::is_whitespace); // Trims a sequence of whitespace followed by a newline at the start. - if lines.first().map(is_whitespace).unwrap_or(false) { + if lines.first().map_or(false, is_whitespace) { lines.remove(0); } // Trims a newline followed by a sequence of whitespace at the end. - if lines.last().map(is_whitespace).unwrap_or(false) { + if lines.last().map_or(false, is_whitespace) { lines.pop(); } diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs index b71079791..02562839d 100644 --- a/src/parse/scanner.rs +++ b/src/parse/scanner.rs @@ -32,8 +32,6 @@ impl<'s> Scanner<'s> { /// Returns whether the char was consumed. pub fn eat_if(&mut self, c: char) -> bool { // Don't decode the char twice through peek() and eat(). - // - // TODO: Benchmark this vs. the naive version. if self.iter.next() == Some(c) { self.index += c.len_utf8(); true @@ -71,8 +69,6 @@ impl<'s> Scanner<'s> { if f(c) { // Undo the previous `next()` without peeking all the time // during iteration. - // - // TODO: Benchmark this vs. the naive peeking version. self.reset(); break; }