git.delta.rocks / jrsonnet / refs/commits / 78d45eb4e76f

difftreelog

feat treat trivial identity function as so

Yaroslav Bolyukin2022-10-25parent: #284612b.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
4use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_gcmodule::{Cc, Trace};
5use jrsonnet_interner::IStr;5use jrsonnet_interner::IStr;
6pub use jrsonnet_macros::builtin;6pub use jrsonnet_macros::builtin;
7use jrsonnet_parser::{ExprLocation, LocExpr, ParamsDesc};7use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};
88
9use self::{9use self::{
10 builtin::{Builtin, StaticBuiltin},10 builtin::{Builtin, StaticBuiltin},
176176
177 /// Is this function an indentity function.177 /// Is this function an indentity function.
178 ///178 ///
179 /// Currently only works for builtin `std.id`, aka `Self::Id` value, `function(x) x` defined by jsonnet will not count as identity.179 /// Currently only works for builtin `std.id`, aka `Self::Id` value, and `function(x) x`.
180 ///
181 /// This function should only be used for optimization, not for the conditional logic, i.e code should work with syntetic identity function too
180 pub const fn is_identity(&self) -> bool {182 pub fn is_identity(&self) -> bool {
181 matches!(self, Self::Id)183 if matches!(self, Self::Id) {
184 return true;
185 }
186
187 match self {
188 Self::Id => true,
189 Self::Normal(desc) => {
190 if desc.params.len() != 1 {
191 return false;
192 }
193 let param = &desc.params[0];
194 if param.1.is_some() {
195 return false;
196 }
197 #[allow(clippy::infallible_destructuring_match)]
198 let id = match &param.0 {
199 Destruct::Full(id) => id,
200 #[cfg(feature = "exp-destruct")]
201 _ => return false,
202 };
203 &desc.body.0 as &Expr == &Expr::Var(id.clone())
204 }
205 _ => false,
206 }
182 }207 }
183 /// Identity function value.208 /// Identity function value.
184 pub const fn identity() -> Self {209 pub const fn identity() -> Self {