mirror of
https://github.com/denoland/deno.git
synced 2025-02-12 16:59:32 -05:00
chore(core): simplify extension config codegen (#18263)
Avoid creating an extra closure when no config needed. I eventually want to make OpStateFn fixed sized to avoid boxing it.
This commit is contained in:
parent
058865c9a3
commit
9bfa8dc90c
1 changed files with 17 additions and 48 deletions
|
@ -211,13 +211,12 @@ macro_rules! extension {
|
||||||
fn with_ops $( < $( $param : $type + 'static ),+ > )?(ext: &mut $crate::ExtensionBuilder) {
|
fn with_ops $( < $( $param : $type + 'static ),+ > )?(ext: &mut $crate::ExtensionBuilder) {
|
||||||
// If individual ops are specified, roll them up into a vector and apply them
|
// If individual ops are specified, roll them up into a vector and apply them
|
||||||
$(
|
$(
|
||||||
let v = vec![
|
ext.ops(vec![
|
||||||
$(
|
$(
|
||||||
$( #[ $m ] )*
|
$( #[ $m ] )*
|
||||||
$( $op )::+ :: decl $( :: <$op_param> )? ()
|
$( $op )::+ :: decl $( :: <$op_param> )? ()
|
||||||
),+
|
),+
|
||||||
];
|
]);
|
||||||
ext.ops(v);
|
|
||||||
)?
|
)?
|
||||||
|
|
||||||
// Otherwise use the ops_fn, if provided
|
// Otherwise use the ops_fn, if provided
|
||||||
|
@ -228,14 +227,7 @@ macro_rules! extension {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
fn with_state_and_middleware$( < $( $param : $type + 'static ),+ > )?(ext: &mut $crate::ExtensionBuilder, $( $( $options_id : $options_type ),* )? ) {
|
fn with_state_and_middleware$( < $( $param : $type + 'static ),+ > )?(ext: &mut $crate::ExtensionBuilder, $( $( $options_id : $options_type ),* )? ) {
|
||||||
#[allow(unused_variables)]
|
$crate::extension!(! __config__ ext $( parameters = [ $( $param : $type ),* ] )? $( config = { $( $options_id : $options_type ),* } )? $( state_fn = $state_fn )? );
|
||||||
let config = $crate::extension!(! __config__ $( parameters = [ $( $param : $type ),* ] )? $( config = { $( $options_id : $options_type ),* } )? );
|
|
||||||
|
|
||||||
$(
|
|
||||||
ext.state(move |state: &mut $crate::OpState| {
|
|
||||||
config.call_callback(state, $state_fn)
|
|
||||||
});
|
|
||||||
)?
|
|
||||||
|
|
||||||
$(
|
$(
|
||||||
ext.event_loop_middleware($event_loop_middleware_fn);
|
ext.event_loop_middleware($event_loop_middleware_fn);
|
||||||
|
@ -285,50 +277,27 @@ macro_rules! extension {
|
||||||
};
|
};
|
||||||
|
|
||||||
// This branch of the macro generates a config object that calls the state function with itself.
|
// This branch of the macro generates a config object that calls the state function with itself.
|
||||||
(! __config__ $( parameters = [ $( $param:ident : $type:ident ),+ ] )? config = { $( $options_id:ident : $options_type:ty ),* } ) => {
|
(! __config__ $ext:ident $( parameters = [ $( $param:ident : $type:ident ),+ ] )? config = { $( $options_id:ident : $options_type:ty ),* } $( state_fn = $state_fn:expr )? ) => {
|
||||||
{
|
{
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
struct Config $( < $( $param : $type + 'static ),+ > )? {
|
struct Config $( < $( $param : $type + 'static ),+ > )? {
|
||||||
$( pub $options_id : $options_type , )*
|
$( pub $options_id : $options_type , )*
|
||||||
$( __phantom_data: ::std::marker::PhantomData<($( $param ),+)>, )?
|
$( __phantom_data: ::std::marker::PhantomData<($( $param ),+)>, )?
|
||||||
}
|
}
|
||||||
|
let config = Config {
|
||||||
impl $( < $( $param : $type + 'static ),+ > )? Config $( < $( $param ),+ > )? {
|
|
||||||
/// Call a function of |state, cfg| using this configuration structure.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[inline(always)]
|
|
||||||
fn call_callback<F: Fn(&mut $crate::OpState, Self)>(self, state: &mut $crate::OpState, f: F) {
|
|
||||||
f(state, self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Config {
|
|
||||||
$( $options_id , )*
|
$( $options_id , )*
|
||||||
$( __phantom_data: ::std::marker::PhantomData::<($( $param ),+)>::default() )?
|
$( __phantom_data: ::std::marker::PhantomData::<($( $param ),+)>::default() )?
|
||||||
}
|
};
|
||||||
|
|
||||||
|
let state_fn: fn(&mut $crate::OpState, Config $( < $( $param ),+ > )? ) = $( $state_fn )?;
|
||||||
|
$ext.state(move |state: &mut $crate::OpState| {
|
||||||
|
state_fn(state, config);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// This branch of the macro generates an empty config object that doesn't actually make any callbacks on the state function.
|
(! __config__ $ext:ident $( parameters = [ $( $param:ident : $type:ident ),+ ] )? $( state_fn = $state_fn:expr )? ) => {
|
||||||
(! __config__ $( parameters = [ $( $param:ident : $type:ident ),+ ] )? ) => {
|
$( $ext.state($state_fn); )?
|
||||||
{
|
|
||||||
#[doc(hidden)]
|
|
||||||
struct Config {
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
/// Call a function of |state| using the fields of this configuration structure.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[inline(always)]
|
|
||||||
fn call_callback<F: Fn(&mut $crate::OpState)>(self, state: &mut $crate::OpState, f: F) {
|
|
||||||
f(state)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Config {}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
(! __ops__ $ext:ident __eot__) => {
|
(! __ops__ $ext:ident __eot__) => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue