diff --git a/emath/src/pos2.rs b/emath/src/pos2.rs index 5833bb74..09b5c02b 100644 --- a/emath/src/pos2.rs +++ b/emath/src/pos2.rs @@ -111,47 +111,57 @@ impl Pos2 { } } + #[inline] pub fn distance(self, other: Self) -> f32 { (self - other).length() } + #[inline] pub fn distance_sq(self, other: Self) -> f32 { (self - other).length_sq() } + #[inline(always)] pub fn floor(self) -> Self { pos2(self.x.floor(), self.y.floor()) } + #[inline(always)] pub fn round(self) -> Self { pos2(self.x.round(), self.y.round()) } + #[inline(always)] pub fn ceil(self) -> Self { pos2(self.x.ceil(), self.y.ceil()) } /// True if all members are also finite. + #[inline(always)] pub fn is_finite(self) -> bool { self.x.is_finite() && self.y.is_finite() } /// True if any member is NaN. + #[inline(always)] pub fn any_nan(self) -> bool { self.x.is_nan() || self.y.is_nan() } #[must_use] + #[inline] pub fn min(self, other: Self) -> Self { pos2(self.x.min(other.x), self.y.min(other.y)) } #[must_use] + #[inline] pub fn max(self, other: Self) -> Self { pos2(self.x.max(other.x), self.y.max(other.y)) } #[must_use] + #[inline] pub fn clamp(self, min: Self, max: Self) -> Self { Self { x: self.x.clamp(min.x, max.x), @@ -163,6 +173,7 @@ impl Pos2 { impl std::ops::Index for Pos2 { type Output = f32; + #[inline(always)] fn index(&self, index: usize) -> &f32 { match index { 0 => &self.x, @@ -173,6 +184,7 @@ impl std::ops::Index for Pos2 { } impl std::ops::IndexMut for Pos2 { + #[inline(always)] fn index_mut(&mut self, index: usize) -> &mut f32 { match index { 0 => &mut self.x, diff --git a/emath/src/vec2.rs b/emath/src/vec2.rs index 77f9f681..9ce16ea2 100644 --- a/emath/src/vec2.rs +++ b/emath/src/vec2.rs @@ -149,53 +149,63 @@ impl Vec2 { } #[must_use] + #[inline(always)] pub fn floor(self) -> Self { vec2(self.x.floor(), self.y.floor()) } #[must_use] + #[inline(always)] pub fn round(self) -> Self { vec2(self.x.round(), self.y.round()) } #[must_use] + #[inline(always)] pub fn ceil(self) -> Self { vec2(self.x.ceil(), self.y.ceil()) } /// True if all members are also finite. + #[inline(always)] pub fn is_finite(self) -> bool { self.x.is_finite() && self.y.is_finite() } /// True if any member is NaN. + #[inline(always)] pub fn any_nan(self) -> bool { self.x.is_nan() || self.y.is_nan() } #[must_use] + #[inline] pub fn min(self, other: Self) -> Self { vec2(self.x.min(other.x), self.y.min(other.y)) } #[must_use] + #[inline] pub fn max(self, other: Self) -> Self { vec2(self.x.max(other.x), self.y.max(other.y)) } /// Returns the minimum of `self.x` and `self.y`. #[must_use] + #[inline(always)] pub fn min_elem(self) -> f32 { self.x.min(self.y) } /// Returns the maximum of `self.x` and `self.y`. + #[inline(always)] #[must_use] pub fn max_elem(self) -> f32 { self.x.max(self.y) } #[must_use] + #[inline] pub fn clamp(self, min: Self, max: Self) -> Self { Self { x: self.x.clamp(min.x, max.x), @@ -207,6 +217,7 @@ impl Vec2 { impl std::ops::Index for Vec2 { type Output = f32; + #[inline(always)] fn index(&self, index: usize) -> &f32 { match index { 0 => &self.x, @@ -217,6 +228,7 @@ impl std::ops::Index for Vec2 { } impl std::ops::IndexMut for Vec2 { + #[inline(always)] fn index_mut(&mut self, index: usize) -> &mut f32 { match index { 0 => &mut self.x, diff --git a/epaint/src/color.rs b/epaint/src/color.rs index 317081ee..13de4bfe 100644 --- a/epaint/src/color.rs +++ b/epaint/src/color.rs @@ -17,12 +17,15 @@ pub struct Color32(pub(crate) [u8; 4]); impl std::ops::Index for Color32 { type Output = u8; + + #[inline(always)] fn index(&self, index: usize) -> &u8 { &self.0[index] } } impl std::ops::IndexMut for Color32 { + #[inline(always)] fn index_mut(&mut self, index: usize) -> &mut u8 { &mut self.0[index] } @@ -46,15 +49,18 @@ impl Color32 { pub const LIGHT_BLUE: Color32 = Color32::from_rgb(140, 160, 255); pub const GOLD: Color32 = Color32::from_rgb(255, 215, 0); + #[inline(always)] pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self { Self([r, g, b, 255]) } + #[inline(always)] pub const fn from_rgb_additive(r: u8, g: u8, b: u8) -> Self { Self([r, g, b, 0]) } /// From `sRGBA` with premultiplied alpha. + #[inline(always)] pub const fn from_rgba_premultiplied(r: u8, g: u8, b: u8, a: u8) -> Self { Self([r, g, b, a]) } @@ -84,10 +90,12 @@ impl Color32 { Self([r, g, b, a]) } + #[inline(always)] pub const fn from_gray(l: u8) -> Self { Self([l, l, l, 255]) } + #[inline(always)] pub const fn from_black_alpha(a: u8) -> Self { Self([0, 0, 0, a]) } @@ -96,6 +104,7 @@ impl Color32 { Rgba::from_white_alpha(linear_f32_from_linear_u8(a)).into() } + #[inline(always)] pub const fn from_additive_luminance(l: u8) -> Self { Self([l, l, l, 0]) } @@ -131,6 +140,7 @@ impl Color32 { } /// Returns an additive version of self + #[inline(always)] pub fn additive(self) -> Self { let [r, g, b, _] = self.to_array(); Self([r, g, b, 0]) @@ -166,12 +176,15 @@ pub struct Rgba(pub(crate) [f32; 4]); impl std::ops::Index for Rgba { type Output = f32; + + #[inline(always)] fn index(&self, index: usize) -> &f32 { &self.0[index] } } impl std::ops::IndexMut for Rgba { + #[inline(always)] fn index_mut(&mut self, index: usize) -> &mut f32 { &mut self.0[index] } @@ -185,14 +198,17 @@ impl Rgba { pub const GREEN: Rgba = Rgba::from_rgb(0.0, 1.0, 0.0); pub const BLUE: Rgba = Rgba::from_rgb(0.0, 0.0, 1.0); + #[inline(always)] pub const fn from_rgba_premultiplied(r: f32, g: f32, b: f32, a: f32) -> Self { Self([r, g, b, a]) } + #[inline(always)] pub const fn from_rgb(r: f32, g: f32, b: f32) -> Self { Self([r, g, b, 1.0]) } + #[inline(always)] pub const fn from_gray(l: f32) -> Self { Self([l, l, l, 1.0]) } @@ -204,18 +220,21 @@ impl Rgba { } /// Transparent black + #[inline(always)] pub fn from_black_alpha(a: f32) -> Self { debug_assert!(0.0 <= a && a <= 1.0); Self([0.0, 0.0, 0.0, a]) } /// Transparent white + #[inline(always)] pub fn from_white_alpha(a: f32) -> Self { debug_assert!(0.0 <= a && a <= 1.0); Self([a, a, a, a]) } /// Return an additive version of this color (alpha = 0) + #[inline(always)] pub fn additive(self) -> Self { let [r, g, b, _] = self.0; Self([r, g, b, 0.0]) @@ -253,6 +272,7 @@ impl Rgba { } /// How perceptually intense (bright) is the color? + #[inline] pub fn intensity(&self) -> f32 { 0.3 * self.r() + 0.59 * self.g() + 0.11 * self.b() } @@ -336,10 +356,10 @@ impl std::ops::Mul for f32 { impl From for Rgba { fn from(srgba: Color32) -> Rgba { Rgba([ - linear_f32_from_gamma_u8(srgba[0]), - linear_f32_from_gamma_u8(srgba[1]), - linear_f32_from_gamma_u8(srgba[2]), - linear_f32_from_linear_u8(srgba[3]), + linear_f32_from_gamma_u8(srgba.0[0]), + linear_f32_from_gamma_u8(srgba.0[1]), + linear_f32_from_gamma_u8(srgba.0[2]), + linear_f32_from_linear_u8(srgba.0[3]), ]) } } @@ -347,10 +367,10 @@ impl From for Rgba { impl From for Color32 { fn from(rgba: Rgba) -> Color32 { Color32([ - gamma_u8_from_linear_f32(rgba[0]), - gamma_u8_from_linear_f32(rgba[1]), - gamma_u8_from_linear_f32(rgba[2]), - linear_u8_from_linear_f32(rgba[3]), + gamma_u8_from_linear_f32(rgba.0[0]), + gamma_u8_from_linear_f32(rgba.0[1]), + gamma_u8_from_linear_f32(rgba.0[2]), + linear_u8_from_linear_f32(rgba.0[3]), ]) } } @@ -366,6 +386,7 @@ pub fn linear_f32_from_gamma_u8(s: u8) -> f32 { /// linear [0, 255] -> linear [0, 1]. /// Useful for alpha-channel. +#[inline(always)] pub fn linear_f32_from_linear_u8(a: u8) -> f32 { a as f32 / 255.0 } @@ -386,6 +407,7 @@ pub fn gamma_u8_from_linear_f32(l: f32) -> u8 { /// linear [0, 1] -> linear [0, 255] (clamped). /// Useful for alpha-channel. +#[inline(always)] pub fn linear_u8_from_linear_f32(a: f32) -> u8 { (a * 255.0).round() as u8 // rust does a saturating cast since 1.45 } diff --git a/epaint/src/shape.rs b/epaint/src/shape.rs index de99f8e2..ceceac52 100644 --- a/epaint/src/shape.rs +++ b/epaint/src/shape.rs @@ -155,6 +155,7 @@ impl Shape { Self::mesh(mesh) } + #[inline(always)] pub fn texture_id(&self) -> super::TextureId { if let Shape::Mesh(mesh) = self { mesh.texture_id diff --git a/epaint/src/text/fonts.rs b/epaint/src/text/fonts.rs index 4ffa08d2..f9eb65f4 100644 --- a/epaint/src/text/fonts.rs +++ b/epaint/src/text/fonts.rs @@ -359,6 +359,7 @@ impl Fonts { impl std::ops::Index for Fonts { type Output = Font; + #[inline(always)] fn index(&self, text_style: TextStyle) -> &Font { &self.fonts[&text_style] } diff --git a/epaint/src/text/galley.rs b/epaint/src/text/galley.rs index 00fa06b7..4ad62920 100644 --- a/epaint/src/text/galley.rs +++ b/epaint/src/text/galley.rs @@ -72,30 +72,36 @@ pub struct Row { } impl Row { + #[inline] pub fn sanity_check(&self) { assert!(!self.x_offsets.is_empty()); assert!(self.x_offsets.len() == self.uv_rects.len() + 1); } /// Excludes the implicit `\n` after the `Row`, if any. + #[inline] pub fn char_count_excluding_newline(&self) -> usize { assert!(!self.x_offsets.is_empty()); self.x_offsets.len() - 1 } /// Includes the implicit `\n` after the `Row`, if any. + #[inline] pub fn char_count_including_newline(&self) -> usize { self.char_count_excluding_newline() + (self.ends_with_newline as usize) } + #[inline] pub fn min_x(&self) -> f32 { *self.x_offsets.first().unwrap() } + #[inline] pub fn max_x(&self) -> f32 { *self.x_offsets.last().unwrap() } + #[inline] pub fn height(&self) -> f32 { self.y_max - self.y_min } @@ -124,6 +130,7 @@ impl Row { } // Move down this much + #[inline(always)] pub fn translate_y(&mut self, dy: f32) { self.y_min += dy; self.y_max += dy; diff --git a/epaint/src/texture_atlas.rs b/epaint/src/texture_atlas.rs index 6cc6db4a..fbc3dd7b 100644 --- a/epaint/src/texture_atlas.rs +++ b/epaint/src/texture_atlas.rs @@ -30,6 +30,7 @@ impl Texture { impl std::ops::Index<(usize, usize)> for Texture { type Output = u8; + #[inline] fn index(&self, (x, y): (usize, usize)) -> &u8 { assert!(x < self.width); assert!(y < self.height); @@ -38,6 +39,7 @@ impl std::ops::Index<(usize, usize)> for Texture { } impl std::ops::IndexMut<(usize, usize)> for Texture { + #[inline] fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut u8 { assert!(x < self.width); assert!(y < self.height);