Skip to content

Commit

Permalink
janitor: Remove some unnecessary conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger committed Jan 16, 2023
1 parent 3e1ebbb commit c09e72c
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion examples/slide_puzzle/main.rs
Expand Up @@ -23,7 +23,7 @@ fn shuffle() -> Vec<i8> {
inversions += positions[x + 1..].iter().filter(|x| **x >= 0 && **x < v).count();
}
//((blank on odd row from bottom) == (#inversions even))
let blank_row = positions.iter().position(|x| *x == -1).unwrap() as usize / 4;
let blank_row = positions.iter().position(|x| *x == -1).unwrap() / 4;
inversions % 2 != blank_row % 2
}

Expand Down
2 changes: 1 addition & 1 deletion internal/backends/qt/qt_accessible.rs
Expand Up @@ -23,7 +23,7 @@ use std::pin::Pin;
const NAME: u32 = QAccessible_Text_Name;
const DESCRIPTION: u32 = QAccessible_Text_Description;
const VALUE: u32 = QAccessible_Text_Value;
const CHECKED: u32 = QAccessible_Text_UserText as u32;
const CHECKED: u32 = QAccessible_Text_UserText;
const VALUE_MINIMUM: u32 = CHECKED + 1;
const VALUE_MAXIMUM: u32 = VALUE_MINIMUM + 1;
const VALUE_STEP: u32 = VALUE_MAXIMUM + 1;
Expand Down
10 changes: 5 additions & 5 deletions internal/backends/qt/qt_widgets/slider.rs
Expand Up @@ -123,9 +123,9 @@ impl Item for NativeSlider {
) -> InputEventResult {
let size: qttypes::QSize = get_size!(self);
let enabled = self.enabled();
let value = self.value() as f32;
let min = self.minimum() as f32;
let max = self.maximum() as f32;
let value = self.value();
let min = self.minimum();
let max = self.maximum();
let mut data = self.data();
let active_controls = data.active_controls;
let pressed: bool = data.pressed != 0;
Expand Down Expand Up @@ -157,7 +157,7 @@ impl Item for NativeSlider {
InputEventResult::EventIgnored
}
MouseEvent::Pressed { position: pos, button: PointerEventButton::Left } => {
data.pressed_x = pos.x as f32;
data.pressed_x = pos.x;
data.pressed = 1;
data.pressed_val = value;
InputEventResult::GrabMouse
Expand All @@ -170,7 +170,7 @@ impl Item for NativeSlider {
if data.pressed != 0 {
// FIXME: use QStyle::subControlRect to find out the actual size of the groove
let new_val = data.pressed_val
+ ((pos.x as f32) - data.pressed_x) * (max - min) / size.width as f32;
+ ((pos.x) - data.pressed_x) * (max - min) / size.width as f32;
let new_val = new_val.max(min).min(max);
self.value.set(new_val);
Self::FIELD_OFFSETS.changed.apply_pin(self).call(&(new_val,));
Expand Down
12 changes: 6 additions & 6 deletions internal/backends/winit/glwindow.rs
Expand Up @@ -277,8 +277,8 @@ impl<Renderer: WinitCompatibleRenderer + 'static> WindowAdapterSealed for GLWind
return;
}

let mut width = window_item.width().get() as f32;
let mut height = window_item.height().get() as f32;
let mut width = window_item.width().get();
let mut height = window_item.height().get();

let mut must_resize = false;

Expand Down Expand Up @@ -336,10 +336,10 @@ impl<Renderer: WinitCompatibleRenderer + 'static> WindowAdapterSealed for GLWind
}

if (constraints_horizontal, constraints_vertical) != self.constraints() {
let min_width = constraints_horizontal.min.min(constraints_horizontal.max) as f32;
let min_height = constraints_vertical.min.min(constraints_vertical.max) as f32;
let max_width = constraints_horizontal.max.max(constraints_horizontal.min) as f32;
let max_height = constraints_vertical.max.max(constraints_vertical.min) as f32;
let min_width = constraints_horizontal.min.min(constraints_horizontal.max);
let min_height = constraints_vertical.min.min(constraints_vertical.max);
let max_width = constraints_horizontal.max.max(constraints_horizontal.min);
let max_height = constraints_vertical.max.max(constraints_vertical.min);

let sf = self.window.scale_factor();

Expand Down
2 changes: 1 addition & 1 deletion internal/backends/winit/renderer/femtovg/itemrenderer.rs
Expand Up @@ -440,7 +440,7 @@ impl<'a> ItemRenderer for GLItemRenderer<'a> {
.glyphs
.iter()
.find_map(|glyph| {
if glyph.byte_index == (cursor_pos as usize - start) {
if glyph.byte_index == (cursor_pos - start) {
Some(glyph.x)
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/expression_tree.rs
Expand Up @@ -1534,7 +1534,7 @@ pub fn pretty_print(f: &mut dyn std::fmt::Write, expression: &Expression) -> std
}
write!(f, ")")
}
Expression::EnumerationValue(e) => match e.enumeration.values.get(e.value as usize) {
Expression::EnumerationValue(e) => match e.enumeration.values.get(e.value) {
Some(val) => write!(f, "{}.{}", e.enumeration.name, val),
None => write!(f, "{}.{}", e.enumeration.name, e.value),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/core/item_tree.rs
Expand Up @@ -270,7 +270,7 @@ impl ItemRc {
crate::item_tree::ItemTreeNode::Item { .. } => {
panic!("Got an Item, expected a repeater!")
}
crate::item_tree::ItemTreeNode::DynamicTree { index, .. } => *index as usize,
crate::item_tree::ItemTreeNode::DynamicTree { index, .. } => *index,
};

let range = parent_ref_pin.as_ref().get_subtree_range(subtree_index);
Expand Down
7 changes: 3 additions & 4 deletions internal/core/layout.rs
Expand Up @@ -180,7 +180,7 @@ mod grid_internal {
let max_grow = data
.iter()
.filter(|it| A::can_grow(it) > 0 as _)
.map(|it| A::can_grow(it) as f32 / actual_stretch(it.stretch))
.map(|it| A::can_grow(it) / actual_stretch(it.stretch))
.min_by(order_coord)?;

let current_size: Coord = data
Expand All @@ -191,14 +191,13 @@ mod grid_internal {

//let to_distribute = size_without_spacing - (size_cannot_grow + current_size);
let to_distribute =
A::to_distribute(size_without_spacing, size_cannot_grow + current_size) as f32;
A::to_distribute(size_without_spacing, size_cannot_grow + current_size);
if to_distribute <= 0. || max_grow <= 0. {
return Some(());
}

let grow = if total_stretch <= 0. {
to_distribute
/ (data.iter().filter(|it| A::can_grow(it) > 0 as _).count() as Coord) as f32
to_distribute / (data.iter().filter(|it| A::can_grow(it) > 0 as _).count() as Coord)
} else {
to_distribute / total_stretch
}
Expand Down
3 changes: 1 addition & 2 deletions internal/core/properties/properties_animations.rs
Expand Up @@ -60,8 +60,7 @@ impl<T: InterpolatedPropertyValue + Clone> PropertyValueAnimationData<T> {
// wrap around
current_iteration += time_progress / duration;
time_progress %= duration;
self.start_time =
new_tick - core::time::Duration::from_millis(time_progress as u64);
self.start_time = new_tick - core::time::Duration::from_millis(time_progress);
}

if (self.details.iteration_count < 0.)
Expand Down
2 changes: 1 addition & 1 deletion internal/core/software_renderer.rs
Expand Up @@ -1176,7 +1176,7 @@ impl<'a, T: ProcessScene> crate::item_rendering::ItemRenderer for SceneBuilder<'
let radius = rect.border_radius();
// FIXME: gradients
let color = self.alpha_color(&rect.background());
let border_color = if border.get() as f32 > 0.01 {
let border_color = if border.get() > 0.01 {
self.alpha_color(&rect.border_color())
} else {
Color::default()
Expand Down
16 changes: 8 additions & 8 deletions internal/core/software_renderer/draw_functions.rs
Expand Up @@ -170,10 +170,10 @@ pub(super) fn draw_rounded_rectangle_line(
}
let c = if border == Shifted(0) { rr.inner_color } else { rr.border_color };
let col = PremultipliedRgbaColor {
alpha: (((c.alpha as u32) * cov as u32) / 255) as u8,
red: (((c.red as u32) * cov as u32) / 255) as u8,
green: (((c.green as u32) * cov as u32) / 255) as u8,
blue: (((c.blue as u32) * cov as u32) / 255) as u8,
alpha: (((c.alpha as u32) * cov) / 255) as u8,
red: (((c.red as u32) * cov) / 255) as u8,
green: (((c.green as u32) * cov) / 255) as u8,
blue: (((c.blue as u32) * cov) / 255) as u8,
};
line_buffer[pos_x + x].blend(col);
},
Expand Down Expand Up @@ -253,10 +253,10 @@ pub(super) fn draw_rounded_rectangle_line(
}
let c = if border == Shifted(0) { rr.inner_color } else { rr.border_color };
let col = PremultipliedRgbaColor {
alpha: (((c.alpha as u32) * (255 - cov) as u32) / 255) as u8,
red: (((c.red as u32) * (255 - cov) as u32) / 255) as u8,
green: (((c.green as u32) * (255 - cov) as u32) / 255) as u8,
blue: (((c.blue as u32) * (255 - cov) as u32) / 255) as u8,
alpha: (((c.alpha as u32) * (255 - cov)) / 255) as u8,
red: (((c.red as u32) * (255 - cov)) / 255) as u8,
green: (((c.green as u32) * (255 - cov)) / 255) as u8,
blue: (((c.blue as u32) * (255 - cov)) / 255) as u8,
};
line_buffer[pos_x + x].blend(col);
});
Expand Down
2 changes: 1 addition & 1 deletion internal/core/software_renderer/fonts/pixelfont.rs
Expand Up @@ -99,7 +99,7 @@ impl TextShaper for PixelFont {
.map(|char_map_index| {
let glyph_index =
self.bitmap_font.character_map[char_map_index].glyph_index as usize;
let bitmap_glyph = &self.glyphs.glyph_data[glyph_index as usize];
let bitmap_glyph = &self.glyphs.glyph_data[glyph_index];
let x_advance = PhysicalLength::new(bitmap_glyph.x_advance);
Glyph {
glyph_id: Some(Self::glyph_index_to_glyph_id(glyph_index)),
Expand Down
2 changes: 1 addition & 1 deletion tools/lsp/server_loop.rs
Expand Up @@ -1044,7 +1044,7 @@ fn get_document_and_offset<'a>(
pos: &'a Position,
) -> Option<(&'a i_slint_compiler::object_tree::Document, u32)> {
let o = document_cache.newline_offsets.get(&text_document_uri)?.get(pos.line as usize)?
+ pos.character as u32;
+ pos.character;

let doc = document_cache.documents.get_document(&text_document_uri.to_file_path().ok()?)?;
doc.node.as_ref()?.text_range().contains(o.into()).then(|| (doc, o))
Expand Down

0 comments on commit c09e72c

Please sign in to comment.