From a52bd4470b6d0cf84e971ff9c4a8fa8fc49b37f2 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 26 Sep 2020 12:17:16 +0200 Subject: [PATCH] bench with Options and usize, inline trait impls --- benches/linear.rs | 4 ++++ src/lib.rs | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/benches/linear.rs b/benches/linear.rs index 57bb845a..434a3722 100644 --- a/benches/linear.rs +++ b/benches/linear.rs @@ -28,6 +28,10 @@ pub fn benchmark(c: &mut Criterion) { b.iter(|| textwrap::fill(text, &options)); }); + group.bench_with_input(BenchmarkId::new("fill_usize", length), &text, |b, text| { + b.iter(|| textwrap::fill(text, *length)); + }); + #[cfg(feature = "hyphenation")] { use hyphenation::{Language, Load, Standard}; diff --git a/src/lib.rs b/src/lib.rs index d438cfd2..826f7e4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,36 +162,46 @@ impl<'indent> WrapOptions<'indent> for Options<'indent> { } impl<'indent> WrapOptions<'indent> for &Options<'indent> { + #[inline] fn width(&self) -> usize { self.width } + #[inline] fn initial_indent(&self) -> &'indent str { self.initial_indent } + #[inline] fn subsequent_indent(&self) -> &'indent str { self.subsequent_indent } + #[inline] fn break_words(&self) -> bool { self.break_words } + #[inline] fn split<'w>(&self, word: &'w str) -> Vec<(&'w str, &'w str, &'w str)> { self.splitter.split(word) } } impl<'a> WrapOptions<'a> for usize { + #[inline] fn width(&self) -> usize { *self } + #[inline] fn initial_indent(&self) -> &'a str { "" } + #[inline] fn subsequent_indent(&self) -> &'a str { "" } + #[inline] fn break_words(&self) -> bool { true } + #[inline] fn split<'w>(&self, word: &'w str) -> Vec<(&'w str, &'w str, &'w str)> { let s = HyphenSplitter; s.split(word)