From 18a01b8355932f01296a40bb7ba6b45884d1bd10 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 13 Apr 2025 21:11:07 +0300 Subject: [PATCH] constify and deinline some methods --- output/src/area.rs | 58 +++++++++++++------------- output/src/coordinate.rs | 18 ++++---- output/src/op_align.rs | 22 +++++----- output/src/op_bsp.rs | 12 +++--- output/src/op_cond.rs | 14 ++++++- output/src/op_iter.rs | 4 +- output/src/{reduce.rs => op_reduce.rs} | 2 +- output/src/op_transform.rs | 12 +++--- output/src/size.rs | 26 ++++++------ output/src/thunk.rs | 48 +++++++++++++++------ 10 files changed, 124 insertions(+), 92 deletions(-) rename output/src/{reduce.rs => op_reduce.rs} (97%) diff --git a/output/src/area.rs b/output/src/area.rs index 261ba54..aefcfe8 100644 --- a/output/src/area.rs +++ b/output/src/area.rs @@ -6,77 +6,75 @@ pub trait Area: From<[N;4]> + Debug + Copy { fn y (&self) -> N; fn w (&self) -> N; fn h (&self) -> N; - #[inline] fn zero () -> [N;4] { + fn zero () -> [N;4] { [N::zero(), N::zero(), N::zero(), N::zero()] } - #[inline] fn from_position (pos: impl Size) -> [N;4] { + fn from_position (pos: impl Size) -> [N;4] { let [x, y] = pos.wh(); [x, y, 0.into(), 0.into()] } - #[inline] fn from_size (size: impl Size) -> [N;4] { + fn from_size (size: impl Size) -> [N;4] { let [w, h] = size.wh(); [0.into(), 0.into(), w, h] } - #[inline] fn expect_min (&self, w: N, h: N) -> Usually<&Self> { + fn expect_min (&self, w: N, h: N) -> Usually<&Self> { if self.w() < w || self.h() < h { Err(format!("min {w}x{h}").into()) } else { Ok(self) } } - #[inline] fn xy (&self) -> [N;2] { + fn xy (&self) -> [N;2] { [self.x(), self.y()] } - #[inline] fn wh (&self) -> [N;2] { + fn wh (&self) -> [N;2] { [self.w(), self.h()] } - #[inline] fn xywh (&self) -> [N;4] { + fn xywh (&self) -> [N;4] { [self.x(), self.y(), self.w(), self.h()] } - #[inline] fn clip_h (&self, h: N) -> [N;4] { + fn clip_h (&self, h: N) -> [N;4] { [self.x(), self.y(), self.w(), self.h().min(h)] } - #[inline] fn clip_w (&self, w: N) -> [N;4] { + fn clip_w (&self, w: N) -> [N;4] { [self.x(), self.y(), self.w().min(w), self.h()] } - #[inline] fn clip (&self, wh: impl Size) -> [N;4] { + fn clip (&self, wh: impl Size) -> [N;4] { [self.x(), self.y(), wh.w(), wh.h()] } - #[inline] fn set_w (&self, w: N) -> [N;4] { + fn set_w (&self, w: N) -> [N;4] { [self.x(), self.y(), w, self.h()] } - #[inline] fn set_h (&self, h: N) -> [N;4] { + fn set_h (&self, h: N) -> [N;4] { [self.x(), self.y(), self.w(), h] } - #[inline] fn x2 (&self) -> N { + fn x2 (&self) -> N { self.x().plus(self.w()) } - #[inline] fn y2 (&self) -> N { + fn y2 (&self) -> N { self.y().plus(self.h()) } - #[inline] fn lrtb (&self) -> [N;4] { + fn lrtb (&self) -> [N;4] { [self.x(), self.x2(), self.y(), self.y2()] } - #[inline] fn center (&self) -> [N;2] { + fn center (&self) -> [N;2] { [self.x().plus(self.w()/2.into()), self.y().plus(self.h()/2.into())] } - #[inline] fn center_x (&self, n: N) -> [N;4] { + fn center_x (&self, n: N) -> [N;4] { let [x, y, w, h] = self.xywh(); [(x.plus(w / 2.into())).minus(n / 2.into()), y.plus(h / 2.into()), n, 1.into()] } - #[inline] fn center_y (&self, n: N) -> [N;4] { + fn center_y (&self, n: N) -> [N;4] { let [x, y, w, h] = self.xywh(); [x.plus(w / 2.into()), (y.plus(h / 2.into())).minus(n / 2.into()), 1.into(), n] } - #[inline] fn center_xy (&self, [n, m]: [N;2]) -> [N;4] { + fn center_xy (&self, [n, m]: [N;2]) -> [N;4] { let [x, y, w, h] = self.xywh(); [(x.plus(w / 2.into())).minus(n / 2.into()), (y.plus(h / 2.into())).minus(m / 2.into()), n, m] } - - #[inline] fn centered (&self) -> [N;2] { + fn centered (&self) -> [N;2] { [self.x().minus(self.w()/2.into()), self.y().minus(self.h()/2.into())] } - fn iter_x (&self) -> impl Iterator where N: std::iter::Step { self.x()..(self.x()+self.w()) } @@ -86,17 +84,17 @@ pub trait Area: From<[N;4]> + Debug + Copy { } impl Area for (N, N, N, N) { - #[inline] fn x (&self) -> N { self.0 } - #[inline] fn y (&self) -> N { self.1 } - #[inline] fn w (&self) -> N { self.2 } - #[inline] fn h (&self) -> N { self.3 } + fn x (&self) -> N { self.0 } + fn y (&self) -> N { self.1 } + fn w (&self) -> N { self.2 } + fn h (&self) -> N { self.3 } } impl Area for [N;4] { - #[inline] fn x (&self) -> N { self[0] } - #[inline] fn y (&self) -> N { self[1] } - #[inline] fn w (&self) -> N { self[2] } - #[inline] fn h (&self) -> N { self[3] } + fn x (&self) -> N { self[0] } + fn y (&self) -> N { self[1] } + fn w (&self) -> N { self[2] } + fn h (&self) -> N { self[3] } } #[cfg(test)] mod test_area { diff --git a/output/src/coordinate.rs b/output/src/coordinate.rs index 9523fbc..262be9e 100644 --- a/output/src/coordinate.rs +++ b/output/src/coordinate.rs @@ -1,12 +1,6 @@ use std::fmt::{Debug, Display}; use std::ops::{Add, Sub, Mul, Div}; -impl Coordinate for u16 { - #[inline] fn plus (self, other: Self) -> Self { - self.saturating_add(other) - } -} - /// A linear coordinate. pub trait Coordinate: Send + Sync + Copy + Add @@ -19,13 +13,19 @@ pub trait Coordinate: Send + Sync + Copy + Into + Into { - #[inline] fn zero () -> Self { 0.into() } - #[inline] fn minus (self, other: Self) -> Self { + fn zero () -> Self { 0.into() } + fn plus (self, other: Self) -> Self; + fn minus (self, other: Self) -> Self { if self >= other { self - other } else { 0.into() } } - fn plus (self, other: Self) -> Self; +} + +impl Coordinate for u16 { + fn plus (self, other: Self) -> Self { + self.saturating_add(other) + } } diff --git a/output/src/op_align.rs b/output/src/op_align.rs index c2a719d..d254ce3 100644 --- a/output/src/op_align.rs +++ b/output/src/op_align.rs @@ -63,17 +63,17 @@ try_from_expr!(<'a, E>: Align>: |state, iter|{ }); impl Align { - #[inline] pub fn c (a: A) -> Self { Self(Alignment::Center, a) } - #[inline] pub fn x (a: A) -> Self { Self(Alignment::X, a) } - #[inline] pub fn y (a: A) -> Self { Self(Alignment::Y, a) } - #[inline] pub fn n (a: A) -> Self { Self(Alignment::N, a) } - #[inline] pub fn s (a: A) -> Self { Self(Alignment::S, a) } - #[inline] pub fn e (a: A) -> Self { Self(Alignment::E, a) } - #[inline] pub fn w (a: A) -> Self { Self(Alignment::W, a) } - #[inline] pub fn nw (a: A) -> Self { Self(Alignment::NW, a) } - #[inline] pub fn sw (a: A) -> Self { Self(Alignment::SW, a) } - #[inline] pub fn ne (a: A) -> Self { Self(Alignment::NE, a) } - #[inline] pub fn se (a: A) -> Self { Self(Alignment::SE, a) } + #[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) } + #[inline] pub const fn x (a: A) -> Self { Self(Alignment::X, a) } + #[inline] pub const fn y (a: A) -> Self { Self(Alignment::Y, a) } + #[inline] pub const fn n (a: A) -> Self { Self(Alignment::N, a) } + #[inline] pub const fn s (a: A) -> Self { Self(Alignment::S, a) } + #[inline] pub const fn e (a: A) -> Self { Self(Alignment::E, a) } + #[inline] pub const fn w (a: A) -> Self { Self(Alignment::W, a) } + #[inline] pub const fn nw (a: A) -> Self { Self(Alignment::NW, a) } + #[inline] pub const fn sw (a: A) -> Self { Self(Alignment::SW, a) } + #[inline] pub const fn ne (a: A) -> Self { Self(Alignment::NE, a) } + #[inline] pub const fn se (a: A) -> Self { Self(Alignment::SE, a) } } impl> Content for Align { fn content (&self) -> impl Render { diff --git a/output/src/op_bsp.rs b/output/src/op_bsp.rs index f1a5a5d..52e697d 100644 --- a/output/src/op_bsp.rs +++ b/output/src/op_bsp.rs @@ -41,12 +41,12 @@ try_from_expr!(<'a, E>: Bsp, RenderBox<'a, E>>: |state, iter| { } }); impl Bsp { - #[inline] pub fn n (a: A, b: B) -> Self { Self(North, a, b) } - #[inline] pub fn s (a: A, b: B) -> Self { Self(South, a, b) } - #[inline] pub fn e (a: A, b: B) -> Self { Self(East, a, b) } - #[inline] pub fn w (a: A, b: B) -> Self { Self(West, a, b) } - #[inline] pub fn a (a: A, b: B) -> Self { Self(Above, a, b) } - #[inline] pub fn b (a: A, b: B) -> Self { Self(Below, a, b) } + #[inline] pub const fn n (a: A, b: B) -> Self { Self(North, a, b) } + #[inline] pub const fn s (a: A, b: B) -> Self { Self(South, a, b) } + #[inline] pub const fn e (a: A, b: B) -> Self { Self(East, a, b) } + #[inline] pub const fn w (a: A, b: B) -> Self { Self(West, a, b) } + #[inline] pub const fn a (a: A, b: B) -> Self { Self(Above, a, b) } + #[inline] pub const fn b (a: A, b: B) -> Self { Self(Below, a, b) } } pub trait BspAreas, B: Content> { fn direction (&self) -> Direction; diff --git a/output/src/op_cond.rs b/output/src/op_cond.rs index a52c06f..5408852 100644 --- a/output/src/op_cond.rs +++ b/output/src/op_cond.rs @@ -2,11 +2,21 @@ use crate::*; /// Show an item only when a condition is true. pub struct When(pub bool, pub A); -impl When { #[inline] pub fn new (c: bool, a: A) -> Self { Self(c, a) } } +impl When { + /// Create a binary condition. + pub const fn new (c: bool, a: A) -> Self { + Self(c, a) + } +} /// Show one item if a condition is true and another if the condition is false pub struct Either(pub bool, pub A, pub B); -impl Either { #[inline] pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } } +impl Either { + /// Create a ternary condition. + pub const fn new (c: bool, a: A, b: B) -> Self { + Self(c, a, b) + } +} #[cfg(feature = "dsl")] try_from_expr!(<'a, E>: When>: |state, iter| { diff --git a/output/src/op_iter.rs b/output/src/op_iter.rs index c1f2796..32202bf 100644 --- a/output/src/op_iter.rs +++ b/output/src/op_iter.rs @@ -40,9 +40,9 @@ impl<'a, E, A, B, I, F, G> Map where I: Iterator + Send + Sync + 'a, F: Fn() -> I + Send + Sync + 'a, { - pub fn new (get_iterator: F, get_item: G) -> Self { + pub const fn new (get_iterator: F, get_item: G) -> Self { Self { - __: Default::default(), + __: PhantomData, get_iterator, get_item } diff --git a/output/src/reduce.rs b/output/src/op_reduce.rs similarity index 97% rename from output/src/reduce.rs rename to output/src/op_reduce.rs index 55021ef..dfcc00e 100644 --- a/output/src/reduce.rs +++ b/output/src/op_reduce.rs @@ -12,7 +12,7 @@ impl Reduce where F: Fn() -> I + Send + Sync, G: Fn(A, B, usize)->A + Send + Sync { - pub fn new (f: F, g: G) -> Self { Self(Default::default(), f, g) } + pub const fn new (f: F, g: G) -> Self { Self(Default::default(), f, g) } } impl Content for Reduce where diff --git a/output/src/op_transform.rs b/output/src/op_transform.rs index c9426df..6715588 100644 --- a/output/src/op_transform.rs +++ b/output/src/op_transform.rs @@ -26,9 +26,9 @@ macro_rules! transform_xy { ($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => { pub enum $Enum { X(T), Y(T), XY(T) } impl $Enum { - #[inline] pub fn x (item: T) -> Self { Self::X(item) } - #[inline] pub fn y (item: T) -> Self { Self::Y(item) } - #[inline] pub fn xy (item: T) -> Self { Self::XY(item) } + #[inline] pub const fn x (item: T) -> Self { Self::X(item) } + #[inline] pub const fn y (item: T) -> Self { Self::Y(item) } + #[inline] pub const fn xy (item: T) -> Self { Self::XY(item) } } #[cfg(feature = "dsl")] impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> @@ -78,9 +78,9 @@ macro_rules! transform_xy_unit { ($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => { pub enum $Enum { X(U, T), Y(U, T), XY(U, U, T), } impl $Enum { - #[inline] pub fn x (x: U, item: T) -> Self { Self::X(x, item) } - #[inline] pub fn y (y: U, item: T) -> Self { Self::Y(y, item) } - #[inline] pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) } + #[inline] pub const fn x (x: U, item: T) -> Self { Self::X(x, item) } + #[inline] pub const fn y (y: U, item: T) -> Self { Self::Y(y, item) } + #[inline] pub const fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) } } #[cfg(feature = "dsl")] impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> diff --git a/output/src/size.rs b/output/src/size.rs index c0518d8..cfeeeed 100644 --- a/output/src/size.rs +++ b/output/src/size.rs @@ -4,39 +4,39 @@ use std::fmt::Debug; pub trait Size: From<[N;2]> + Debug + Copy { fn x (&self) -> N; fn y (&self) -> N; - #[inline] fn w (&self) -> N { self.x() } - #[inline] fn h (&self) -> N { self.y() } - #[inline] fn wh (&self) -> [N;2] { [self.x(), self.y()] } - #[inline] fn clip_w (&self, w: N) -> [N;2] { [self.w().min(w), self.h()] } - #[inline] fn clip_h (&self, h: N) -> [N;2] { [self.w(), self.h().min(h)] } - #[inline] fn expect_min (&self, w: N, h: N) -> Usually<&Self> { + fn w (&self) -> N { self.x() } + fn h (&self) -> N { self.y() } + fn wh (&self) -> [N;2] { [self.x(), self.y()] } + fn clip_w (&self, w: N) -> [N;2] { [self.w().min(w), self.h()] } + fn clip_h (&self, h: N) -> [N;2] { [self.w(), self.h().min(h)] } + fn expect_min (&self, w: N, h: N) -> Usually<&Self> { if self.w() < w || self.h() < h { Err(format!("min {w}x{h}").into()) } else { Ok(self) } } - #[inline] fn zero () -> [N;2] { + fn zero () -> [N;2] { [N::zero(), N::zero()] } - #[inline] fn to_area_pos (&self) -> [N;4] { + fn to_area_pos (&self) -> [N;4] { let [x, y] = self.wh(); [x, y, 0.into(), 0.into()] } - #[inline] fn to_area_size (&self) -> [N;4] { + fn to_area_size (&self) -> [N;4] { let [w, h] = self.wh(); [0.into(), 0.into(), w, h] } } impl Size for (N, N) { - #[inline] fn x (&self) -> N { self.0 } - #[inline] fn y (&self) -> N { self.1 } + fn x (&self) -> N { self.0 } + fn y (&self) -> N { self.1 } } impl Size for [N;2] { - #[inline] fn x (&self) -> N { self[0] } - #[inline] fn y (&self) -> N { self[1] } + fn x (&self) -> N { self[0] } + fn y (&self) -> N { self[1] } } #[cfg(test)] mod test_size { diff --git a/output/src/thunk.rs b/output/src/thunk.rs index 8bcbcaa..9d2c0bf 100644 --- a/output/src/thunk.rs +++ b/output/src/thunk.rs @@ -2,28 +2,39 @@ use crate::*; use std::marker::PhantomData; /// Lazily-evaluated [Render]able. -pub struct Thunk, F: Fn()->T + Send + Sync>(PhantomData, F); +pub struct Thunk, F: Fn()->T + Send + Sync>( + PhantomData, + F +); impl, F: Fn()->T + Send + Sync> Thunk { - pub fn new (thunk: F) -> Self { - Self(Default::default(), thunk) + pub const fn new (thunk: F) -> Self { + Self(PhantomData, thunk) } } impl, F: Fn()->T + Send + Sync> Content for Thunk { fn content (&self) -> impl Render { (self.1)() } } -pub struct ThunkBox<'a, E: Output>(PhantomData, BoxRenderBox<'a, E> + Send + Sync + 'a>); +pub struct ThunkBox<'a, E: Output>( + PhantomData, + BoxRenderBox<'a, E> + Send + Sync + 'a> +); impl<'a, E: Output> ThunkBox<'a, E> { - pub fn new (thunk: BoxRenderBox<'a, E> + Send + Sync + 'a>) -> Self { - Self(Default::default(), thunk) + pub const fn new (thunk: BoxRenderBox<'a, E> + Send + Sync + 'a>) -> Self { + Self(PhantomData, thunk) } } impl<'a, E: Output> Content for ThunkBox<'a, E> { fn content (&self) -> impl Render { (self.1)() } } -impl<'a, E: Output, F: Fn()->T + Send + Sync + 'a, T: Render + Send + Sync + 'a> From for ThunkBox<'a, E> { +impl<'a, E, F, T> From for ThunkBox<'a, E> +where + E: Output, + F: Fn()->T + Send + Sync + 'a, + T: Render + Send + Sync + 'a +{ fn from (f: F) -> Self { - Self(Default::default(), Box::new(move||f().boxed())) + Self(PhantomData, Box::new(move||f().boxed())) } } //impl<'a, E: Output, F: Fn()->Box + 'a> + Send + Sync + 'a> From for ThunkBox<'a, E> { @@ -34,17 +45,30 @@ impl<'a, E: Output, F: Fn()->T + Send + Sync + 'a, T: Render + Send + Sync + pub struct ThunkRender(PhantomData, F); impl ThunkRender { - pub fn new (render: F) -> Self { Self(Default::default(), render) } + pub fn new (render: F) -> Self { Self(PhantomData, render) } } impl Content for ThunkRender { fn render (&self, to: &mut E) { (self.1)(to) } } -pub struct ThunkLayoutE::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync>(PhantomData, F1, F2); +pub struct ThunkLayout< + E: Output, + F1: Fn(E::Area)->E::Area + Send + Sync, + F2: Fn(&mut E) + Send + Sync +>( + PhantomData, + F1, + F2 +); implE::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> ThunkLayout { - pub fn new (layout: F1, render: F2) -> Self { Self(Default::default(), layout, render) } + pub fn new (layout: F1, render: F2) -> Self { Self(PhantomData, layout, render) } } -implE::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> Content for ThunkLayout { +impl Content for ThunkLayout +where + E: Output, + F1: Fn(E::Area)->E::Area + Send + Sync, + F2: Fn(&mut E) + Send + Sync +{ fn layout (&self, to: E::Area) -> E::Area { (self.1)(to) } fn render (&self, to: &mut E) { (self.2)(to) } }