difftreelog
refactor drop impl ArgsLike for HashMap
in: master
2 files changed
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth89 }89 }90}90}9192// TODO: Is this implementation really required, as there is no Context to use?93// Maybe something a bit stricter is possible to add, especially with precompiled calls?94impl ArgLike for TlaArg {95 fn evaluate_arg(&self, _ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {96 if tailstrict {97 self.evaluate_tailstrict().map(Thunk::evaluated)98 } else {99 self.evaluate()100 }101 }102}10391104pub trait ArgsLike {92pub trait ArgsLike {105 fn unnamed_len(&self) -> usize;93 fn unnamed_len(&self) -> usize;208 }196 }209}197}210211impl<V: ArgLike, S> ArgsLike for HashMap<IStr, V, S> {212 fn unnamed_len(&self) -> usize {213 0214 }215216 fn unnamed_iter(217 &self,218 _ctx: Context,219 _tailstrict: bool,220 _handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,221 ) -> Result<()> {222 Ok(())223 }224225 fn named_iter(226 &self,227 ctx: Context,228 tailstrict: bool,229 handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,230 ) -> Result<()> {231 for (name, value) in self {232 handler(name, value.evaluate_arg(ctx.clone(), tailstrict)?)?;233 }234 Ok(())235 }236237 fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {238 for name in self.keys() {239 handler(name);240 }241 }242243 fn is_empty(&self) -> bool {244 self.is_empty()245 }246}247248macro_rules! impl_args_like {249 ($count:expr; $($gen:ident)*) => {250 impl<$($gen: ArgLike,)*> ArgsLike for ($($gen,)*) {251 fn unnamed_len(&self) -> usize {252 $count253 }254 #[allow(non_snake_case, unused_assignments)]255 fn unnamed_iter(256 &self,257 ctx: Context,258 tailstrict: bool,259 handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,260 ) -> Result<()> {261 let mut i = 0usize;262 let ($($gen,)*) = self;263 $(264 handler(i, $gen.evaluate_arg(ctx.clone(), tailstrict)?)?;265 i+=1;266 )*267 Ok(())268 }269 fn named_iter(270 &self,271 _ctx: Context,272 _tailstrict: bool,273 _handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,274 ) -> Result<()> {275 Ok(())276 }277 fn named_names(&self, _handler: &mut dyn FnMut(&IStr)) {}278279 fn is_empty(&self) -> bool {280 // impl_args_like only implements non-empty tuples.281 false282 }283 }284 };285 ($count:expr; $($cur:ident)* @ $c:ident $($rest:ident)*) => {286 impl_args_like!($count; $($cur)*);287 impl_args_like!($count + 1usize; $($cur)* $c @ $($rest)*);288 };289 ($count:expr; $($cur:ident)* @) => {290 impl_args_like!($count; $($cur)*);291 }292}293impl_args_like! {294 // First argument is already in position, so count starts from 1295 1usize; A @ B C D E F G H I J K L296}297298impl ArgsLike for () {299 fn unnamed_len(&self) -> usize {300 0301 }302303 fn unnamed_iter(304 &self,305 _ctx: Context,306 _tailstrict: bool,307 _handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,308 ) -> Result<()> {309 Ok(())310 }311312 fn named_iter(313 &self,314 _ctx: Context,315 _tailstrict: bool,316 _handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,317 ) -> Result<()> {318 Ok(())319 }320321 fn named_names(&self, _handler: &mut dyn FnMut(&IStr)) {}322 fn is_empty(&self) -> bool {323 true324 }325}326198crates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -1,11 +1,10 @@
use std::{collections::HashMap, hash::BuildHasher};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::Source;
use crate::{
- function::{CallLocation, TlaArg},
- in_description_frame, with_state, Result, Val,
+ function::{CallLocation, PreparedFuncVal, TlaArg},
+ in_description_frame, Result, Val,
};
pub fn apply_tla<H: BuildHasher>(args: &HashMap<IStr, TlaArg, H>, val: Val) -> Result<Val> {
@@ -13,17 +12,14 @@
in_description_frame(
|| "during TLA call".to_owned(),
|| {
- func.evaluate(
- with_state(|s| {
- s.create_default_context(Source::new_virtual(
- "<top-level-arg>".into(),
- IStr::empty(),
- ))
- }),
- CallLocation::native(),
- args,
- false,
- )
+ let mut names = Vec::with_capacity(args.len());
+ let mut values = Vec::with_capacity(args.len());
+ for (name, value) in args {
+ names.push(name.clone());
+ values.push(value.evaluate()?);
+ }
+ let prepared = PreparedFuncVal::new(func, 0, &names)?;
+ prepared.call(CallLocation::native(), &[], &values)
},
)?
} else {