From a7b29f97276fcb19d8109bca790169ebad0334b6 Mon Sep 17 00:00:00 2001 From: Nicolas <> Date: Wed, 13 Apr 2022 19:25:33 +0200 Subject: [PATCH] Small optimization --- src/char.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/char.rs b/src/char.rs index 3cb54b4..4bac492 100644 --- a/src/char.rs +++ b/src/char.rs @@ -11,6 +11,7 @@ pub trait CharSampler { } pub struct SimpleCharSampler { + len: f32, chars: String } @@ -18,7 +19,9 @@ impl SimpleCharSampler { /// # Arguments /// * `chars`: The characters ordered by brightness. pub fn new(chars: String) -> Self { - Self { chars } + let len = chars.chars().count() as f32; + + Self { chars, len } } } @@ -26,7 +29,7 @@ impl CharSampler for SimpleCharSampler { fn sample(&self, level: f32) -> char { assert!(0.0 <= level && level < 1.0); - let index = level * self.chars.chars().count() as f32; + let index = level * self.len; self.chars.chars().nth(index as usize).unwrap() }