removing engine generic from transforms

This commit is contained in:
🪞👃🪞 2025-01-18 02:52:54 +01:00
parent 452bdf9598
commit a949117017
8 changed files with 184 additions and 184 deletions

View file

@ -7,19 +7,18 @@ use RefAtom::*;
/// using `PhantomData` to permit the double generic.
macro_rules! transform_xy {
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => {
pub enum $Enum<E, T> { _Unused(PhantomData<E>), X(T), Y(T), XY(T) }
impl<E, T> $Enum<E, T> {
pub enum $Enum<T> { X(T), Y(T), XY(T) }
impl<T> $Enum<T> {
pub fn x (item: T) -> Self { Self::X(item) }
pub fn y (item: T) -> Self { Self::Y(item) }
pub fn xy (item: T) -> Self { Self::XY(item) }
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E, T> {
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
fn content (&self) -> impl Render<E> {
match self {
Self::X(item) => item,
Self::Y(item) => item,
Self::XY(item) => item,
_ => unreachable!(),
}
}
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area {
@ -27,14 +26,20 @@ macro_rules! transform_xy {
$area
}
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E, RenderBox<'a, E>> {
fn try_from_atom (
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
) -> Option<Self> {
Some(match (head.to_ref(), tail) {
(Key($x), [a]) => Self::x(state.get_content(a).expect("no content")),
(Key($y), [a]) => Self::y(state.get_content(a).expect("no content")),
(Key($xy), [a]) => Self::xy(state.get_content(a).expect("no content")),
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_content(atoms.next().expect("no content")).expect("no content")
),
$y => Self::y(
state.get_content(atoms.next().expect("no content")).expect("no content")
),
$xy => Self::xy(
state.get_content(atoms.next().expect("no content")).expect("no content")
),
_ => return None
})
}
@ -45,60 +50,53 @@ macro_rules! transform_xy {
/// along either the X axis, the Y axis, or both.
macro_rules! transform_xy_unit {
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
pub enum $Enum<E, U, T> { _Unused(E), X(U, T), Y(U, T), XY(U, U, T), }
impl<E, U, T> $Enum<E, U, T> {
pub enum $Enum<U, T> { X(U, T), Y(U, T), XY(U, U, T), }
impl<U, T> $Enum<U, T> {
pub fn x (x: U, item: T) -> Self { Self::X(x, item) }
pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
}
impl<E, U: Copy + Coordinate, T> $Enum<E, U, T> {
impl<U: Copy + Coordinate, T> $Enum<U, T> {
pub fn dx (&self) -> U {
match self {
Self::X(x, _) => *x,
Self::Y(_, _) => 0.into(),
Self::XY(x, _, _) => *x,
_ => unreachable!(),
Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x,
}
}
pub fn dy (&self) -> U {
match self {
Self::X(_, _) => 0.into(),
Self::Y(y, _) => *y,
Self::XY(_, y, _) => *y,
_ => unreachable!(),
Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y,
}
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E, E::Unit, T> {
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
fn content (&self) -> impl Render<E> {
Some(match self {
Self::X(_, content) => content,
Self::Y(_, content) => content,
Self::X(_, content) => content,
Self::Y(_, content) => content,
Self::XY(_, _, content) => content,
_ => unreachable!(),
})
}
fn layout (&$self, $to: E::Area) -> E::Area {
$layout.into()
}
}
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
fn try_from_atom (
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
) -> Option<Self> {
Some(match (head.to_ref(), tail) {
(Key($x), [x, a]) => Self::x(
state.get_unit(x).expect("no x"),
state.get_content(a).expect("no content"),
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_atoms (state: &'a T, mut atoms: impl Iterator<Item = RefAtom<'a>> + 'a) -> Option<Self> {
let head = atoms.next()?;
if head.kind() != TokenKind::Key { return None }
Some(match head.text() {
$x => Self::x(
state.get_unit(atoms.next().expect("no x")).expect("no x"),
state.get_content(atoms.next().expect("no content")).expect("no content")
),
(Key($y), [y, a]) => Self::y(
state.get_unit(y).expect("no y"),
state.get_content(a).expect("no content"),
$y => Self::y(
state.get_unit(atoms.next().expect("no y")).expect("no y"),
state.get_content(atoms.next().expect("no content")).expect("no content")
),
(Key($xy), [x, y, a]) => Self::xy(
state.get_unit(x).expect("no x"),
state.get_unit(y).expect("no y"),
state.get_content(a).expect("no content"),
$xy => Self::xy(
state.get_unit(atoms.next().expect("no x")).expect("no x"),
state.get_unit(atoms.next().expect("no y")).expect("no y"),
state.get_content(atoms.next().expect("no content")).expect("no content"),
),
_ => return None
})