iterator being const when not needed

This commit is contained in:
🪞👃🪞 2025-01-18 23:58:18 +01:00
parent 67148a4aa4
commit a595e2e895
4 changed files with 36 additions and 46 deletions

View file

@ -1,7 +1,7 @@
use crate::*;
pub trait TryFromAtom<'a, T>: Sized {
fn try_from_atom (state: &'a T, value: Value<'a>) -> Option<Self> {
if let Value::Exp(0, iter) = value { return Self::try_from_expr(state, iter) }
if let Exp(0, iter) = value { return Self::try_from_expr(state, iter) }
None
}
fn try_from_expr (_state: &'a T, _iter: TokenIter<'a>) -> Option<Self> {
@ -43,10 +43,8 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
impl Context<$type> for $State {
#[allow(unreachable_code)]
fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom {
$(Value::Sym($pat) => $expr,)*
_ => return None
})
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
@ -55,10 +53,8 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
impl<$lt> Context<$lt, $type> for $State {
#[allow(unreachable_code)]
fn get (&$lt $self, atom: &Value) -> Option<$type> {
Some(match atom {
$(Value::Sym($pat) => $expr,)*
_ => return None
})
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* _ => return None })
}
}
};
@ -71,11 +67,8 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom {
$(Value::Sym($pat) => $expr,)*
Value::Num(n) => *n as $type,
_ => return None
})
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
}
}
};
@ -83,11 +76,8 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom {
$(Value::Sym($pat) => $expr,)*
Value::Num(n) => *n as $type,
_ => return None
})
use Value::*;
Some(match atom { $(Sym($pat) => $expr,)* Num(n) => *n as $type, _ => return None })
}
}
};
@ -100,11 +90,12 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, atom: &Value) -> Option<$type> {
use Value::*;
Some(match atom {
Value::Num(n) => match *n { 0 => false, _ => true },
Value::Sym(":false") | Value::Sym(":f") => false,
Value::Sym(":true") | Value::Sym(":t") => true,
$(Value::Sym($pat) => $expr,)*
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return Context::get(self, atom)
})
}
@ -114,11 +105,12 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
fn get (&$self, atom: &Value) -> Option<$type> {
use Value::*;
Some(match atom {
Value::Num(n) => match *n { 0 => false, _ => true },
Value::Sym(":false") | Value::Sym(":f") => false,
Value::Sym(":true") | Value::Sym(":t") => true,
$(Value::Sym($pat) => $expr,)*
Num(n) => match *n { 0 => false, _ => true },
Sym(":false") | Sym(":f") => false,
Sym(":true") | Sym(":t") => true,
$(Sym($pat) => $expr,)*
_ => return None
})
}

View file

@ -32,7 +32,6 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
}
}
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
panic!("bsp:\n{iter:#?}\n{:#?}", iter.peek());
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
let iter = iter.clone().next().unwrap().1;
match key {

View file

@ -7,7 +7,7 @@ pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> { pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
if let Some(Token { value: Value::Key("when"), .. }) = iter.peek() {
let iter = iter.clone();
let iter = iter.clone().next().unwrap().1;
let condition = iter.next();
if let Some((ref condition, _)) = condition {
let condition = state.get(&condition.value).expect("no condition");
@ -20,7 +20,7 @@ try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
});
try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some((Token { value: Value::Key("either"), .. }, _)) = iter.next() {
let iter = iter.clone();
let iter = iter.clone().next().unwrap().1;
let condition = iter.next();
if let Some((ref condition, _)) = condition {
let condition = state.get(&condition.value).expect("no condition");

View file

@ -1,9 +1,6 @@
use crate::*;
/// Defines an enum that transforms its content
/// along either the X axis, the Y axis, or both.
///
/// The `_Unused` variant wraps the `Output` type
/// 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<T> { X(T), Y(T), XY(T) }
@ -15,11 +12,12 @@ macro_rules! transform_xy {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
for $Enum<RenderBox<'a, E>> {
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {
Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
let iter = iter.clone();
Some(if let Some((Token { value: Value::Key($x), .. }, iter)) = iter.next() {
Self::x(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else if let Some((Token { value: Value::Key($y), .. }, _)) = iter.next() {
} else if let Some((Token { value: Value::Key($y), .. }, iter)) = iter.next() {
Self::y(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else if let Some((Token { value: Value::Key($xy), .. }, _)) = iter.next() {
} else if let Some((Token { value: Value::Key($xy), .. }, iter)) = iter.next() {
Self::xy(state.get_content(&iter.next().expect("no content").0.value).expect("no content"))
} else {
return None
@ -41,6 +39,7 @@ macro_rules! transform_xy {
}
}
}
/// Defines an enum that parametrically transforms its content
/// along either the X axis, the Y axis, or both.
macro_rules! transform_xy_unit {
@ -54,18 +53,18 @@ macro_rules! transform_xy_unit {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {
Some(if let Some((Token { value: Value::Key($x), .. }, _)) = iter.next() {
let x = state.get(&iter.next().expect("no x").0.value).expect("no x");
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
Some(if let Some((Token { value: Value::Key($x), .. }, iter)) = iter.next() {
let (x, iter) = iter.next().map(|(x, iter)|(state.get(&x.value).expect("no x provided"), iter)).expect("no x specified");
let (c, _) = iter.next().map(|(x, iter)|(state.get_content(&x.value).expect("no content provided"), iter)).expect("no content specified");
Self::x(x, c)
} else if let Some((Token { value: Value::Key($y), .. }, _)) = iter.next() {
let y = state.get(&iter.next().expect("no y").0.value).expect("no y");
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
} else if let Some((Token { value: Value::Key($y), .. }, iter)) = iter.next() {
let (y, iter) = iter.next().map(|(y, iter)|(state.get(&y.value).expect("no y provided"), iter)).expect("no y specified");
let (c, _) = iter.next().map(|(x, iter)|(state.get_content(&x.value).expect("no content provided"), iter)).expect("no content specified");
Self::y(y, c)
} else if let Some((Token { value: Value::Key($xy), .. }, _)) = iter.next() {
let x = state.get(&iter.next().expect("no x").0.value).expect("no x");
let y = state.get(&iter.next().expect("no y").0.value).expect("no y");
let c = state.get_content(&iter.next().expect("no content").0.value).expect("no content");
} else if let Some((Token { value: Value::Key($xy), .. }, iter)) = iter.next() {
let (x, iter) = iter.next().map(|(x, iter)|(state.get(&x.value).expect("no x provided"), iter)).expect("no x specified");
let (y, iter) = iter.next().map(|(y, iter)|(state.get(&y.value).expect("no y provided"), iter)).expect("no y specified");
let (c, _) = iter.next().map(|(x, iter)|(state.get_content(&x.value).expect("no content provided"), iter)).expect("no content specified");
Self::xy(x, y, c)
} else {
return None