Skip to content

Commit

Permalink
Remove unnecessary saturating casts
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 15, 2023
1 parent 88ec3a8 commit 65b0c38
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/alignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ impl HorizontalAlignment {
self,
renderer: &impl TextRenderer,
measurement: LineMeasurement,
) -> (u32, SpaceConfig) {
) -> (i32, SpaceConfig) {
let space_width = str_width(renderer, " ");
let space_config = SpaceConfig::new(space_width, None);
let remaining_space = measurement.max_line_width - measurement.width;
match self {
HorizontalAlignment::Left => (0, space_config),
HorizontalAlignment::Center => ((remaining_space + 1) / 2, space_config),
HorizontalAlignment::Right => (remaining_space, space_config),
HorizontalAlignment::Center => ((remaining_space as i32 + 1) / 2, space_config),
HorizontalAlignment::Right => (remaining_space as i32, space_config),
HorizontalAlignment::Justified => {
let space_count = measurement.space_count;
let space_info = if !measurement.last_line() && space_count != 0 {
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ impl LineCursor {
self.position -= abs;
Ok(by)
} else {
Err(-self.position.saturating_as::<i32>())
Err(-(self.position as i32))
}
} else {
let space = self.space().saturating_as();
let space = self.space() as i32;
if by <= space {
// Here we know by > 0, cast is safe
self.position += by as u32;
Expand Down
3 changes: 1 addition & 2 deletions src/rendering/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
style::TextBoxStyle,
utils::str_width,
};
use az::SaturatingAs;
use embedded_graphics::{
draw_target::DrawTarget,
geometry::Point,
Expand Down Expand Up @@ -175,7 +174,7 @@ where

let (left, space_config) = self.style.alignment.place_line(text_renderer, lm);

self.cursor.move_cursor(left.saturating_as()).ok();
self.cursor.move_cursor(left as i32).ok();

let mut render_element_handler = RenderElementHandler {
text_renderer,
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/line_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ where
handler.whitespace("\t", 0, moved)?
}

Ok(moved) | Err(moved) => handler.move_cursor(moved.saturating_as())?,
Ok(moved) | Err(moved) => handler.move_cursor(moved as i32)?,
}
Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ use crate::{
},
utils::str_width,
};
use az::SaturatingAs;
use embedded_graphics::{
pixelcolor::Rgb888,
text::{renderer::TextRenderer, LineHeight},
Expand Down Expand Up @@ -407,7 +406,7 @@ impl<'a, S: TextRenderer> ElementHandler for MeasureLineElementHandler<'a, S> {
}

fn move_cursor(&mut self, by: i32) -> Result<(), Self::Error> {
self.cursor = (self.cursor.saturating_as::<i32>() + by) as u32;
self.cursor = (self.cursor as i32 + by) as u32;

Ok(())
}
Expand Down
4 changes: 1 addition & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Misc utilities

use az::SaturatingAs;
use embedded_graphics::{
prelude::Point,
text::{renderer::TextRenderer, Baseline},
Expand All @@ -11,8 +10,7 @@ pub fn str_width(renderer: &impl TextRenderer, s: &str) -> u32 {
renderer
.measure_string(s, Point::zero(), Baseline::Top)
.next_position
.x
.saturating_as()
.x as u32
}

#[cfg(test)]
Expand Down

0 comments on commit 65b0c38

Please sign in to comment.