proc: builtims
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-25 11:43:35 +03:00
parent 3e1084555b
commit 31e84bf5b3
3 changed files with 55 additions and 68 deletions

View file

@ -84,14 +84,6 @@ impl ToTokens for ExposeImpl {
quote! { Some(::tengri::dsl::Value::Sym(#key)) => state.#value(), }
});
write_quote_to(out, quote! {
/// Generated by [tengri_proc::expose].
impl<'n> ::tengri::dsl::Give<'n, #t> for #state {
fn give <'source: 'n> (
&self, words: ::tengri::dsl::TokenIter<'source>
) -> Perhaps<#t> {
Take::take(self, words)
}
}
/// Generated by [tengri_proc::expose].
impl<'n> ::tengri::dsl::Take<'n, #state> for #t {
fn take <'source: 'n> (

View file

@ -43,7 +43,7 @@ impl ToTokens for ViewDef {
let self_ty = &block.self_ty;
// Expressions are handled by built-in functions
// that operate over constants and symbols.
let builtin = builtins_with_types().map(|ty|write_quote(quote! { #ty }));
let builtin = builtins_with_boxes_output(quote! { #output });
// Symbols are handled by user-taked functions
// that take no parameters but `&self`.
let exposed = exposed.iter().map(|(key, value)|write_quote(quote! {
@ -52,18 +52,18 @@ impl ToTokens for ViewDef {
write_quote_to(out, quote! {
/// Generated by [tengri_proc].
///
/// Gives [#self_ty] the ability to construct the [Render]able
/// which might corresponds to a given [TokenStream],
/// Makes [#self_ty] able to construct the [Render]able
/// which might correspond to a given [TokenStream],
/// while taking [#self_ty]'s state into consideration.
impl<'state> Give<'state, Box<dyn Render<#output> + 'state>> for #self_ty {
fn give <'source: 'state> (&self, mut words: TokenIter<'source>)
impl<'state: 'static> Take<'state, #self_ty> for Box<dyn Render<#output> + 'state> {
fn take <'source: 'state> (state: &#self_ty, mut words: TokenIter<'source>)
-> Perhaps<Box<dyn Render<#output> + 'state>>
{
let state = self;
//let state = self;
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
match value {
#(::tengri::dsl::Value::Exp(_, expr) => {
#builtin::take(state, expr)?.map(|value|value.boxed())
Give::<'state, #builtin>::give(state, expr)?.map(|value|value.boxed())
},)*
#(
#exposed,
@ -91,21 +91,33 @@ impl ToTokens for ViewDef {
}
}
fn builtins_with_types () -> impl Iterator<Item=TokenStream2> {
fn builtins_with_holes () -> impl Iterator<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { _ })
}
fn builtins_with_boxes () -> impl Iterator<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<_>+'state> })
}
fn builtins_with_boxes_output (o: TokenStream2) -> impl Iterator<Item=TokenStream2> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<#o>+'state> })
}
fn builtins_with (n: TokenStream2, c: TokenStream2) -> impl Iterator<Item=TokenStream2> {
[
quote! { When::< Box<dyn Render<_>> > },
quote! { Either::< Box<dyn Render<_>>, Box<dyn Render<_>>> },
quote! { Align::< Box<dyn Render<_>> > },
quote! { Bsp::< Box<dyn Render<_>>, Box<dyn Render<_>>> },
quote! { Fill::< Box<dyn Render<_>> > },
quote! { Fixed::<_, Box<dyn Render<_>> > },
quote! { Min::<_, Box<dyn Render<_>> > },
quote! { Max::<_, Box<dyn Render<_>> > },
quote! { Shrink::<_, Box<dyn Render<_>> > },
quote! { Expand::<_, Box<dyn Render<_>> > },
quote! { Push::<_, Box<dyn Render<_>> > },
quote! { Pull::<_, Box<dyn Render<_>> > },
quote! { Margin::<_, Box<dyn Render<_>> > },
quote! { Padding::<_, Box<dyn Render<_>> > },
quote! { When::< #c > },
quote! { Either::< #c, #c> },
quote! { Align::< #c > },
quote! { Bsp::< #c, #c> },
quote! { Fill::< #c > },
quote! { Fixed::<#n, #c > },
quote! { Min::<#n, #c > },
quote! { Max::<#n, #c > },
quote! { Shrink::<#n, #c > },
quote! { Expand::<#n, #c > },
quote! { Push::<#n, #c > },
quote! { Pull::<#n, #c > },
quote! { Margin::<#n, #c > },
quote! { Padding::<#n, #c > },
].into_iter()
}