read explicit lifetime to FromDsl
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-20 22:02:51 +03:00
parent 7c1cddc759
commit 455d6d00d5
14 changed files with 252 additions and 286 deletions

View file

@ -36,39 +36,33 @@ pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A);
#[cfg(feature = "dsl")]
impl<State: Dsl<A>, A> FromDsl<State> for Align<A> {
fn take_from <'state, 'source: 'state> (state: &'state State, iter: &mut TokenIter<'source>)
-> Perhaps<Self>
{
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
match key {
"align/c"|"align/x"|"align/y"|
"align/n"|"align/s"|"align/e"|"align/w"|
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = iter.next().unwrap();
let content = state.take_or_fail(&mut iter.clone(), "expected content")?;
return Ok(Some(match key {
"align/c" => Self::c(content),
"align/x" => Self::x(content),
"align/y" => Self::y(content),
"align/n" => Self::n(content),
"align/s" => Self::s(content),
"align/e" => Self::e(content),
"align/w" => Self::w(content),
"align/nw" => Self::nw(content),
"align/ne" => Self::ne(content),
"align/sw" => Self::sw(content),
"align/se" => Self::se(content),
_ => unreachable!()
}))
},
_ => return Ok(None)
}
} else {
Ok(None)
}
from_dsl!(@a: Align<A>: |state, iter|if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
match key {
"align/c"|"align/x"|"align/y"|
"align/n"|"align/s"|"align/e"|"align/w"|
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = iter.next().unwrap();
let content = state.take_or_fail(&mut iter.clone(), "expected content")?;
return Ok(Some(match key {
"align/c" => Self::c(content),
"align/x" => Self::x(content),
"align/y" => Self::y(content),
"align/n" => Self::n(content),
"align/s" => Self::s(content),
"align/e" => Self::e(content),
"align/w" => Self::w(content),
"align/nw" => Self::nw(content),
"align/ne" => Self::ne(content),
"align/sw" => Self::sw(content),
"align/se" => Self::se(content),
_ => unreachable!()
}))
},
_ => return Ok(None)
}
}
} else {
Ok(None)
});
impl<A> Align<A> {
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
@ -85,7 +79,7 @@ impl<A> Align<A> {
}
impl<E: Output, A: Content<E>> Content<E> for Align<A> {
fn content (&self) -> impl Render<E> {
fn content (&self) -> impl Render<E> + '_ {
&self.1
}
fn layout (&self, on: E::Area) -> E::Area {

View file

@ -21,39 +21,35 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
}
}
#[cfg(feature = "dsl")]
impl<A, B, T: Dsl<A> + Dsl<B>> FromDsl<T> for Bsp<A, B> {
fn take_from <'state, 'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
Ok(if let Some(Token {
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
..
}) = iter.peek() {
let base = iter.clone();
return Ok(Some(match iter.next() {
Some(Token { value: Value::Key("bsp/n"), .. }) =>
Self::n(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/s"), .. }) =>
Self::s(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/e"), .. }) =>
Self::e(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/w"), .. }) =>
Self::w(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/a"), .. }) =>
Self::a(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/b"), .. }) =>
Self::b(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
_ => unreachable!(),
}))
} else {
None
})
}
}
from_dsl!(@ab: Bsp<A, B>: |state, iter|Ok(if let Some(Token {
value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"),
..
}) = iter.peek() {
let base = iter.clone();
return Ok(Some(match iter.next() {
Some(Token { value: Value::Key("bsp/n"), .. }) =>
Self::n(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/s"), .. }) =>
Self::s(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/e"), .. }) =>
Self::e(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/w"), .. }) =>
Self::w(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/a"), .. }) =>
Self::a(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
Some(Token { value: Value::Key("bsp/b"), .. }) =>
Self::b(state.take_or_fail(iter, "expected content 1")?,
state.take_or_fail(iter, "expected content 2")?),
_ => unreachable!(),
}))
} else {
None
}));
impl<A, B> Bsp<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) }

View file

@ -18,8 +18,8 @@ impl<A, B> Either<A, B> {
}
}
#[cfg(feature = "dsl")]
impl<A, T: Dsl<bool> + Dsl<A>> FromDsl<T> for When<A> {
fn take_from <'state, 'source: 'state> (
impl<'source, A, T: Dsl<bool> + Dsl<A>> FromDsl<'source, T> for When<A> {
fn take_from <'state> (
state: &'state T,
token: &mut TokenIter<'source>
) -> Perhaps<Self> {
@ -56,8 +56,8 @@ impl<E: Output, A: Render<E>> Content<E> for When<A> {
}
}
#[cfg(feature = "dsl")]
impl<A, B, T: Dsl<bool> + Dsl<A> + Dsl<B>> FromDsl<T> for Either<A, B> {
fn take_from <'state, 'source: 'state> (
impl<'source, A, B, T: Dsl<bool> + Dsl<A> + Dsl<B>> FromDsl<'source, T> for Either<A, B> {
fn take_from <'state> (
state: &'state T,
token: &mut TokenIter<'source>
) -> Perhaps<Self> {

View file

@ -2,72 +2,67 @@ use crate::*;
use std::marker::PhantomData;
/// Lazily-evaluated [Render]able.
pub struct Thunk<E: Output, T: Render<E>, F: Fn()->T + Send + Sync>(
pub struct Thunk<E: Output, T: Render<E>, F: Fn()->T>(
PhantomData<E>,
F
);
impl<E: Output, T: Render<E>, F: Fn()->T + Send + Sync> Thunk<E, T, F> {
impl<E: Output, T: Render<E>, F: Fn()->T> Thunk<E, T, F> {
pub const fn new (thunk: F) -> Self {
Self(PhantomData, thunk)
}
}
impl<E: Output, T: Render<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E, T, F> {
impl<E: Output, T: Render<E>, F: Fn()->T> Content<E> for Thunk<E, T, F> {
fn content (&self) -> impl Render<E> { (self.1)() }
}
pub struct ThunkBox<'a, E: Output>(
pub struct ThunkBox<E: Output>(
PhantomData<E>,
Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>
Box<dyn Fn()->Box<dyn Render<E>>>,
);
impl<'a, E: Output> ThunkBox<'a, E> {
pub const fn new (thunk: Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>) -> Self {
impl<E: Output> ThunkBox<E> {
pub const fn new (thunk: Box<dyn Fn()->Box<dyn Render<E>>>) -> Self {
Self(PhantomData, thunk)
}
}
impl<'a, E: Output> Content<E> for ThunkBox<'a, E> {
fn content (&self) -> impl Render<E> { (self.1)() }
impl<E: Output> Content<E> for ThunkBox<E> {
fn content (&self) -> impl Render<E> { (&self.1)() }
}
impl<'a, E, F, T> From<F> for ThunkBox<'a, E>
where
E: Output,
F: Fn()->T + Send + Sync + 'a,
T: Render<E> + Send + Sync + 'a
{
fn from (f: F) -> Self {
Self(PhantomData, Box::new(move||f().boxed()))
impl<E: Output> From<Box<dyn Fn()->Box<dyn Render<E>>>> for ThunkBox<E> {
fn from (f: Box<dyn Fn()->Box<dyn Render<E>>>) -> Self {
Self(PhantomData, f)
}
}
//impl<'a, E: Output, F: Fn()->Box<dyn Render<E> + 'a> + Send + Sync + 'a> From<F> for ThunkBox<'a, E> {
//impl<'a, E: Output, F: Fn()->Box<dyn Render<E> + 'a> + 'a> From<F> for ThunkBox<'a, E> {
//fn from (f: F) -> Self {
//Self(Default::default(), Box::new(f))
//}
//}
pub struct ThunkRender<E: Output, F: Fn(&mut E) + Send + Sync>(PhantomData<E>, F);
impl<E: Output, F: Fn(&mut E) + Send + Sync> ThunkRender<E, F> {
pub struct ThunkRender<E: Output, F: Fn(&mut E)>(PhantomData<E>, F);
impl<E: Output, F: Fn(&mut E)> ThunkRender<E, F> {
pub fn new (render: F) -> Self { Self(PhantomData, render) }
}
impl<E: Output, F: Fn(&mut E) + Send + Sync> Content<E> for ThunkRender<E, F> {
impl<E: Output, F: Fn(&mut E)> Content<E> for ThunkRender<E, F> {
fn render (&self, to: &mut E) { (self.1)(to) }
}
pub struct ThunkLayout<
E: Output,
F1: Fn(E::Area)->E::Area + Send + Sync,
F2: Fn(&mut E) + Send + Sync
F1: Fn(E::Area)->E::Area,
F2: Fn(&mut E)
>(
PhantomData<E>,
F1,
F2
);
impl<E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> ThunkLayout<E, F1, F2> {
impl<E: Output, F1: Fn(E::Area)->E::Area, F2: Fn(&mut E)> ThunkLayout<E, F1, F2> {
pub fn new (layout: F1, render: F2) -> Self { Self(PhantomData, layout, render) }
}
impl<E, F1, F2> Content<E> for ThunkLayout<E, F1, F2>
where
E: Output,
F1: Fn(E::Area)->E::Area + Send + Sync,
F2: Fn(&mut E) + Send + Sync
F1: Fn(E::Area)->E::Area,
F2: Fn(&mut E)
{
fn layout (&self, to: E::Area) -> E::Area { (self.1)(to) }
fn render (&self, to: &mut E) { (self.2)(to) }

View file

@ -33,8 +33,8 @@ macro_rules! transform_xy {
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
}
#[cfg(feature = "dsl")]
impl<A, T: Dsl<A>> FromDsl<T> for $Enum<A> {
fn take_from <'state, 'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
impl<'source, A, T: Dsl<A>> FromDsl<'source, T> for $Enum<A> {
fn take_from <'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
if let Some(Token { value: Value::Key(k), .. }) = iter.peek() {
let mut base = iter.clone();
return Ok(Some(match iter.next() {
@ -51,7 +51,7 @@ macro_rules! transform_xy {
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
fn content (&self) -> impl Render<E> {
fn content (&self) -> impl Render<E> + '_ {
match self {
Self::X(item) => item,
Self::Y(item) => item,
@ -77,8 +77,8 @@ macro_rules! transform_xy_unit {
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
}
#[cfg(feature = "dsl")]
impl<A, U: Coordinate, T: Dsl<A> + Dsl<U>> FromDsl<T> for $Enum<U, A> {
fn take_from <'state, 'source: 'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
impl<'source, A, U: Coordinate, T: Dsl<A> + Dsl<U>> FromDsl<'source, T> for $Enum<U, A> {
fn take_from <'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps<Self> {
Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = iter.peek() {
let mut base = iter.clone();
Some(match iter.next() {
@ -103,7 +103,7 @@ macro_rules! transform_xy_unit {
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
fn content (&self) -> impl Render<E> {
fn content (&self) -> impl Render<E> + '_ {
Some(match self {
Self::X(_, content) => content,
Self::Y(_, content) => content,

View file

@ -29,8 +29,8 @@ pub trait Render<E: Output> {
/// Write data to display.
fn render (&self, output: &mut E);
/// Perform type erasure, turning `self` into an opaque [RenderBox].
fn boxed <'a> (self) -> RenderBox<'a, E> where Self: Send + Sync + Sized + 'a {
Box::new(self) as RenderBox<'a, E>
fn boxed <'a> (self) -> Box<dyn Render<E> + 'a> where Self: Sized + 'a {
Box::new(self) as Box<dyn Render<E> + 'a>
}
}
@ -47,20 +47,20 @@ impl<E: Output, C: Content<E>> Render<E> for C {
/// Opaque pointer to a renderable living on the heap.
///
/// Return this from [Content::content] to use dynamic dispatch.
pub type RenderBox<'a, E> = Box<RenderDyn<'a, E>>;
pub type RenderBox<E> = Box<RenderDyn<E>>;
/// You can render from a box.
impl<'a, E: Output> Content<E> for RenderBox<'a, E> {
fn content (&self) -> impl Render<E> { self.deref() }
impl<E: Output> Content<E> for RenderBox<E> {
fn content (&self) -> impl Render<E> + '_ { self.deref() }
//fn boxed <'b> (self) -> RenderBox<'b, E> where Self: Sized + 'b { self }
}
/// Opaque pointer to a renderable.
pub type RenderDyn<'a, E> = dyn Render<E> + Send + Sync + 'a;
pub type RenderDyn<E> = dyn Render<E>;
/// You can render from an opaque pointer.
impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
fn content (&self) -> impl Render<E> {
impl<E: Output> Content<E> for &RenderDyn<E> where Self: Sized {
fn content (&self) -> impl Render<E> + '_ {
#[allow(suspicious_double_ref_op)]
self.deref()
}
@ -77,7 +77,7 @@ impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
/// Composable renderable with static dispatch.
pub trait Content<E: Output> {
/// Return a [Render]able of a specific type.
fn content (&self) -> impl Render<E> { () }
fn content (&self) -> impl Render<E> + '_ { () }
/// Perform layout. By default, delegates to [Self::content].
fn layout (&self, area: E::Area) -> E::Area { self.content().layout(area) }
/// Draw to output. By default, delegates to [Self::content].
@ -86,7 +86,7 @@ pub trait Content<E: Output> {
/// Every pointer to [Content] is a [Content].
impl<E: Output, C: Content<E>> Content<E> for &C {
fn content (&self) -> impl Render<E> { (*self).content() }
fn content (&self) -> impl Render<E> + '_ { (*self).content() }
fn layout (&self, area: E::Area) -> E::Area { (*self).layout(area) }
fn render (&self, output: &mut E) { (*self).render(output) }
}
@ -98,7 +98,7 @@ impl<E: Output> Content<E> for () {
}
impl<E: Output, T: Content<E>> Content<E> for Option<T> {
fn content (&self) -> impl Render<E> {
fn content (&self) -> impl Render<E> + '_ {
self.as_ref()
}
fn layout (&self, area: E::Area) -> E::Area {
@ -117,7 +117,7 @@ impl<E: Output, T: Content<E>> Content<E> for Option<T> {
// Implement for all [Output]s.
(|$self:ident:$Struct:ty| $content:expr) => {
impl<E: Output> Content<E> for $Struct {
fn content (&$self) -> impl Render<E> { Some($content) }
fn content (&$self) -> impl Render<E> + '_ { Some($content) }
}
};
// Implement for specific [Output].
@ -127,7 +127,7 @@ impl<E: Output, T: Content<E>> Content<E> for Option<T> {
|$content:expr) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn content (&$self) -> impl Render<$Output> { $content }
fn content (&$self) -> impl Render<$Output> + '_ { $content }
}
};
}