Add a bunch on inline annotations

This commit is contained in:
Emil Ernerfeldt 2021-04-01 22:10:34 +02:00
parent d702e3078a
commit d7f9e2246c
7 changed files with 65 additions and 8 deletions

View file

@ -111,47 +111,57 @@ impl Pos2 {
} }
} }
#[inline]
pub fn distance(self, other: Self) -> f32 { pub fn distance(self, other: Self) -> f32 {
(self - other).length() (self - other).length()
} }
#[inline]
pub fn distance_sq(self, other: Self) -> f32 { pub fn distance_sq(self, other: Self) -> f32 {
(self - other).length_sq() (self - other).length_sq()
} }
#[inline(always)]
pub fn floor(self) -> Self { pub fn floor(self) -> Self {
pos2(self.x.floor(), self.y.floor()) pos2(self.x.floor(), self.y.floor())
} }
#[inline(always)]
pub fn round(self) -> Self { pub fn round(self) -> Self {
pos2(self.x.round(), self.y.round()) pos2(self.x.round(), self.y.round())
} }
#[inline(always)]
pub fn ceil(self) -> Self { pub fn ceil(self) -> Self {
pos2(self.x.ceil(), self.y.ceil()) pos2(self.x.ceil(), self.y.ceil())
} }
/// True if all members are also finite. /// True if all members are also finite.
#[inline(always)]
pub fn is_finite(self) -> bool { pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite() self.x.is_finite() && self.y.is_finite()
} }
/// True if any member is NaN. /// True if any member is NaN.
#[inline(always)]
pub fn any_nan(self) -> bool { pub fn any_nan(self) -> bool {
self.x.is_nan() || self.y.is_nan() self.x.is_nan() || self.y.is_nan()
} }
#[must_use] #[must_use]
#[inline]
pub fn min(self, other: Self) -> Self { pub fn min(self, other: Self) -> Self {
pos2(self.x.min(other.x), self.y.min(other.y)) pos2(self.x.min(other.x), self.y.min(other.y))
} }
#[must_use] #[must_use]
#[inline]
pub fn max(self, other: Self) -> Self { pub fn max(self, other: Self) -> Self {
pos2(self.x.max(other.x), self.y.max(other.y)) pos2(self.x.max(other.x), self.y.max(other.y))
} }
#[must_use] #[must_use]
#[inline]
pub fn clamp(self, min: Self, max: Self) -> Self { pub fn clamp(self, min: Self, max: Self) -> Self {
Self { Self {
x: self.x.clamp(min.x, max.x), x: self.x.clamp(min.x, max.x),
@ -163,6 +173,7 @@ impl Pos2 {
impl std::ops::Index<usize> for Pos2 { impl std::ops::Index<usize> for Pos2 {
type Output = f32; type Output = f32;
#[inline(always)]
fn index(&self, index: usize) -> &f32 { fn index(&self, index: usize) -> &f32 {
match index { match index {
0 => &self.x, 0 => &self.x,
@ -173,6 +184,7 @@ impl std::ops::Index<usize> for Pos2 {
} }
impl std::ops::IndexMut<usize> for Pos2 { impl std::ops::IndexMut<usize> for Pos2 {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut f32 { fn index_mut(&mut self, index: usize) -> &mut f32 {
match index { match index {
0 => &mut self.x, 0 => &mut self.x,

View file

@ -149,53 +149,63 @@ impl Vec2 {
} }
#[must_use] #[must_use]
#[inline(always)]
pub fn floor(self) -> Self { pub fn floor(self) -> Self {
vec2(self.x.floor(), self.y.floor()) vec2(self.x.floor(), self.y.floor())
} }
#[must_use] #[must_use]
#[inline(always)]
pub fn round(self) -> Self { pub fn round(self) -> Self {
vec2(self.x.round(), self.y.round()) vec2(self.x.round(), self.y.round())
} }
#[must_use] #[must_use]
#[inline(always)]
pub fn ceil(self) -> Self { pub fn ceil(self) -> Self {
vec2(self.x.ceil(), self.y.ceil()) vec2(self.x.ceil(), self.y.ceil())
} }
/// True if all members are also finite. /// True if all members are also finite.
#[inline(always)]
pub fn is_finite(self) -> bool { pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite() self.x.is_finite() && self.y.is_finite()
} }
/// True if any member is NaN. /// True if any member is NaN.
#[inline(always)]
pub fn any_nan(self) -> bool { pub fn any_nan(self) -> bool {
self.x.is_nan() || self.y.is_nan() self.x.is_nan() || self.y.is_nan()
} }
#[must_use] #[must_use]
#[inline]
pub fn min(self, other: Self) -> Self { pub fn min(self, other: Self) -> Self {
vec2(self.x.min(other.x), self.y.min(other.y)) vec2(self.x.min(other.x), self.y.min(other.y))
} }
#[must_use] #[must_use]
#[inline]
pub fn max(self, other: Self) -> Self { pub fn max(self, other: Self) -> Self {
vec2(self.x.max(other.x), self.y.max(other.y)) vec2(self.x.max(other.x), self.y.max(other.y))
} }
/// Returns the minimum of `self.x` and `self.y`. /// Returns the minimum of `self.x` and `self.y`.
#[must_use] #[must_use]
#[inline(always)]
pub fn min_elem(self) -> f32 { pub fn min_elem(self) -> f32 {
self.x.min(self.y) self.x.min(self.y)
} }
/// Returns the maximum of `self.x` and `self.y`. /// Returns the maximum of `self.x` and `self.y`.
#[inline(always)]
#[must_use] #[must_use]
pub fn max_elem(self) -> f32 { pub fn max_elem(self) -> f32 {
self.x.max(self.y) self.x.max(self.y)
} }
#[must_use] #[must_use]
#[inline]
pub fn clamp(self, min: Self, max: Self) -> Self { pub fn clamp(self, min: Self, max: Self) -> Self {
Self { Self {
x: self.x.clamp(min.x, max.x), x: self.x.clamp(min.x, max.x),
@ -207,6 +217,7 @@ impl Vec2 {
impl std::ops::Index<usize> for Vec2 { impl std::ops::Index<usize> for Vec2 {
type Output = f32; type Output = f32;
#[inline(always)]
fn index(&self, index: usize) -> &f32 { fn index(&self, index: usize) -> &f32 {
match index { match index {
0 => &self.x, 0 => &self.x,
@ -217,6 +228,7 @@ impl std::ops::Index<usize> for Vec2 {
} }
impl std::ops::IndexMut<usize> for Vec2 { impl std::ops::IndexMut<usize> for Vec2 {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut f32 { fn index_mut(&mut self, index: usize) -> &mut f32 {
match index { match index {
0 => &mut self.x, 0 => &mut self.x,

View file

@ -17,12 +17,15 @@ pub struct Color32(pub(crate) [u8; 4]);
impl std::ops::Index<usize> for Color32 { impl std::ops::Index<usize> for Color32 {
type Output = u8; type Output = u8;
#[inline(always)]
fn index(&self, index: usize) -> &u8 { fn index(&self, index: usize) -> &u8 {
&self.0[index] &self.0[index]
} }
} }
impl std::ops::IndexMut<usize> for Color32 { impl std::ops::IndexMut<usize> for Color32 {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut u8 { fn index_mut(&mut self, index: usize) -> &mut u8 {
&mut self.0[index] &mut self.0[index]
} }
@ -46,15 +49,18 @@ impl Color32 {
pub const LIGHT_BLUE: Color32 = Color32::from_rgb(140, 160, 255); pub const LIGHT_BLUE: Color32 = Color32::from_rgb(140, 160, 255);
pub const GOLD: Color32 = Color32::from_rgb(255, 215, 0); pub const GOLD: Color32 = Color32::from_rgb(255, 215, 0);
#[inline(always)]
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self { pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self([r, g, b, 255]) Self([r, g, b, 255])
} }
#[inline(always)]
pub const fn from_rgb_additive(r: u8, g: u8, b: u8) -> Self { pub const fn from_rgb_additive(r: u8, g: u8, b: u8) -> Self {
Self([r, g, b, 0]) Self([r, g, b, 0])
} }
/// From `sRGBA` with premultiplied alpha. /// From `sRGBA` with premultiplied alpha.
#[inline(always)]
pub const fn from_rgba_premultiplied(r: u8, g: u8, b: u8, a: u8) -> Self { pub const fn from_rgba_premultiplied(r: u8, g: u8, b: u8, a: u8) -> Self {
Self([r, g, b, a]) Self([r, g, b, a])
} }
@ -84,10 +90,12 @@ impl Color32 {
Self([r, g, b, a]) Self([r, g, b, a])
} }
#[inline(always)]
pub const fn from_gray(l: u8) -> Self { pub const fn from_gray(l: u8) -> Self {
Self([l, l, l, 255]) Self([l, l, l, 255])
} }
#[inline(always)]
pub const fn from_black_alpha(a: u8) -> Self { pub const fn from_black_alpha(a: u8) -> Self {
Self([0, 0, 0, a]) Self([0, 0, 0, a])
} }
@ -96,6 +104,7 @@ impl Color32 {
Rgba::from_white_alpha(linear_f32_from_linear_u8(a)).into() Rgba::from_white_alpha(linear_f32_from_linear_u8(a)).into()
} }
#[inline(always)]
pub const fn from_additive_luminance(l: u8) -> Self { pub const fn from_additive_luminance(l: u8) -> Self {
Self([l, l, l, 0]) Self([l, l, l, 0])
} }
@ -131,6 +140,7 @@ impl Color32 {
} }
/// Returns an additive version of self /// Returns an additive version of self
#[inline(always)]
pub fn additive(self) -> Self { pub fn additive(self) -> Self {
let [r, g, b, _] = self.to_array(); let [r, g, b, _] = self.to_array();
Self([r, g, b, 0]) Self([r, g, b, 0])
@ -166,12 +176,15 @@ pub struct Rgba(pub(crate) [f32; 4]);
impl std::ops::Index<usize> for Rgba { impl std::ops::Index<usize> for Rgba {
type Output = f32; type Output = f32;
#[inline(always)]
fn index(&self, index: usize) -> &f32 { fn index(&self, index: usize) -> &f32 {
&self.0[index] &self.0[index]
} }
} }
impl std::ops::IndexMut<usize> for Rgba { impl std::ops::IndexMut<usize> for Rgba {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut f32 { fn index_mut(&mut self, index: usize) -> &mut f32 {
&mut self.0[index] &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 GREEN: Rgba = Rgba::from_rgb(0.0, 1.0, 0.0);
pub const BLUE: Rgba = Rgba::from_rgb(0.0, 0.0, 1.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 { pub const fn from_rgba_premultiplied(r: f32, g: f32, b: f32, a: f32) -> Self {
Self([r, g, b, a]) Self([r, g, b, a])
} }
#[inline(always)]
pub const fn from_rgb(r: f32, g: f32, b: f32) -> Self { pub const fn from_rgb(r: f32, g: f32, b: f32) -> Self {
Self([r, g, b, 1.0]) Self([r, g, b, 1.0])
} }
#[inline(always)]
pub const fn from_gray(l: f32) -> Self { pub const fn from_gray(l: f32) -> Self {
Self([l, l, l, 1.0]) Self([l, l, l, 1.0])
} }
@ -204,18 +220,21 @@ impl Rgba {
} }
/// Transparent black /// Transparent black
#[inline(always)]
pub fn from_black_alpha(a: f32) -> Self { pub fn from_black_alpha(a: f32) -> Self {
debug_assert!(0.0 <= a && a <= 1.0); debug_assert!(0.0 <= a && a <= 1.0);
Self([0.0, 0.0, 0.0, a]) Self([0.0, 0.0, 0.0, a])
} }
/// Transparent white /// Transparent white
#[inline(always)]
pub fn from_white_alpha(a: f32) -> Self { pub fn from_white_alpha(a: f32) -> Self {
debug_assert!(0.0 <= a && a <= 1.0); debug_assert!(0.0 <= a && a <= 1.0);
Self([a, a, a, a]) Self([a, a, a, a])
} }
/// Return an additive version of this color (alpha = 0) /// Return an additive version of this color (alpha = 0)
#[inline(always)]
pub fn additive(self) -> Self { pub fn additive(self) -> Self {
let [r, g, b, _] = self.0; let [r, g, b, _] = self.0;
Self([r, g, b, 0.0]) Self([r, g, b, 0.0])
@ -253,6 +272,7 @@ impl Rgba {
} }
/// How perceptually intense (bright) is the color? /// How perceptually intense (bright) is the color?
#[inline]
pub fn intensity(&self) -> f32 { pub fn intensity(&self) -> f32 {
0.3 * self.r() + 0.59 * self.g() + 0.11 * self.b() 0.3 * self.r() + 0.59 * self.g() + 0.11 * self.b()
} }
@ -336,10 +356,10 @@ impl std::ops::Mul<Rgba> for f32 {
impl From<Color32> for Rgba { impl From<Color32> for Rgba {
fn from(srgba: Color32) -> Rgba { fn from(srgba: Color32) -> Rgba {
Rgba([ Rgba([
linear_f32_from_gamma_u8(srgba[0]), linear_f32_from_gamma_u8(srgba.0[0]),
linear_f32_from_gamma_u8(srgba[1]), linear_f32_from_gamma_u8(srgba.0[1]),
linear_f32_from_gamma_u8(srgba[2]), linear_f32_from_gamma_u8(srgba.0[2]),
linear_f32_from_linear_u8(srgba[3]), linear_f32_from_linear_u8(srgba.0[3]),
]) ])
} }
} }
@ -347,10 +367,10 @@ impl From<Color32> for Rgba {
impl From<Rgba> for Color32 { impl From<Rgba> for Color32 {
fn from(rgba: Rgba) -> Color32 { fn from(rgba: Rgba) -> Color32 {
Color32([ Color32([
gamma_u8_from_linear_f32(rgba[0]), gamma_u8_from_linear_f32(rgba.0[0]),
gamma_u8_from_linear_f32(rgba[1]), gamma_u8_from_linear_f32(rgba.0[1]),
gamma_u8_from_linear_f32(rgba[2]), gamma_u8_from_linear_f32(rgba.0[2]),
linear_u8_from_linear_f32(rgba[3]), 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]. /// linear [0, 255] -> linear [0, 1].
/// Useful for alpha-channel. /// Useful for alpha-channel.
#[inline(always)]
pub fn linear_f32_from_linear_u8(a: u8) -> f32 { pub fn linear_f32_from_linear_u8(a: u8) -> f32 {
a as f32 / 255.0 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). /// linear [0, 1] -> linear [0, 255] (clamped).
/// Useful for alpha-channel. /// Useful for alpha-channel.
#[inline(always)]
pub fn linear_u8_from_linear_f32(a: f32) -> u8 { pub fn linear_u8_from_linear_f32(a: f32) -> u8 {
(a * 255.0).round() as u8 // rust does a saturating cast since 1.45 (a * 255.0).round() as u8 // rust does a saturating cast since 1.45
} }

View file

@ -155,6 +155,7 @@ impl Shape {
Self::mesh(mesh) Self::mesh(mesh)
} }
#[inline(always)]
pub fn texture_id(&self) -> super::TextureId { pub fn texture_id(&self) -> super::TextureId {
if let Shape::Mesh(mesh) = self { if let Shape::Mesh(mesh) = self {
mesh.texture_id mesh.texture_id

View file

@ -359,6 +359,7 @@ impl Fonts {
impl std::ops::Index<TextStyle> for Fonts { impl std::ops::Index<TextStyle> for Fonts {
type Output = Font; type Output = Font;
#[inline(always)]
fn index(&self, text_style: TextStyle) -> &Font { fn index(&self, text_style: TextStyle) -> &Font {
&self.fonts[&text_style] &self.fonts[&text_style]
} }

View file

@ -72,30 +72,36 @@ pub struct Row {
} }
impl Row { impl Row {
#[inline]
pub fn sanity_check(&self) { pub fn sanity_check(&self) {
assert!(!self.x_offsets.is_empty()); assert!(!self.x_offsets.is_empty());
assert!(self.x_offsets.len() == self.uv_rects.len() + 1); assert!(self.x_offsets.len() == self.uv_rects.len() + 1);
} }
/// Excludes the implicit `\n` after the `Row`, if any. /// Excludes the implicit `\n` after the `Row`, if any.
#[inline]
pub fn char_count_excluding_newline(&self) -> usize { pub fn char_count_excluding_newline(&self) -> usize {
assert!(!self.x_offsets.is_empty()); assert!(!self.x_offsets.is_empty());
self.x_offsets.len() - 1 self.x_offsets.len() - 1
} }
/// Includes the implicit `\n` after the `Row`, if any. /// Includes the implicit `\n` after the `Row`, if any.
#[inline]
pub fn char_count_including_newline(&self) -> usize { pub fn char_count_including_newline(&self) -> usize {
self.char_count_excluding_newline() + (self.ends_with_newline as usize) self.char_count_excluding_newline() + (self.ends_with_newline as usize)
} }
#[inline]
pub fn min_x(&self) -> f32 { pub fn min_x(&self) -> f32 {
*self.x_offsets.first().unwrap() *self.x_offsets.first().unwrap()
} }
#[inline]
pub fn max_x(&self) -> f32 { pub fn max_x(&self) -> f32 {
*self.x_offsets.last().unwrap() *self.x_offsets.last().unwrap()
} }
#[inline]
pub fn height(&self) -> f32 { pub fn height(&self) -> f32 {
self.y_max - self.y_min self.y_max - self.y_min
} }
@ -124,6 +130,7 @@ impl Row {
} }
// Move down this much // Move down this much
#[inline(always)]
pub fn translate_y(&mut self, dy: f32) { pub fn translate_y(&mut self, dy: f32) {
self.y_min += dy; self.y_min += dy;
self.y_max += dy; self.y_max += dy;

View file

@ -30,6 +30,7 @@ impl Texture {
impl std::ops::Index<(usize, usize)> for Texture { impl std::ops::Index<(usize, usize)> for Texture {
type Output = u8; type Output = u8;
#[inline]
fn index(&self, (x, y): (usize, usize)) -> &u8 { fn index(&self, (x, y): (usize, usize)) -> &u8 {
assert!(x < self.width); assert!(x < self.width);
assert!(y < self.height); assert!(y < self.height);
@ -38,6 +39,7 @@ impl std::ops::Index<(usize, usize)> for Texture {
} }
impl std::ops::IndexMut<(usize, usize)> for Texture { impl std::ops::IndexMut<(usize, usize)> for Texture {
#[inline]
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut u8 { fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut u8 {
assert!(x < self.width); assert!(x < self.width);
assert!(y < self.height); assert!(y < self.height);