git.delta.rocks / jrsonnet / refs/commits / cb765629121c

difftreelog

style fix clippy warnings

Yaroslav Bolyukin2023-07-27parent: #5dc3b98.patch.diff
in: master

11 files changed

modifiedcrates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -22,7 +22,7 @@
 		self.0
 			.set(value)
 			.map_err(|_| ())
-			.expect("wrapper is filled already")
+			.expect("wrapper is filled already");
 	}
 }
 impl<T: Clone + Trace + 'static> Pending<T> {
@@ -53,8 +53,8 @@
 	}
 }
 
-impl<T: Trace + Clone> Into<Thunk<T>> for Pending<T> {
-	fn into(self) -> Thunk<T> {
-		Thunk::new(self)
+impl<T: Trace + Clone> From<Pending<T>> for Thunk<T> {
+	fn from(value: Pending<T>) -> Self {
+		Self::new(value)
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -294,7 +294,7 @@
 	// We have single context for all fields, so we can cache binds
 	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));
 
-	for member in members.iter() {
+	for member in members {
 		match member {
 			Member::Field(field) => {
 				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;
modifiedcrates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -210,14 +210,14 @@
 		tailstrict: bool,
 		handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
 	) -> Result<()> {
-		for (name, value) in self.iter() {
+		for (name, value) in self {
 			handler(name, value.evaluate_arg(ctx.clone(), tailstrict)?)?;
 		}
 		Ok(())
 	}
 
 	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
-		for (name, _) in self.iter() {
+		for (name, _) in self {
 			handler(name);
 		}
 	}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5	clippy::all,6	clippy::nursery,7	clippy::pedantic,8	// missing_docs,9	elided_lifetimes_in_paths,10	explicit_outlives_requirements,11	noop_method_call,12	single_use_lifetimes,13	variant_size_differences,14	rustdoc::all15)]16#![allow(17	macro_expanded_macro_exports_accessed_by_absolute_paths,18	clippy::ptr_arg,19	// Too verbose20	clippy::must_use_candidate,21	// A lot of functions pass around errors thrown by code22	clippy::missing_errors_doc,23	// A lot of pointers have interior Rc24	clippy::needless_pass_by_value,25	// Its fine26	clippy::wildcard_imports,27	clippy::enum_glob_use,28	clippy::module_name_repetitions,29	// TODO: fix individual issues, however this works as intended almost everywhere30	clippy::cast_precision_loss,31	clippy::cast_possible_wrap,32	clippy::cast_possible_truncation,33	clippy::cast_sign_loss,34	// False positives35	// https://github.com/rust-lang/rust-clippy/issues/690236	clippy::use_self,37	// https://github.com/rust-lang/rust-clippy/issues/853938	clippy::iter_with_drain,39	clippy::type_repetition_in_bounds,40	// ci is being run with nightly, but library should work on stable41	clippy::missing_const_for_fn,42)]4344// For jrsonnet-macros45extern crate self as jrsonnet_evaluator;4647mod arr;48#[cfg(feature = "async-import")]49pub mod async_import;50mod ctx;51mod dynamic;52pub mod error;53mod evaluate;54pub mod function;55pub mod gc;56mod import;57mod integrations;58pub mod manifest;59mod map;60mod obj;61pub mod stack;62pub mod stdlib;63mod tla;64pub mod trace;65pub mod typed;66pub mod val;6768use std::{69	any::Any,70	cell::{Ref, RefCell, RefMut},71	fmt::{self, Debug},72	path::Path,73};7475pub use ctx::*;76pub use dynamic::*;77pub use error::{Error, ErrorKind::*, Result, ResultExt};78pub use evaluate::*;79use function::CallLocation;80use gc::{GcHashMap, TraceBox};81use hashbrown::hash_map::RawEntryMut;82pub use import::*;83use jrsonnet_gcmodule::{Cc, Trace};84pub use jrsonnet_interner::{IBytes, IStr};85pub use jrsonnet_parser as parser;86use jrsonnet_parser::*;87pub use obj::*;88use stack::check_depth;89pub use tla::apply_tla;90pub use val::{Thunk, Val};9192/// Thunk without bound `super`/`this`93/// object inheritance may be overriden multiple times, and will be fixed only on field read94pub trait Unbound: Trace {95	/// Type of value after object context is bound96	type Bound;97	/// Create value bound to specified object context98	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;99}100101/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code102/// Standard jsonnet fields are always unbound103#[derive(Clone, Trace)]104pub enum MaybeUnbound {105	/// Value needs to be bound to `this`/`super`106	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),107	/// Value is object-independent108	Bound(Thunk<Val>),109}110111impl Debug for MaybeUnbound {112	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {113		write!(f, "MaybeUnbound")114	}115}116impl MaybeUnbound {117	/// Attach object context to value, if required118	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {119		match self {120			Self::Unbound(v) => v.bind(sup, this),121			Self::Bound(v) => Ok(v.evaluate()?),122		}123	}124}125126/// During import, this trait will be called to create initial context for file.127/// It may initialize global variables, stdlib for example.128pub trait ContextInitializer: Trace {129	/// For which size the builder should be preallocated130	fn reserve_vars(&self) -> usize {131		0132	}133	/// Initialize default file context.134	/// Has default implementation, which calls `populate`.135	/// Prefer to always implement `populate` instead.136	fn initialize(&self, state: State, for_file: Source) -> Context {137		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());138		self.populate(for_file, &mut builder);139		builder.build()140	}141	/// For composability: extend builder. May panic if this initialization is not supported,142	/// and the context may only be created via `initialize`.143	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);144	/// Allows upcasting from abstract to concrete context initializer.145	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.146	fn as_any(&self) -> &dyn Any;147}148149/// Context initializer which adds nothing.150impl ContextInitializer for () {151	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}152	fn as_any(&self) -> &dyn Any {153		self154	}155}156157macro_rules! impl_context_initializer {158	($($gen:ident)*) => {159		#[allow(non_snake_case)]160		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {161			fn reserve_vars(&self) -> usize {162				let mut out = 0;163				let ($($gen,)*) = self;164				$(out += $gen.reserve_vars();)*165				out166			}167			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {168				let ($($gen,)*) = self;169				$($gen.populate(for_file.clone(), builder);)*170			}171			fn as_any(&self) -> &dyn Any {172				self173			}174		}175	};176	($($cur:ident)* @ $c:ident $($rest:ident)*) => {177		impl_context_initializer!($($cur)*);178		impl_context_initializer!($($cur)* $c @ $($rest)*);179	};180	($($cur:ident)* @) => {181		impl_context_initializer!($($cur)*);182	}183}184impl_context_initializer! {185	A @ B C D E F G186}187188/// Dynamically reconfigurable evaluation settings189#[derive(Trace)]190pub struct EvaluationSettings {191	/// Context initializer, which will be used for imports and everything192	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`193	pub context_initializer: TraceBox<dyn ContextInitializer>,194	/// Used to resolve file locations/contents195	pub import_resolver: TraceBox<dyn ImportResolver>,196}197impl Default for EvaluationSettings {198	fn default() -> Self {199		Self {200			context_initializer: tb!(()),201			import_resolver: tb!(DummyImportResolver),202		}203	}204}205206#[derive(Trace)]207struct FileData {208	string: Option<IStr>,209	bytes: Option<IBytes>,210	parsed: Option<LocExpr>,211	evaluated: Option<Val>,212213	evaluating: bool,214}215impl FileData {216	fn new_string(data: IStr) -> Self {217		Self {218			string: Some(data),219			bytes: None,220			parsed: None,221			evaluated: None,222			evaluating: false,223		}224	}225	fn new_bytes(data: IBytes) -> Self {226		Self {227			string: None,228			bytes: Some(data),229			parsed: None,230			evaluated: None,231			evaluating: false,232		}233	}234	pub(crate) fn get_string(&mut self) -> Option<IStr> {235		if self.string.is_none() {236			self.string = Some(237				self.bytes238					.as_ref()239					.expect("either string or bytes should be set")240					.clone()241					.cast_str()?,242			);243		}244		Some(self.string.clone().expect("just set"))245	}246}247248#[derive(Default, Trace)]249pub struct EvaluationStateInternals {250	/// Internal state251	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,252	/// Settings, safe to change at runtime253	settings: RefCell<EvaluationSettings>,254}255256/// Maintains stack trace and import resolution257#[derive(Default, Clone, Trace)]258pub struct State(Cc<EvaluationStateInternals>);259260impl State {261	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise262	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {263		let mut file_cache = self.file_cache();264		let mut file = file_cache.raw_entry_mut().from_key(&path);265266		let file = match file {267			RawEntryMut::Occupied(ref mut d) => d.get_mut(),268			RawEntryMut::Vacant(v) => {269				let data = self.settings().import_resolver.load_file_contents(&path)?;270				v.insert(271					path.clone(),272					FileData::new_string(273						std::str::from_utf8(&data)274							.map_err(|_| ImportBadFileUtf8(path.clone()))?275							.into(),276					),277				)278				.1279			}280		};281		Ok(file282			.get_string()283			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)284	}285	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise286	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {287		let mut file_cache = self.file_cache();288		let mut file = file_cache.raw_entry_mut().from_key(&path);289290		let file = match file {291			RawEntryMut::Occupied(ref mut d) => d.get_mut(),292			RawEntryMut::Vacant(v) => {293				let data = self.settings().import_resolver.load_file_contents(&path)?;294				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))295					.1296			}297		};298		if let Some(str) = &file.bytes {299			return Ok(str.clone());300		}301		if file.bytes.is_none() {302			file.bytes = Some(303				file.string304					.as_ref()305					.expect("either string or bytes should be set")306					.clone()307					.cast_bytes(),308			);309		}310		Ok(file.bytes.as_ref().expect("just set").clone())311	}312	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise313	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {314		let mut file_cache = self.file_cache();315		let mut file = file_cache.raw_entry_mut().from_key(&path);316317		let file = match file {318			RawEntryMut::Occupied(ref mut d) => d.get_mut(),319			RawEntryMut::Vacant(v) => {320				let data = self.settings().import_resolver.load_file_contents(&path)?;321				v.insert(322					path.clone(),323					FileData::new_string(324						std::str::from_utf8(&data)325							.map_err(|_| ImportBadFileUtf8(path.clone()))?326							.into(),327					),328				)329				.1330			}331		};332		if let Some(val) = &file.evaluated {333			return Ok(val.clone());334		}335		let code = file336			.get_string()337			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;338		let file_name = Source::new(path.clone(), code.clone());339		if file.parsed.is_none() {340			file.parsed = Some(341				jrsonnet_parser::parse(342					&code,343					&ParserSettings {344						source: file_name.clone(),345					},346				)347				.map_err(|e| ImportSyntaxError {348					path: file_name.clone(),349					error: Box::new(e),350				})?,351			);352		}353		let parsed = file.parsed.as_ref().expect("just set").clone();354		if file.evaluating {355			throw!(InfiniteRecursionDetected)356		}357		file.evaluating = true;358		// Dropping file cache guard here, as evaluation may use this map too359		drop(file_cache);360		let res = evaluate(self.create_default_context(file_name), &parsed);361362		let mut file_cache = self.file_cache();363		let mut file = file_cache.raw_entry_mut().from_key(&path);364365		let RawEntryMut::Occupied(file) = &mut file else {366			unreachable!("this file was just here!")367		};368		let file = file.get_mut();369		file.evaluating = false;370		match res {371			Ok(v) => {372				file.evaluated = Some(v.clone());373				Ok(v)374			}375			Err(e) => Err(e),376		}377	}378379	/// Has same semantics as `import 'path'` called from `from` file380	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {381		let resolved = self.resolve_from(from, path)?;382		self.import_resolved(resolved)383	}384	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {385		let resolved = self.resolve(path)?;386		self.import_resolved(resolved)387	}388389	/// Creates context with all passed global variables390	pub fn create_default_context(&self, source: Source) -> Context {391		let context_initializer = &self.settings().context_initializer;392		context_initializer.initialize(self.clone(), source)393	}394395	/// Creates context with all passed global variables, calling custom modifier396	pub fn create_default_context_with(397		&self,398		source: Source,399		context_initializer: impl ContextInitializer,400	) -> Context {401		let default_initializer = &self.settings().context_initializer;402		let mut builder = ContextBuilder::with_capacity(403			self.clone(),404			default_initializer.reserve_vars() + context_initializer.reserve_vars(),405		);406		default_initializer.populate(source.clone(), &mut builder);407		context_initializer.populate(source, &mut builder);408409		builder.build()410	}411412	/// Executes code creating a new stack frame413	pub fn push<T>(414		e: CallLocation<'_>,415		frame_desc: impl FnOnce() -> String,416		f: impl FnOnce() -> Result<T>,417	) -> Result<T> {418		let _guard = check_depth()?;419420		f().with_description_src(e, frame_desc)421	}422423	/// Executes code creating a new stack frame424	pub fn push_val(425		&self,426		e: &ExprLocation,427		frame_desc: impl FnOnce() -> String,428		f: impl FnOnce() -> Result<Val>,429	) -> Result<Val> {430		let _guard = check_depth()?;431432		f().with_description_src(e, frame_desc)433	}434	/// Executes code creating a new stack frame435	pub fn push_description<T>(436		frame_desc: impl FnOnce() -> String,437		f: impl FnOnce() -> Result<T>,438	) -> Result<T> {439		let _guard = check_depth()?;440441		f().with_description(frame_desc)442	}443}444445/// Internals446impl State {447	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {448		self.0.file_cache.borrow_mut()449	}450	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {451		self.0.settings.borrow()452	}453	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {454		self.0.settings.borrow_mut()455	}456	pub fn add_global(&self, name: IStr, value: Thunk<Val>) {457		#[derive(Trace)]458		struct GlobalsCtx {459			globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,460			inner: TraceBox<dyn ContextInitializer>,461		}462		impl ContextInitializer for GlobalsCtx {463			fn reserve_vars(&self) -> usize {464				self.inner.reserve_vars() + self.globals.borrow().len()465			}466			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {467				self.inner.populate(for_file, builder);468				for (name, val) in self.globals.borrow().iter() {469					builder.bind(name.clone(), val.clone());470				}471			}472473			fn as_any(&self) -> &dyn Any {474				self475			}476		}477		let mut settings = self.settings_mut();478		let initializer = &mut settings.context_initializer;479		if let Some(global) = initializer.as_any().downcast_ref::<GlobalsCtx>() {480			global.globals.borrow_mut().insert(name, value);481		} else {482			let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));483			settings.context_initializer = tb!(GlobalsCtx {484				globals: {485					let mut out = GcHashMap::with_capacity(1);486					out.insert(name, value);487					RefCell::new(out)488				},489				inner490			});491		}492	}493}494495#[derive(Trace)]496pub struct InitialUnderscore(pub Thunk<Val>);497impl ContextInitializer for InitialUnderscore {498	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {499		builder.bind("_".into(), self.0.clone());500	}501502	fn as_any(&self) -> &dyn Any {503		self504	}505}506507/// Raw methods evaluate passed values but don't perform TLA execution508impl State {509	/// Parses and evaluates the given snippet510	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {511		let code = code.into();512		let source = Source::new_virtual(name.into(), code.clone());513		let parsed = jrsonnet_parser::parse(514			&code,515			&ParserSettings {516				source: source.clone(),517			},518		)519		.map_err(|e| ImportSyntaxError {520			path: source.clone(),521			error: Box::new(e),522		})?;523		evaluate(self.create_default_context(source), &parsed)524	}525	/// Parses and evaluates the given snippet with custom context modifier526	pub fn evaluate_snippet_with(527		&self,528		name: impl Into<IStr>,529		code: impl Into<IStr>,530		context_initializer: impl ContextInitializer,531	) -> Result<Val> {532		let code = code.into();533		let source = Source::new_virtual(name.into(), code.clone());534		let parsed = jrsonnet_parser::parse(535			&code,536			&ParserSettings {537				source: source.clone(),538			},539		)540		.map_err(|e| ImportSyntaxError {541			path: source.clone(),542			error: Box::new(e),543		})?;544		evaluate(545			self.create_default_context_with(source, context_initializer),546			&parsed,547		)548	}549}550551/// Settings utilities552impl State {553	// Only panics in case of [`ImportResolver`] contract violation554	#[allow(clippy::missing_panics_doc)]555	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {556		self.import_resolver().resolve_from(from, path.as_ref())557	}558559	// Only panics in case of [`ImportResolver`] contract violation560	#[allow(clippy::missing_panics_doc)]561	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {562		self.import_resolver().resolve(path.as_ref())563	}564	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {565		Ref::map(self.settings(), |s| &*s.import_resolver)566	}567	pub fn set_import_resolver(&self, resolver: impl ImportResolver) {568		self.settings_mut().import_resolver = tb!(resolver);569	}570	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {571		Ref::map(self.settings(), |s| &*s.context_initializer)572	}573	pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {574		self.settings_mut().context_initializer = tb!(initializer);575	}576}
after · crates/jrsonnet-evaluator/src/lib.rs
1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5	clippy::all,6	clippy::nursery,7	clippy::pedantic,8	// missing_docs,9	elided_lifetimes_in_paths,10	explicit_outlives_requirements,11	noop_method_call,12	single_use_lifetimes,13	variant_size_differences,14	rustdoc::all15)]16#![allow(17	macro_expanded_macro_exports_accessed_by_absolute_paths,18	clippy::ptr_arg,19	// Too verbose20	clippy::must_use_candidate,21	// A lot of functions pass around errors thrown by code22	clippy::missing_errors_doc,23	// A lot of pointers have interior Rc24	clippy::needless_pass_by_value,25	// Its fine26	clippy::wildcard_imports,27	clippy::enum_glob_use,28	clippy::module_name_repetitions,29	// TODO: fix individual issues, however this works as intended almost everywhere30	clippy::cast_precision_loss,31	clippy::cast_possible_wrap,32	clippy::cast_possible_truncation,33	clippy::cast_sign_loss,34	// False positives35	// https://github.com/rust-lang/rust-clippy/issues/690236	clippy::use_self,37	// https://github.com/rust-lang/rust-clippy/issues/853938	clippy::iter_with_drain,39	clippy::type_repetition_in_bounds,40	// ci is being run with nightly, but library should work on stable41	clippy::missing_const_for_fn,42	// too many false-positives with .expect() calls43	clippy::missing_panics_doc,44)]4546// For jrsonnet-macros47extern crate self as jrsonnet_evaluator;4849mod arr;50#[cfg(feature = "async-import")]51pub mod async_import;52mod ctx;53mod dynamic;54pub mod error;55mod evaluate;56pub mod function;57pub mod gc;58mod import;59mod integrations;60pub mod manifest;61mod map;62mod obj;63pub mod stack;64pub mod stdlib;65mod tla;66pub mod trace;67pub mod typed;68pub mod val;6970use std::{71	any::Any,72	cell::{Ref, RefCell, RefMut},73	fmt::{self, Debug},74	path::Path,75};7677pub use ctx::*;78pub use dynamic::*;79pub use error::{Error, ErrorKind::*, Result, ResultExt};80pub use evaluate::*;81use function::CallLocation;82use gc::{GcHashMap, TraceBox};83use hashbrown::hash_map::RawEntryMut;84pub use import::*;85use jrsonnet_gcmodule::{Cc, Trace};86pub use jrsonnet_interner::{IBytes, IStr};87pub use jrsonnet_parser as parser;88use jrsonnet_parser::*;89pub use obj::*;90use stack::check_depth;91pub use tla::apply_tla;92pub use val::{Thunk, Val};9394/// Thunk without bound `super`/`this`95/// object inheritance may be overriden multiple times, and will be fixed only on field read96pub trait Unbound: Trace {97	/// Type of value after object context is bound98	type Bound;99	/// Create value bound to specified object context100	fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;101}102103/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code104/// Standard jsonnet fields are always unbound105#[derive(Clone, Trace)]106pub enum MaybeUnbound {107	/// Value needs to be bound to `this`/`super`108	Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),109	/// Value is object-independent110	Bound(Thunk<Val>),111}112113impl Debug for MaybeUnbound {114	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {115		write!(f, "MaybeUnbound")116	}117}118impl MaybeUnbound {119	/// Attach object context to value, if required120	pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {121		match self {122			Self::Unbound(v) => v.bind(sup, this),123			Self::Bound(v) => Ok(v.evaluate()?),124		}125	}126}127128/// During import, this trait will be called to create initial context for file.129/// It may initialize global variables, stdlib for example.130pub trait ContextInitializer: Trace {131	/// For which size the builder should be preallocated132	fn reserve_vars(&self) -> usize {133		0134	}135	/// Initialize default file context.136	/// Has default implementation, which calls `populate`.137	/// Prefer to always implement `populate` instead.138	fn initialize(&self, state: State, for_file: Source) -> Context {139		let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());140		self.populate(for_file, &mut builder);141		builder.build()142	}143	/// For composability: extend builder. May panic if this initialization is not supported,144	/// and the context may only be created via `initialize`.145	fn populate(&self, for_file: Source, builder: &mut ContextBuilder);146	/// Allows upcasting from abstract to concrete context initializer.147	/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.148	fn as_any(&self) -> &dyn Any;149}150151/// Context initializer which adds nothing.152impl ContextInitializer for () {153	fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}154	fn as_any(&self) -> &dyn Any {155		self156	}157}158159macro_rules! impl_context_initializer {160	($($gen:ident)*) => {161		#[allow(non_snake_case)]162		impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {163			fn reserve_vars(&self) -> usize {164				let mut out = 0;165				let ($($gen,)*) = self;166				$(out += $gen.reserve_vars();)*167				out168			}169			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {170				let ($($gen,)*) = self;171				$($gen.populate(for_file.clone(), builder);)*172			}173			fn as_any(&self) -> &dyn Any {174				self175			}176		}177	};178	($($cur:ident)* @ $c:ident $($rest:ident)*) => {179		impl_context_initializer!($($cur)*);180		impl_context_initializer!($($cur)* $c @ $($rest)*);181	};182	($($cur:ident)* @) => {183		impl_context_initializer!($($cur)*);184	}185}186impl_context_initializer! {187	A @ B C D E F G188}189190/// Dynamically reconfigurable evaluation settings191#[derive(Trace)]192pub struct EvaluationSettings {193	/// Context initializer, which will be used for imports and everything194	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`195	pub context_initializer: TraceBox<dyn ContextInitializer>,196	/// Used to resolve file locations/contents197	pub import_resolver: TraceBox<dyn ImportResolver>,198}199impl Default for EvaluationSettings {200	fn default() -> Self {201		Self {202			context_initializer: tb!(()),203			import_resolver: tb!(DummyImportResolver),204		}205	}206}207208#[derive(Trace)]209struct FileData {210	string: Option<IStr>,211	bytes: Option<IBytes>,212	parsed: Option<LocExpr>,213	evaluated: Option<Val>,214215	evaluating: bool,216}217impl FileData {218	fn new_string(data: IStr) -> Self {219		Self {220			string: Some(data),221			bytes: None,222			parsed: None,223			evaluated: None,224			evaluating: false,225		}226	}227	fn new_bytes(data: IBytes) -> Self {228		Self {229			string: None,230			bytes: Some(data),231			parsed: None,232			evaluated: None,233			evaluating: false,234		}235	}236	pub(crate) fn get_string(&mut self) -> Option<IStr> {237		if self.string.is_none() {238			self.string = Some(239				self.bytes240					.as_ref()241					.expect("either string or bytes should be set")242					.clone()243					.cast_str()?,244			);245		}246		Some(self.string.clone().expect("just set"))247	}248}249250#[derive(Default, Trace)]251pub struct EvaluationStateInternals {252	/// Internal state253	file_cache: RefCell<GcHashMap<SourcePath, FileData>>,254	/// Settings, safe to change at runtime255	settings: RefCell<EvaluationSettings>,256}257258/// Maintains stack trace and import resolution259#[derive(Default, Clone, Trace)]260pub struct State(Cc<EvaluationStateInternals>);261262impl State {263	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise264	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {265		let mut file_cache = self.file_cache();266		let mut file = file_cache.raw_entry_mut().from_key(&path);267268		let file = match file {269			RawEntryMut::Occupied(ref mut d) => d.get_mut(),270			RawEntryMut::Vacant(v) => {271				let data = self.settings().import_resolver.load_file_contents(&path)?;272				v.insert(273					path.clone(),274					FileData::new_string(275						std::str::from_utf8(&data)276							.map_err(|_| ImportBadFileUtf8(path.clone()))?277							.into(),278					),279				)280				.1281			}282		};283		Ok(file284			.get_string()285			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?)286	}287	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise288	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {289		let mut file_cache = self.file_cache();290		let mut file = file_cache.raw_entry_mut().from_key(&path);291292		let file = match file {293			RawEntryMut::Occupied(ref mut d) => d.get_mut(),294			RawEntryMut::Vacant(v) => {295				let data = self.settings().import_resolver.load_file_contents(&path)?;296				v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))297					.1298			}299		};300		if let Some(str) = &file.bytes {301			return Ok(str.clone());302		}303		if file.bytes.is_none() {304			file.bytes = Some(305				file.string306					.as_ref()307					.expect("either string or bytes should be set")308					.clone()309					.cast_bytes(),310			);311		}312		Ok(file.bytes.as_ref().expect("just set").clone())313	}314	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise315	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {316		let mut file_cache = self.file_cache();317		let mut file = file_cache.raw_entry_mut().from_key(&path);318319		let file = match file {320			RawEntryMut::Occupied(ref mut d) => d.get_mut(),321			RawEntryMut::Vacant(v) => {322				let data = self.settings().import_resolver.load_file_contents(&path)?;323				v.insert(324					path.clone(),325					FileData::new_string(326						std::str::from_utf8(&data)327							.map_err(|_| ImportBadFileUtf8(path.clone()))?328							.into(),329					),330				)331				.1332			}333		};334		if let Some(val) = &file.evaluated {335			return Ok(val.clone());336		}337		let code = file338			.get_string()339			.ok_or_else(|| ImportBadFileUtf8(path.clone()))?;340		let file_name = Source::new(path.clone(), code.clone());341		if file.parsed.is_none() {342			file.parsed = Some(343				jrsonnet_parser::parse(344					&code,345					&ParserSettings {346						source: file_name.clone(),347					},348				)349				.map_err(|e| ImportSyntaxError {350					path: file_name.clone(),351					error: Box::new(e),352				})?,353			);354		}355		let parsed = file.parsed.as_ref().expect("just set").clone();356		if file.evaluating {357			throw!(InfiniteRecursionDetected)358		}359		file.evaluating = true;360		// Dropping file cache guard here, as evaluation may use this map too361		drop(file_cache);362		let res = evaluate(self.create_default_context(file_name), &parsed);363364		let mut file_cache = self.file_cache();365		let mut file = file_cache.raw_entry_mut().from_key(&path);366367		let RawEntryMut::Occupied(file) = &mut file else {368			unreachable!("this file was just here!")369		};370		let file = file.get_mut();371		file.evaluating = false;372		match res {373			Ok(v) => {374				file.evaluated = Some(v.clone());375				Ok(v)376			}377			Err(e) => Err(e),378		}379	}380381	/// Has same semantics as `import 'path'` called from `from` file382	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {383		let resolved = self.resolve_from(from, path)?;384		self.import_resolved(resolved)385	}386	pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {387		let resolved = self.resolve(path)?;388		self.import_resolved(resolved)389	}390391	/// Creates context with all passed global variables392	pub fn create_default_context(&self, source: Source) -> Context {393		let context_initializer = &self.settings().context_initializer;394		context_initializer.initialize(self.clone(), source)395	}396397	/// Creates context with all passed global variables, calling custom modifier398	pub fn create_default_context_with(399		&self,400		source: Source,401		context_initializer: impl ContextInitializer,402	) -> Context {403		let default_initializer = &self.settings().context_initializer;404		let mut builder = ContextBuilder::with_capacity(405			self.clone(),406			default_initializer.reserve_vars() + context_initializer.reserve_vars(),407		);408		default_initializer.populate(source.clone(), &mut builder);409		context_initializer.populate(source, &mut builder);410411		builder.build()412	}413414	/// Executes code creating a new stack frame415	pub fn push<T>(416		e: CallLocation<'_>,417		frame_desc: impl FnOnce() -> String,418		f: impl FnOnce() -> Result<T>,419	) -> Result<T> {420		let _guard = check_depth()?;421422		f().with_description_src(e, frame_desc)423	}424425	/// Executes code creating a new stack frame426	pub fn push_val(427		&self,428		e: &ExprLocation,429		frame_desc: impl FnOnce() -> String,430		f: impl FnOnce() -> Result<Val>,431	) -> Result<Val> {432		let _guard = check_depth()?;433434		f().with_description_src(e, frame_desc)435	}436	/// Executes code creating a new stack frame437	pub fn push_description<T>(438		frame_desc: impl FnOnce() -> String,439		f: impl FnOnce() -> Result<T>,440	) -> Result<T> {441		let _guard = check_depth()?;442443		f().with_description(frame_desc)444	}445}446447/// Internals448impl State {449	fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {450		self.0.file_cache.borrow_mut()451	}452	pub fn settings(&self) -> Ref<'_, EvaluationSettings> {453		self.0.settings.borrow()454	}455	pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {456		self.0.settings.borrow_mut()457	}458	pub fn add_global(&self, name: IStr, value: Thunk<Val>) {459		#[derive(Trace)]460		struct GlobalsCtx {461			globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,462			inner: TraceBox<dyn ContextInitializer>,463		}464		impl ContextInitializer for GlobalsCtx {465			fn reserve_vars(&self) -> usize {466				self.inner.reserve_vars() + self.globals.borrow().len()467			}468			fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {469				self.inner.populate(for_file, builder);470				for (name, val) in self.globals.borrow().iter() {471					builder.bind(name.clone(), val.clone());472				}473			}474475			fn as_any(&self) -> &dyn Any {476				self477			}478		}479		let mut settings = self.settings_mut();480		let initializer = &mut settings.context_initializer;481		if let Some(global) = initializer.as_any().downcast_ref::<GlobalsCtx>() {482			global.globals.borrow_mut().insert(name, value);483		} else {484			let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));485			settings.context_initializer = tb!(GlobalsCtx {486				globals: {487					let mut out = GcHashMap::with_capacity(1);488					out.insert(name, value);489					RefCell::new(out)490				},491				inner492			});493		}494	}495}496497#[derive(Trace)]498pub struct InitialUnderscore(pub Thunk<Val>);499impl ContextInitializer for InitialUnderscore {500	fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {501		builder.bind("_".into(), self.0.clone());502	}503504	fn as_any(&self) -> &dyn Any {505		self506	}507}508509/// Raw methods evaluate passed values but don't perform TLA execution510impl State {511	/// Parses and evaluates the given snippet512	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {513		let code = code.into();514		let source = Source::new_virtual(name.into(), code.clone());515		let parsed = jrsonnet_parser::parse(516			&code,517			&ParserSettings {518				source: source.clone(),519			},520		)521		.map_err(|e| ImportSyntaxError {522			path: source.clone(),523			error: Box::new(e),524		})?;525		evaluate(self.create_default_context(source), &parsed)526	}527	/// Parses and evaluates the given snippet with custom context modifier528	pub fn evaluate_snippet_with(529		&self,530		name: impl Into<IStr>,531		code: impl Into<IStr>,532		context_initializer: impl ContextInitializer,533	) -> Result<Val> {534		let code = code.into();535		let source = Source::new_virtual(name.into(), code.clone());536		let parsed = jrsonnet_parser::parse(537			&code,538			&ParserSettings {539				source: source.clone(),540			},541		)542		.map_err(|e| ImportSyntaxError {543			path: source.clone(),544			error: Box::new(e),545		})?;546		evaluate(547			self.create_default_context_with(source, context_initializer),548			&parsed,549		)550	}551}552553/// Settings utilities554impl State {555	// Only panics in case of [`ImportResolver`] contract violation556	#[allow(clippy::missing_panics_doc)]557	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {558		self.import_resolver().resolve_from(from, path.as_ref())559	}560561	// Only panics in case of [`ImportResolver`] contract violation562	#[allow(clippy::missing_panics_doc)]563	pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {564		self.import_resolver().resolve(path.as_ref())565	}566	pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {567		Ref::map(self.settings(), |s| &*s.import_resolver)568	}569	pub fn set_import_resolver(&self, resolver: impl ImportResolver) {570		self.settings_mut().import_resolver = tb!(resolver);571	}572	pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {573		Ref::map(self.settings(), |s| &*s.context_initializer)574	}575	pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {576		self.settings_mut().context_initializer = tb!(initializer);577	}578}
modifiedcrates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -15,7 +15,7 @@
 
 impl LayeredHashMap {
 	pub fn iter_keys(self, mut handler: impl FnMut(IStr)) {
-		for (k, _) in self.0.current.iter() {
+		for (k, _) in &*self.0.current {
 			handler(k.clone());
 		}
 		if let Some(parent) = self.0.parent.clone() {
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -206,7 +206,7 @@
 			},
 			Self::ObjectRef(elems) => match value {
 				Val::Obj(obj) => {
-					for (k, v) in elems.iter() {
+					for (k, v) in *elems {
 						if let Some(got_v) = obj.get((*k).into())? {
 							push_type_description(
 								|| format!("property {k}"),
@@ -225,7 +225,7 @@
 			},
 			Self::Union(types) => {
 				let mut errors = Vec::new();
-				for ty in types.iter() {
+				for ty in types {
 					match ty.check(value) {
 						Ok(()) => {
 							return Ok(());
@@ -240,7 +240,7 @@
 			}
 			Self::UnionRef(types) => {
 				let mut errors = Vec::new();
-				for ty in types.iter() {
+				for ty in *types {
 					match ty.check(value) {
 						Ok(()) => {
 							return Ok(());
@@ -254,13 +254,13 @@
 				Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into())
 			}
 			Self::Sum(types) => {
-				for ty in types.iter() {
+				for ty in types {
 					ty.check(value)?;
 				}
 				Ok(())
 			}
 			Self::SumRef(types) => {
-				for ty in types.iter() {
+				for ty in *types {
 					ty.check(value)?;
 				}
 				Ok(())
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -346,15 +346,14 @@
 impl Eq for StrValue {}
 impl PartialOrd for StrValue {
 	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
-		let a = self.clone().into_flat();
-		let b = other.clone().into_flat();
-		Some(a.cmp(&b))
+		Some(self.cmp(other))
 	}
 }
 impl Ord for StrValue {
 	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
-		self.partial_cmp(other)
-			.expect("partial_cmp always returns Some")
+		let a = self.clone().into_flat();
+		let b = other.clone().into_flat();
+		a.cmp(&b)
 	}
 }
 
modifiedcrates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/inner.rs
+++ b/crates/jrsonnet-interner/src/inner.rs
@@ -217,7 +217,7 @@
 impl Eq for Inner {}
 impl PartialOrd for Inner {
 	fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
-		self.as_slice().partial_cmp(other.as_slice())
+		Some(self.cmp(other))
 	}
 }
 impl Ord for Inner {
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -591,7 +591,7 @@
 		.map(TypedField::parse)
 		.collect::<Result<Vec<_>>>()?;
 
-	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl( );
+	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
 
 	let typed = {
 		let fields = fields
modifiedcrates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -26,13 +26,13 @@
 struct NonNaNf64(f64);
 impl PartialOrd for NonNaNf64 {
 	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
-		self.0.partial_cmp(&other.0)
+		Some(self.cmp(other))
 	}
 }
 impl Eq for NonNaNf64 {}
 impl Ord for NonNaNf64 {
 	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
-		self.partial_cmp(other).expect("non nan")
+		self.0.partial_cmp(&other.0).expect("non nan")
 	}
 }
 
modifiedflake.nixdiffbeforeafterboth
--- a/flake.nix
+++ b/flake.nix
@@ -17,7 +17,7 @@
           overlays = [ rust-overlay.overlays.default ];
         };
         rust = ((pkgs.rustChannelOf { date = "2023-07-23"; channel = "nightly"; }).default.override {
-          extensions = [ "rust-src" "miri" "rust-analyzer" ];
+          extensions = [ "rust-src" "miri" "rust-analyzer" "clippy" ];
         });
       in
       rec {