Skip to content

Commit 13d5815

Browse files
Use From::from for lossless casts where possible
1 parent 2cbd134 commit 13d5815

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/graphics/color.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ impl From<u32> for Color {
109109

110110
impl From<Color> for u32 {
111111
fn from(src: Color) -> Self {
112-
((src.r as u32) << 24) | ((src.g as u32) << 16) | ((src.b as u32) << 8) | (src.a as u32)
112+
(u32::from(src.r) << 24)
113+
| (u32::from(src.g) << 16)
114+
| (u32::from(src.b) << 8)
115+
| u32::from(src.a)
113116
}
114117
}
115118

@@ -161,10 +164,10 @@ impl Mul for Color {
161164
/// For each `X` in `rgba`, `result.X = a.X * b.X / 255`.
162165
#[expect(clippy::cast_possible_truncation)]
163166
fn mul(self, other: Color) -> Color {
164-
let (r1, r2) = (self.r as u16, other.r as u16);
165-
let (g1, g2) = (self.g as u16, other.g as u16);
166-
let (b1, b2) = (self.b as u16, other.b as u16);
167-
let (a1, a2) = (self.a as u16, other.a as u16);
167+
let (r1, r2) = (u16::from(self.r), u16::from(other.r));
168+
let (g1, g2) = (u16::from(self.g), u16::from(other.g));
169+
let (b1, b2) = (u16::from(self.b), u16::from(other.b));
170+
let (a1, a2) = (u16::from(self.a), u16::from(other.a));
168171
Self {
169172
r: (r1 * r2 / 255) as u8,
170173
g: (g1 * g2 / 255) as u8,

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
clippy::cast_sign_loss,
1616
clippy::unwrap_used,
1717
clippy::unreadable_literal,
18-
clippy::ptr_as_ptr
18+
clippy::ptr_as_ptr,
19+
clippy::cast_lossless
1920
)]
2021

2122
extern crate link_cplusplus;

0 commit comments

Comments
 (0)