git.delta.rocks / jrsonnet / refs/commits / 905c3fce02d8

difftreelog

refactor drop IntoUntyped for Thunk<T>

zvwvlqrpYaroslav Bolyukin2026-03-23parent: #186ae51.patch.diff
in: master
Unnecessarily reallocates... I wish we had specialization...

1 file changed

modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/typed/conversions.rs
1use std::{collections::BTreeMap, marker::PhantomData, ops::Deref};23use jrsonnet_gcmodule::Trace;4use jrsonnet_interner::{IBytes, IStr};5use jrsonnet_types::{ComplexValType, ValType};67use crate::{8	arr::{ArrValue, BytesArray},9	bail,10	function::FuncVal,11	typed::CheckType,12	val::{IndexableVal, NumValue, StrValue, ThunkMapper},13	ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,14};1516#[doc(hidden)]17pub mod __typed_macro_prelude {18	pub use ::jrsonnet_evaluator::{19		error::{ErrorKind, Result as JrResult},20		typed::{21			CheckType, ComplexValType, FromUntyped, IntoUntyped, ParseTypedObj, SerializeTypedObj,22			Typed,23		},24		IStr, ObjValue, ObjValueBuilder, State, Val,25	};26}27pub use jrsonnet_macros::{FromUntyped, IntoUntyped, Typed};2829#[derive(Trace)]30struct ThunkFromUntyped<K: Trace>(PhantomData<fn() -> K>);31impl<K> ThunkMapper<Val> for ThunkFromUntyped<K>32where33	K: Typed + FromUntyped + Trace,34{35	type Output = K;3637	fn map(self, from: Val) -> Result<Self::Output> {38		K::from_untyped(from)39	}40}41impl<K: Trace> Default for ThunkFromUntyped<K> {42	fn default() -> Self {43		Self(PhantomData)44	}45}46#[derive(Trace)]47struct ThunkIntoUntyped<K: Trace>(PhantomData<fn() -> K>);48impl<K> ThunkMapper<K> for ThunkIntoUntyped<K>49where50	K: Typed + Trace + IntoUntyped,51{52	type Output = Val;5354	fn map(self, from: K) -> Result<Self::Output> {55		K::into_untyped(from)56	}57}58impl<K: Trace> Default for ThunkIntoUntyped<K> {59	fn default() -> Self {60		Self(PhantomData)61	}62}6364#[diagnostic::on_unimplemented(65	note = "don't implement `ParseTypedObj` directly, it is automatically provided by `FromUntyped` derive"66)]67pub trait ParseTypedObj: Typed {68	fn parse(obj: &ObjValue) -> Result<Self>;69}7071#[diagnostic::on_unimplemented(72	note = "don't implement `SerializeTypedObj` directly, it is automatically provided by `IntoUntyped` derive"73)]74pub trait SerializeTypedObj: Typed {75	fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;76	fn into_object(self) -> Result<ObjValue> {77		let mut builder = ObjValueBuilder::new();78		self.serialize(&mut builder)?;79		Ok(builder.build())80	}81}8283pub trait Typed: Sized {84	const TYPE: &'static ComplexValType;85}86pub trait IntoUntyped: Typed {87	// Whatever caller should use `into_lazy_untyped` instead of `into_untyped`88	fn provides_lazy() -> bool {89		false90	}91	fn into_untyped(typed: Self) -> Result<Val>;92	fn into_lazy_untyped(typed: Self) -> Thunk<Val> {93		Thunk::from(Self::into_untyped(typed))94	}95}96pub trait IntoUntypedResult: Typed {97	/// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result98	/// This method returns identity in impl Typed for Result, and should not be overriden99	#[doc(hidden)]100	fn into_untyped_result(typed: Self) -> Result<Val>;101}102impl<T> IntoUntypedResult for T103where104	T: IntoUntyped,105{106	fn into_untyped_result(typed: Self) -> Result<Val> {107		T::into_untyped(typed)108	}109}110111pub trait FromUntyped: Typed {112	fn from_untyped(untyped: Val) -> Result<Self>;113	fn from_lazy_untyped(lazy: Thunk<Val>) -> Result<Self> {114		Self::from_untyped(lazy.evaluate()?)115	}116117	// Whatever caller should use `from_lazy_untyped` instead of `from_untyped` when possible118	fn wants_lazy() -> bool {119		false120	}121}122123impl<T> Typed for Thunk<T>124where125	T: Typed + Trace + Clone,126{127	const TYPE: &'static ComplexValType = &ComplexValType::Lazy(T::TYPE);128}129130impl<T> IntoUntyped for Thunk<T>131where132	T: Typed + IntoUntyped + Trace + Clone,133{134	fn into_untyped(typed: Self) -> Result<Val> {135		T::into_untyped(typed.evaluate()?)136	}137	fn provides_lazy() -> bool {138		true139	}140141	fn into_lazy_untyped(inner: Self) -> Thunk<Val> {142		inner.map(<ThunkIntoUntyped<T>>::default())143	}144}145146impl<T> FromUntyped for Thunk<T>147where148	T: Typed + FromUntyped + Trace + Clone,149{150	fn from_untyped(untyped: Val) -> Result<Self> {151		Self::from_lazy_untyped(Thunk::evaluated(untyped))152	}153154	fn wants_lazy() -> bool {155		true156	}157158	fn from_lazy_untyped(inner: Thunk<Val>) -> Result<Self> {159		Ok(inner.map(<ThunkFromUntyped<T>>::default()))160	}161}162163pub const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS)) - 1) as f64;164pub const MIN_SAFE_INTEGER: f64 = (-((1i64 << (f64::MANTISSA_DIGITS)) - 1)) as f64;165166macro_rules! impl_int {167	($($ty:ty)*) => {$(168		impl Typed for $ty {169			const TYPE: &'static ComplexValType =170				&ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));171		}172		impl FromUntyped for $ty {173			fn from_untyped(value: Val) -> Result<Self> {174				<Self as Typed>::TYPE.check(&value)?;175				match value {176					Val::Num(n) => {177						let n = n.get();178						#[allow(clippy::float_cmp)]179						if n.trunc() != n {180							bail!(181								"cannot convert number with fractional part to {}",182								stringify!($ty)183							)184						}185						Ok(n as Self)186					}187					_ => unreachable!(),188				}189			}190		}191		impl IntoUntyped for $ty {192			fn into_untyped(value: Self) -> Result<Val> {193				Ok(Val::Num(value.into()))194			}195		}196	)*};197}198199impl_int!(i8 u8 i16 u16 i32 u32);200201macro_rules! impl_bounded_int {202	($($name:ident = $ty:ty)*) => {$(203		#[derive(Clone, Copy)]204		pub struct $name<const MIN: $ty, const MAX: $ty>($ty);205		impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {206			pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {207				if value >= MIN && value <= MAX {208					Some(Self(value))209				} else {210					None211				}212			}213			pub const fn value(self) -> $ty {214				self.0215			}216		}217		impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {218			type Target = $ty;219			fn deref(&self) -> &Self::Target {220				&self.0221			}222		}223224		impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {225			const TYPE: &'static ComplexValType =226				&ComplexValType::BoundedNumber(227					Some(MIN as f64),228					Some(MAX as f64),229				);230		}231232		impl<const MIN: $ty, const MAX: $ty> FromUntyped for $name<MIN, MAX> {233			fn from_untyped(value: Val) -> Result<Self> {234				<Self as Typed>::TYPE.check(&value)?;235				match value {236					Val::Num(n) => {237						let n = n.get();238						#[allow(clippy::float_cmp)]239						if n.trunc() != n {240							bail!(241								"cannot convert number with fractional part to {}",242								stringify!($ty)243							)244						}245						Ok(Self(n as $ty))246					}247					_ => unreachable!(),248				}249			}250		}251252		impl<const MIN: $ty, const MAX: $ty> IntoUntyped for $name<MIN, MAX> {253			#[allow(clippy::cast_lossless)]254			fn into_untyped(value: Self) -> Result<Val> {255				Ok(Val::try_num(value.0)?)256			}257		}258	)*};259}260261impl_bounded_int!(262	BoundedI8 = i8263	BoundedI16 = i16264	BoundedI32 = i32265	BoundedI64 = i64266	BoundedUsize = usize267);268269impl Typed for f64 {270	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);271}272impl IntoUntyped for f64 {273	fn into_untyped(value: Self) -> Result<Val> {274		Ok(Val::try_num(value)?)275	}276}277impl FromUntyped for f64 {278	fn from_untyped(value: Val) -> Result<Self> {279		<Self as Typed>::TYPE.check(&value)?;280		match value {281			Val::Num(n) => Ok(n.get()),282			_ => unreachable!(),283		}284	}285}286287pub struct PositiveF64(pub f64);288impl Typed for PositiveF64 {289	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);290}291impl IntoUntyped for PositiveF64 {292	fn into_untyped(value: Self) -> Result<Val> {293		Ok(Val::try_num(value.0)?)294	}295}296impl FromUntyped for PositiveF64 {297	fn from_untyped(value: Val) -> Result<Self> {298		<Self as Typed>::TYPE.check(&value)?;299		match value {300			Val::Num(n) => Ok(Self(n.get())),301			_ => unreachable!(),302		}303	}304}305impl Typed for usize {306	const TYPE: &'static ComplexValType =307		&ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));308}309impl IntoUntyped for usize {310	fn into_untyped(value: Self) -> Result<Val> {311		Ok(Val::try_num(value)?)312	}313}314impl FromUntyped for usize {315	fn from_untyped(value: Val) -> Result<Self> {316		<Self as Typed>::TYPE.check(&value)?;317		match value {318			Val::Num(n) => {319				let n = n.get();320				#[allow(clippy::float_cmp)]321				if n.trunc() != n {322					bail!("cannot convert number with fractional part to usize")323				}324				Ok(n as Self)325			}326			_ => unreachable!(),327		}328	}329}330331impl Typed for IStr {332	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);333}334impl IntoUntyped for IStr {335	fn into_untyped(value: Self) -> Result<Val> {336		Ok(Val::string(value))337	}338}339impl FromUntyped for IStr {340	fn from_untyped(value: Val) -> Result<Self> {341		<Self as Typed>::TYPE.check(&value)?;342		match value {343			Val::Str(s) => Ok(s.into_flat()),344			_ => unreachable!(),345		}346	}347}348349impl Typed for String {350	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);351}352impl IntoUntyped for String {353	fn into_untyped(value: Self) -> Result<Val> {354		Ok(Val::string(value))355	}356}357impl FromUntyped for String {358	fn from_untyped(value: Val) -> Result<Self> {359		<Self as Typed>::TYPE.check(&value)?;360		match value {361			Val::Str(s) => Ok(s.to_string()),362			_ => unreachable!(),363		}364	}365}366367impl Typed for StrValue {368	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);369}370impl IntoUntyped for StrValue {371	fn into_untyped(value: Self) -> Result<Val> {372		Ok(Val::Str(value))373	}374}375impl FromUntyped for StrValue {376	fn from_untyped(value: Val) -> Result<Self> {377		<Self as Typed>::TYPE.check(&value)?;378		match value {379			Val::Str(s) => Ok(s),380			_ => unreachable!(),381		}382	}383}384385impl Typed for char {386	const TYPE: &'static ComplexValType = &ComplexValType::Char;387}388impl IntoUntyped for char {389	fn into_untyped(value: Self) -> Result<Val> {390		Ok(Val::string(value))391	}392}393impl FromUntyped for char {394	fn from_untyped(value: Val) -> Result<Self> {395		<Self as Typed>::TYPE.check(&value)?;396		match value {397			Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),398			_ => unreachable!(),399		}400	}401}402403// TODO: View into vec using ArrayLike?404impl<T> Typed for Vec<T>405where406	T: Typed,407{408	const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);409}410impl<T: Typed + IntoUntyped> IntoUntyped for Vec<T> {411	fn into_untyped(value: Self) -> Result<Val> {412		Ok(Val::Arr(413			value414				.into_iter()415				.map(T::into_untyped)416				.collect::<Result<ArrValue>>()?,417		))418	}419}420impl<T: Typed + FromUntyped> FromUntyped for Vec<T> {421	fn from_untyped(value: Val) -> Result<Self> {422		let Val::Arr(a) = value else {423			<Self as Typed>::TYPE.check(&value)?;424			unreachable!("typecheck should fail")425		};426		a.iter()427			.enumerate()428			.map(|(i, r)| {429				r.and_then(|t| {430					T::from_untyped(t).with_description(|| format!("parsing elem <{i}>"))431				})432			})433			.collect::<Result<Self>>()434	}435}436437// TODO: View into BTreeMap using ObjectCore?438impl<K, V> Typed for BTreeMap<K, V>439where440	K: Typed + Ord,441	V: Typed,442{443	const TYPE: &'static ComplexValType = &ComplexValType::AttrsOf(V::TYPE);444}445impl<K, V> IntoUntyped for BTreeMap<K, V>446where447	K: Typed + Ord + IntoUntyped,448	V: Typed + IntoUntyped,449{450	fn into_untyped(typed: Self) -> Result<Val> {451		let mut out = ObjValueBuilder::with_capacity(typed.len());452		for (k, v) in typed {453			let Some(key) = K::into_untyped(k)?.as_str() else {454				bail!("map key should serialize to string");455			};456			let value = V::into_untyped(v)?;457			out.field(key).value(value);458		}459		Ok(Val::Obj(out.build()))460	}461}462impl<K, V> FromUntyped for BTreeMap<K, V>463where464	K: FromUntyped + Ord,465	V: FromUntyped,466{467	fn from_untyped(value: Val) -> Result<Self> {468		Self::TYPE.check(&value)?;469		let obj = value.as_obj().expect("typecheck should fail");470471		let mut out = Self::new();472		if V::wants_lazy() {473			for key in obj.fields_ex(474				false,475				#[cfg(feature = "exp-preserve-order")]476				false,477			) {478				let value = obj.get_lazy(key.clone()).expect("field exists");479				let value = V::from_lazy_untyped(value)?;480				let key = K::from_untyped(Val::Str(key.into()))?;481				let _ = out.insert(key, value);482			}483		} else {484			for (key, value) in obj.iter(485				#[cfg(feature = "exp-preserve-order")]486				false,487			) {488				let key = K::from_untyped(Val::Str(key.into()))?;489				let value = V::from_untyped(value?)?;490				let _ = out.insert(key, value);491			}492		}493		Ok(out)494	}495}496497impl Typed for Val {498	const TYPE: &'static ComplexValType = &ComplexValType::Any;499}500impl IntoUntyped for Val {501	fn into_untyped(typed: Self) -> Result<Val> {502		Ok(typed)503	}504}505impl FromUntyped for Val {506	fn from_untyped(untyped: Val) -> Result<Self> {507		Ok(untyped)508	}509}510511#[doc(hidden)]512impl<T> Typed for Result<T>513where514	T: Typed,515{516	const TYPE: &'static ComplexValType = &ComplexValType::Any;517}518impl<T: IntoUntyped> IntoUntypedResult for Result<T> {519	fn into_untyped_result(typed: Self) -> Result<Val> {520		typed.map(T::into_untyped)?521	}522}523524/// Specialization525impl Typed for IBytes {526	const TYPE: &'static ComplexValType =527		&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));528}529impl IntoUntyped for IBytes {530	fn into_untyped(value: Self) -> Result<Val> {531		Ok(Val::Arr(ArrValue::bytes(value)))532	}533}534impl FromUntyped for IBytes {535	fn from_untyped(value: Val) -> Result<Self> {536		let Val::Arr(a) = &value else {537			<Self as Typed>::TYPE.check(&value)?;538			unreachable!()539		};540		if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {541			return Ok(bytes.0.as_slice().into());542		}543		<Self as Typed>::TYPE.check(&value)?;544		// Any::downcast_ref::<ByteArray>(&a);545		let mut out = Vec::with_capacity(a.len());546		for e in a.iter() {547			let r = e?;548			out.push(u8::from_untyped(r)?);549		}550		Ok(out.as_slice().into())551	}552}553554pub struct M1;555impl Typed for M1 {556	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));557}558impl IntoUntyped for M1 {559	fn into_untyped(_: Self) -> Result<Val> {560		Ok(Val::Num(NumValue::new(-1.0).expect("finite")))561	}562}563impl FromUntyped for M1 {564	fn from_untyped(value: Val) -> Result<Self> {565		<Self as Typed>::TYPE.check(&value)?;566		Ok(Self)567	}568}569570macro_rules! decl_either {571	($($name: ident, $($id: ident)*);*) => {$(572		#[derive(Clone)]573		pub enum $name<$($id),*> {574			$($id($id)),*575		}576		impl<$($id),*> Typed for $name<$($id),*>577		where578			$($id: Typed,)*579		{580			const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);581		}582		impl<$($id),*> IntoUntyped for $name<$($id),*>583		where584			$($id: Typed + IntoUntyped,)*585		{586			fn into_untyped(value: Self) -> Result<Val> {587				match value {$(588					$name::$id(v) => $id::into_untyped(v)589				),*}590			}591		}592593		impl<$($id),*> FromUntyped for $name<$($id),*>594		where595			$($id: Typed + FromUntyped,)*596		{597			fn from_untyped(value: Val) -> Result<Self> {598				$(599					if $id::TYPE.check(&value).is_ok() {600						$id::from_untyped(value).map(Self::$id)601					} else602				)* {603					<Self as Typed>::TYPE.check(&value)?;604					unreachable!()605				}606			}607		}608	)*}609}610decl_either!(611	Either1, A;612	Either2, A B;613	Either3, A B C;614	Either4, A B C D;615	Either5, A B C D E;616	Either6, A B C D E F;617	Either7, A B C D E F G618);619#[macro_export]620macro_rules! Either {621	($a:ty) => {$crate::typed::Either1<$a>};622	($a:ty, $b:ty) => {$crate::typed::Either2<$a, $b>};623	($a:ty, $b:ty, $c:ty) => {$crate::typed::Either3<$a, $b, $c>};624	($a:ty, $b:ty, $c:ty, $d:ty) => {$crate::typed::Either4<$a, $b, $c, $d>};625	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {$crate::typed::Either5<$a, $b, $c, $d, $e>};626	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {$crate::typed::Either6<$a, $b, $c, $d, $e, $f>};627	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {$crate::typed::Either7<$a, $b, $c, $d, $e, $f, $g>};628}629pub use Either;630631pub type MyType = Either![u32, f64, String];632633impl Typed for ArrValue {634	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);635}636impl IntoUntyped for ArrValue {637	fn into_untyped(value: Self) -> Result<Val> {638		Ok(Val::Arr(value))639	}640}641impl FromUntyped for ArrValue {642	fn from_untyped(value: Val) -> Result<Self> {643		<Self as Typed>::TYPE.check(&value)?;644		match value {645			Val::Arr(a) => Ok(a),646			_ => unreachable!(),647		}648	}649}650651impl Typed for FuncVal {652	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);653}654impl IntoUntyped for FuncVal {655	fn into_untyped(value: Self) -> Result<Val> {656		Ok(Val::Func(value))657	}658}659impl FromUntyped for FuncVal {660	fn from_untyped(value: Val) -> Result<Self> {661		<Self as Typed>::TYPE.check(&value)?;662		match value {663			Val::Func(a) => Ok(a),664			_ => unreachable!(),665		}666	}667}668669impl Typed for ObjValue {670	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);671}672impl IntoUntyped for ObjValue {673	fn into_untyped(value: Self) -> Result<Val> {674		Ok(Val::Obj(value))675	}676}677impl FromUntyped for ObjValue {678	fn from_untyped(value: Val) -> Result<Self> {679		<Self as Typed>::TYPE.check(&value)?;680		match value {681			Val::Obj(a) => Ok(a),682			_ => unreachable!(),683		}684	}685}686687impl Typed for bool {688	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);689}690impl IntoUntyped for bool {691	fn into_untyped(value: Self) -> Result<Val> {692		Ok(Val::Bool(value))693	}694}695impl FromUntyped for bool {696	fn from_untyped(value: Val) -> Result<Self> {697		<Self as Typed>::TYPE.check(&value)?;698		match value {699			Val::Bool(a) => Ok(a),700			_ => unreachable!(),701		}702	}703}704705impl Typed for IndexableVal {706	const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[707		&ComplexValType::Simple(ValType::Arr),708		&ComplexValType::Simple(ValType::Str),709	]);710}711impl IntoUntyped for IndexableVal {712	fn into_untyped(value: Self) -> Result<Val> {713		match value {714			Self::Str(s) => Ok(Val::string(s)),715			Self::Arr(a) => Ok(Val::Arr(a)),716		}717	}718}719impl FromUntyped for IndexableVal {720	fn from_untyped(value: Val) -> Result<Self> {721		<Self as Typed>::TYPE.check(&value)?;722		value.into_indexable()723	}724}725726pub struct Null;727impl Typed for Null {728	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);729}730impl IntoUntyped for Null {731	fn into_untyped(_: Self) -> Result<Val> {732		Ok(Val::Null)733	}734}735impl FromUntyped for Null {736	fn from_untyped(value: Val) -> Result<Self> {737		<Self as Typed>::TYPE.check(&value)?;738		Ok(Self)739	}740}741742impl<T> Typed for Option<T>743where744	T: Typed,745{746	const TYPE: &'static ComplexValType =747		&ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);748}749impl<T> IntoUntyped for Option<T>750where751	T: Typed + IntoUntyped,752{753	fn into_untyped(typed: Self) -> Result<Val> {754		typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))755	}756}757impl<T> FromUntyped for Option<T>758where759	T: Typed + FromUntyped,760{761	fn from_untyped(untyped: Val) -> Result<Self> {762		if matches!(untyped, Val::Null) {763			Ok(None)764		} else {765			T::from_untyped(untyped).map(Some)766		}767	}768}769770impl Typed for NumValue {771	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);772}773impl IntoUntyped for NumValue {774	fn into_untyped(typed: Self) -> Result<Val> {775		Ok(Val::Num(typed))776	}777}778impl FromUntyped for NumValue {779	fn from_untyped(untyped: Val) -> Result<Self> {780		Self::TYPE.check(&untyped)?;781		match untyped {782			Val::Num(v) => Ok(v),783			_ => unreachable!(),784		}785	}786}
after · crates/jrsonnet-evaluator/src/typed/conversions.rs
1use std::{collections::BTreeMap, marker::PhantomData, ops::Deref};23use jrsonnet_gcmodule::Trace;4use jrsonnet_interner::{IBytes, IStr};5use jrsonnet_types::{ComplexValType, ValType};67use crate::{8	arr::{ArrValue, BytesArray},9	bail,10	function::FuncVal,11	typed::CheckType,12	val::{IndexableVal, NumValue, StrValue, ThunkMapper},13	ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,14};1516#[doc(hidden)]17pub mod __typed_macro_prelude {18	pub use ::jrsonnet_evaluator::{19		error::{ErrorKind, Result as JrResult},20		typed::{21			CheckType, ComplexValType, FromUntyped, IntoUntyped, ParseTypedObj, SerializeTypedObj,22			Typed,23		},24		IStr, ObjValue, ObjValueBuilder, State, Val,25	};26}27pub use jrsonnet_macros::{FromUntyped, IntoUntyped, Typed};2829#[derive(Trace)]30struct ThunkFromUntyped<K: Trace>(PhantomData<fn() -> K>);31impl<K> ThunkMapper<Val> for ThunkFromUntyped<K>32where33	K: Typed + FromUntyped + Trace,34{35	type Output = K;3637	fn map(self, from: Val) -> Result<Self::Output> {38		K::from_untyped(from)39	}40}41impl<K: Trace> Default for ThunkFromUntyped<K> {42	fn default() -> Self {43		Self(PhantomData)44	}45}46#[derive(Trace)]47struct ThunkIntoUntyped<K: Trace>(PhantomData<fn() -> K>);48impl<K> ThunkMapper<K> for ThunkIntoUntyped<K>49where50	K: Typed + Trace + IntoUntyped,51{52	type Output = Val;5354	fn map(self, from: K) -> Result<Self::Output> {55		K::into_untyped(from)56	}57}58impl<K: Trace> Default for ThunkIntoUntyped<K> {59	fn default() -> Self {60		Self(PhantomData)61	}62}6364#[diagnostic::on_unimplemented(65	note = "don't implement `ParseTypedObj` directly, it is automatically provided by `FromUntyped` derive"66)]67pub trait ParseTypedObj: Typed {68	fn parse(obj: &ObjValue) -> Result<Self>;69}7071#[diagnostic::on_unimplemented(72	note = "don't implement `SerializeTypedObj` directly, it is automatically provided by `IntoUntyped` derive"73)]74pub trait SerializeTypedObj: Typed {75	fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;76	fn into_object(self) -> Result<ObjValue> {77		let mut builder = ObjValueBuilder::new();78		self.serialize(&mut builder)?;79		Ok(builder.build())80	}81}8283pub trait Typed: Sized {84	const TYPE: &'static ComplexValType;85}86pub trait IntoUntyped: Typed {87	// Whatever caller should use `into_lazy_untyped` instead of `into_untyped`88	fn provides_lazy() -> bool {89		false90	}91	fn into_untyped(typed: Self) -> Result<Val>;92	fn into_lazy_untyped(typed: Self) -> Thunk<Val> {93		Thunk::from(Self::into_untyped(typed))94	}95}96pub trait IntoUntypedResult: Typed {97	/// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result98	/// This method returns identity in impl Typed for Result, and should not be overriden99	#[doc(hidden)]100	fn into_untyped_result(typed: Self) -> Result<Val>;101}102impl<T> IntoUntypedResult for T103where104	T: IntoUntyped,105{106	fn into_untyped_result(typed: Self) -> Result<Val> {107		T::into_untyped(typed)108	}109}110111pub trait FromUntyped: Typed {112	fn from_untyped(untyped: Val) -> Result<Self>;113	fn from_lazy_untyped(lazy: Thunk<Val>) -> Result<Self> {114		Self::from_untyped(lazy.evaluate()?)115	}116117	// Whatever caller should use `from_lazy_untyped` instead of `from_untyped` when possible118	fn wants_lazy() -> bool {119		false120	}121}122123impl<T> Typed for Thunk<T>124where125	T: Typed + Trace + Clone,126{127	const TYPE: &'static ComplexValType = &ComplexValType::Lazy(T::TYPE);128}129130impl IntoUntyped for Thunk<Val> {131	fn into_untyped(typed: Self) -> Result<Val> {132		typed.evaluate()133	}134	fn provides_lazy() -> bool {135		true136	}137138	fn into_lazy_untyped(inner: Self) -> Thunk<Val> {139		inner140	}141}142143impl<T> FromUntyped for Thunk<T>144where145	T: Typed + FromUntyped + Trace + Clone,146{147	fn from_untyped(untyped: Val) -> Result<Self> {148		Self::from_lazy_untyped(Thunk::evaluated(untyped))149	}150151	fn wants_lazy() -> bool {152		true153	}154155	fn from_lazy_untyped(inner: Thunk<Val>) -> Result<Self> {156		Ok(inner.map(<ThunkFromUntyped<T>>::default()))157	}158}159160pub const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS)) - 1) as f64;161pub const MIN_SAFE_INTEGER: f64 = (-((1i64 << (f64::MANTISSA_DIGITS)) - 1)) as f64;162163macro_rules! impl_int {164	($($ty:ty)*) => {$(165		impl Typed for $ty {166			const TYPE: &'static ComplexValType =167				&ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));168		}169		impl FromUntyped for $ty {170			fn from_untyped(value: Val) -> Result<Self> {171				<Self as Typed>::TYPE.check(&value)?;172				match value {173					Val::Num(n) => {174						let n = n.get();175						#[allow(clippy::float_cmp)]176						if n.trunc() != n {177							bail!(178								"cannot convert number with fractional part to {}",179								stringify!($ty)180							)181						}182						Ok(n as Self)183					}184					_ => unreachable!(),185				}186			}187		}188		impl IntoUntyped for $ty {189			fn into_untyped(value: Self) -> Result<Val> {190				Ok(Val::Num(value.into()))191			}192		}193	)*};194}195196impl_int!(i8 u8 i16 u16 i32 u32);197198macro_rules! impl_bounded_int {199	($($name:ident = $ty:ty)*) => {$(200		#[derive(Clone, Copy)]201		pub struct $name<const MIN: $ty, const MAX: $ty>($ty);202		impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {203			pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {204				if value >= MIN && value <= MAX {205					Some(Self(value))206				} else {207					None208				}209			}210			pub const fn value(self) -> $ty {211				self.0212			}213		}214		impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {215			type Target = $ty;216			fn deref(&self) -> &Self::Target {217				&self.0218			}219		}220221		impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {222			const TYPE: &'static ComplexValType =223				&ComplexValType::BoundedNumber(224					Some(MIN as f64),225					Some(MAX as f64),226				);227		}228229		impl<const MIN: $ty, const MAX: $ty> FromUntyped for $name<MIN, MAX> {230			fn from_untyped(value: Val) -> Result<Self> {231				<Self as Typed>::TYPE.check(&value)?;232				match value {233					Val::Num(n) => {234						let n = n.get();235						#[allow(clippy::float_cmp)]236						if n.trunc() != n {237							bail!(238								"cannot convert number with fractional part to {}",239								stringify!($ty)240							)241						}242						Ok(Self(n as $ty))243					}244					_ => unreachable!(),245				}246			}247		}248249		impl<const MIN: $ty, const MAX: $ty> IntoUntyped for $name<MIN, MAX> {250			#[allow(clippy::cast_lossless)]251			fn into_untyped(value: Self) -> Result<Val> {252				Ok(Val::try_num(value.0)?)253			}254		}255	)*};256}257258impl_bounded_int!(259	BoundedI8 = i8260	BoundedI16 = i16261	BoundedI32 = i32262	BoundedI64 = i64263	BoundedUsize = usize264);265266impl Typed for f64 {267	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);268}269impl IntoUntyped for f64 {270	fn into_untyped(value: Self) -> Result<Val> {271		Ok(Val::try_num(value)?)272	}273}274impl FromUntyped for f64 {275	fn from_untyped(value: Val) -> Result<Self> {276		<Self as Typed>::TYPE.check(&value)?;277		match value {278			Val::Num(n) => Ok(n.get()),279			_ => unreachable!(),280		}281	}282}283284pub struct PositiveF64(pub f64);285impl Typed for PositiveF64 {286	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);287}288impl IntoUntyped for PositiveF64 {289	fn into_untyped(value: Self) -> Result<Val> {290		Ok(Val::try_num(value.0)?)291	}292}293impl FromUntyped for PositiveF64 {294	fn from_untyped(value: Val) -> Result<Self> {295		<Self as Typed>::TYPE.check(&value)?;296		match value {297			Val::Num(n) => Ok(Self(n.get())),298			_ => unreachable!(),299		}300	}301}302impl Typed for usize {303	const TYPE: &'static ComplexValType =304		&ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));305}306impl IntoUntyped for usize {307	fn into_untyped(value: Self) -> Result<Val> {308		Ok(Val::try_num(value)?)309	}310}311impl FromUntyped for usize {312	fn from_untyped(value: Val) -> Result<Self> {313		<Self as Typed>::TYPE.check(&value)?;314		match value {315			Val::Num(n) => {316				let n = n.get();317				#[allow(clippy::float_cmp)]318				if n.trunc() != n {319					bail!("cannot convert number with fractional part to usize")320				}321				Ok(n as Self)322			}323			_ => unreachable!(),324		}325	}326}327328impl Typed for IStr {329	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);330}331impl IntoUntyped for IStr {332	fn into_untyped(value: Self) -> Result<Val> {333		Ok(Val::string(value))334	}335}336impl FromUntyped for IStr {337	fn from_untyped(value: Val) -> Result<Self> {338		<Self as Typed>::TYPE.check(&value)?;339		match value {340			Val::Str(s) => Ok(s.into_flat()),341			_ => unreachable!(),342		}343	}344}345346impl Typed for String {347	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);348}349impl IntoUntyped for String {350	fn into_untyped(value: Self) -> Result<Val> {351		Ok(Val::string(value))352	}353}354impl FromUntyped for String {355	fn from_untyped(value: Val) -> Result<Self> {356		<Self as Typed>::TYPE.check(&value)?;357		match value {358			Val::Str(s) => Ok(s.to_string()),359			_ => unreachable!(),360		}361	}362}363364impl Typed for StrValue {365	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);366}367impl IntoUntyped for StrValue {368	fn into_untyped(value: Self) -> Result<Val> {369		Ok(Val::Str(value))370	}371}372impl FromUntyped for StrValue {373	fn from_untyped(value: Val) -> Result<Self> {374		<Self as Typed>::TYPE.check(&value)?;375		match value {376			Val::Str(s) => Ok(s),377			_ => unreachable!(),378		}379	}380}381382impl Typed for char {383	const TYPE: &'static ComplexValType = &ComplexValType::Char;384}385impl IntoUntyped for char {386	fn into_untyped(value: Self) -> Result<Val> {387		Ok(Val::string(value))388	}389}390impl FromUntyped for char {391	fn from_untyped(value: Val) -> Result<Self> {392		<Self as Typed>::TYPE.check(&value)?;393		match value {394			Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),395			_ => unreachable!(),396		}397	}398}399400// TODO: View into vec using ArrayLike?401impl<T> Typed for Vec<T>402where403	T: Typed,404{405	const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);406}407impl<T: Typed + IntoUntyped> IntoUntyped for Vec<T> {408	fn into_untyped(value: Self) -> Result<Val> {409		Ok(Val::Arr(410			value411				.into_iter()412				.map(T::into_untyped)413				.collect::<Result<ArrValue>>()?,414		))415	}416}417impl<T: Typed + FromUntyped> FromUntyped for Vec<T> {418	fn from_untyped(value: Val) -> Result<Self> {419		let Val::Arr(a) = value else {420			<Self as Typed>::TYPE.check(&value)?;421			unreachable!("typecheck should fail")422		};423		a.iter()424			.enumerate()425			.map(|(i, r)| {426				r.and_then(|t| {427					T::from_untyped(t).with_description(|| format!("parsing elem <{i}>"))428				})429			})430			.collect::<Result<Self>>()431	}432}433434// TODO: View into BTreeMap using ObjectCore?435impl<K, V> Typed for BTreeMap<K, V>436where437	K: Typed + Ord,438	V: Typed,439{440	const TYPE: &'static ComplexValType = &ComplexValType::AttrsOf(V::TYPE);441}442impl<K, V> IntoUntyped for BTreeMap<K, V>443where444	K: Typed + Ord + IntoUntyped,445	V: Typed + IntoUntyped,446{447	fn into_untyped(typed: Self) -> Result<Val> {448		let mut out = ObjValueBuilder::with_capacity(typed.len());449		for (k, v) in typed {450			let Some(key) = K::into_untyped(k)?.as_str() else {451				bail!("map key should serialize to string");452			};453			let value = V::into_untyped(v)?;454			out.field(key).value(value);455		}456		Ok(Val::Obj(out.build()))457	}458}459impl<K, V> FromUntyped for BTreeMap<K, V>460where461	K: FromUntyped + Ord,462	V: FromUntyped,463{464	fn from_untyped(value: Val) -> Result<Self> {465		Self::TYPE.check(&value)?;466		let obj = value.as_obj().expect("typecheck should fail");467468		let mut out = Self::new();469		if V::wants_lazy() {470			for key in obj.fields_ex(471				false,472				#[cfg(feature = "exp-preserve-order")]473				false,474			) {475				let value = obj.get_lazy(key.clone()).expect("field exists");476				let value = V::from_lazy_untyped(value)?;477				let key = K::from_untyped(Val::Str(key.into()))?;478				let _ = out.insert(key, value);479			}480		} else {481			for (key, value) in obj.iter(482				#[cfg(feature = "exp-preserve-order")]483				false,484			) {485				let key = K::from_untyped(Val::Str(key.into()))?;486				let value = V::from_untyped(value?)?;487				let _ = out.insert(key, value);488			}489		}490		Ok(out)491	}492}493494impl Typed for Val {495	const TYPE: &'static ComplexValType = &ComplexValType::Any;496}497impl IntoUntyped for Val {498	fn into_untyped(typed: Self) -> Result<Val> {499		Ok(typed)500	}501}502impl FromUntyped for Val {503	fn from_untyped(untyped: Val) -> Result<Self> {504		Ok(untyped)505	}506}507508#[doc(hidden)]509impl<T> Typed for Result<T>510where511	T: Typed,512{513	const TYPE: &'static ComplexValType = &ComplexValType::Any;514}515impl<T: IntoUntyped> IntoUntypedResult for Result<T> {516	fn into_untyped_result(typed: Self) -> Result<Val> {517		typed.map(T::into_untyped)?518	}519}520521/// Specialization522impl Typed for IBytes {523	const TYPE: &'static ComplexValType =524		&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));525}526impl IntoUntyped for IBytes {527	fn into_untyped(value: Self) -> Result<Val> {528		Ok(Val::Arr(ArrValue::bytes(value)))529	}530}531impl FromUntyped for IBytes {532	fn from_untyped(value: Val) -> Result<Self> {533		let Val::Arr(a) = &value else {534			<Self as Typed>::TYPE.check(&value)?;535			unreachable!()536		};537		if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {538			return Ok(bytes.0.as_slice().into());539		}540		<Self as Typed>::TYPE.check(&value)?;541		// Any::downcast_ref::<ByteArray>(&a);542		let mut out = Vec::with_capacity(a.len());543		for e in a.iter() {544			let r = e?;545			out.push(u8::from_untyped(r)?);546		}547		Ok(out.as_slice().into())548	}549}550551pub struct M1;552impl Typed for M1 {553	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));554}555impl IntoUntyped for M1 {556	fn into_untyped(_: Self) -> Result<Val> {557		Ok(Val::Num(NumValue::new(-1.0).expect("finite")))558	}559}560impl FromUntyped for M1 {561	fn from_untyped(value: Val) -> Result<Self> {562		<Self as Typed>::TYPE.check(&value)?;563		Ok(Self)564	}565}566567macro_rules! decl_either {568	($($name: ident, $($id: ident)*);*) => {$(569		#[derive(Clone)]570		pub enum $name<$($id),*> {571			$($id($id)),*572		}573		impl<$($id),*> Typed for $name<$($id),*>574		where575			$($id: Typed,)*576		{577			const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);578		}579		impl<$($id),*> IntoUntyped for $name<$($id),*>580		where581			$($id: Typed + IntoUntyped,)*582		{583			fn into_untyped(value: Self) -> Result<Val> {584				match value {$(585					$name::$id(v) => $id::into_untyped(v)586				),*}587			}588		}589590		impl<$($id),*> FromUntyped for $name<$($id),*>591		where592			$($id: Typed + FromUntyped,)*593		{594			fn from_untyped(value: Val) -> Result<Self> {595				$(596					if $id::TYPE.check(&value).is_ok() {597						$id::from_untyped(value).map(Self::$id)598					} else599				)* {600					<Self as Typed>::TYPE.check(&value)?;601					unreachable!()602				}603			}604		}605	)*}606}607decl_either!(608	Either1, A;609	Either2, A B;610	Either3, A B C;611	Either4, A B C D;612	Either5, A B C D E;613	Either6, A B C D E F;614	Either7, A B C D E F G615);616#[macro_export]617macro_rules! Either {618	($a:ty) => {$crate::typed::Either1<$a>};619	($a:ty, $b:ty) => {$crate::typed::Either2<$a, $b>};620	($a:ty, $b:ty, $c:ty) => {$crate::typed::Either3<$a, $b, $c>};621	($a:ty, $b:ty, $c:ty, $d:ty) => {$crate::typed::Either4<$a, $b, $c, $d>};622	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {$crate::typed::Either5<$a, $b, $c, $d, $e>};623	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {$crate::typed::Either6<$a, $b, $c, $d, $e, $f>};624	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {$crate::typed::Either7<$a, $b, $c, $d, $e, $f, $g>};625}626pub use Either;627628pub type MyType = Either![u32, f64, String];629630impl Typed for ArrValue {631	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);632}633impl IntoUntyped for ArrValue {634	fn into_untyped(value: Self) -> Result<Val> {635		Ok(Val::Arr(value))636	}637}638impl FromUntyped for ArrValue {639	fn from_untyped(value: Val) -> Result<Self> {640		<Self as Typed>::TYPE.check(&value)?;641		match value {642			Val::Arr(a) => Ok(a),643			_ => unreachable!(),644		}645	}646}647648impl Typed for FuncVal {649	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);650}651impl IntoUntyped for FuncVal {652	fn into_untyped(value: Self) -> Result<Val> {653		Ok(Val::Func(value))654	}655}656impl FromUntyped for FuncVal {657	fn from_untyped(value: Val) -> Result<Self> {658		<Self as Typed>::TYPE.check(&value)?;659		match value {660			Val::Func(a) => Ok(a),661			_ => unreachable!(),662		}663	}664}665666impl Typed for ObjValue {667	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);668}669impl IntoUntyped for ObjValue {670	fn into_untyped(value: Self) -> Result<Val> {671		Ok(Val::Obj(value))672	}673}674impl FromUntyped for ObjValue {675	fn from_untyped(value: Val) -> Result<Self> {676		<Self as Typed>::TYPE.check(&value)?;677		match value {678			Val::Obj(a) => Ok(a),679			_ => unreachable!(),680		}681	}682}683684impl Typed for bool {685	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);686}687impl IntoUntyped for bool {688	fn into_untyped(value: Self) -> Result<Val> {689		Ok(Val::Bool(value))690	}691}692impl FromUntyped for bool {693	fn from_untyped(value: Val) -> Result<Self> {694		<Self as Typed>::TYPE.check(&value)?;695		match value {696			Val::Bool(a) => Ok(a),697			_ => unreachable!(),698		}699	}700}701702impl Typed for IndexableVal {703	const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[704		&ComplexValType::Simple(ValType::Arr),705		&ComplexValType::Simple(ValType::Str),706	]);707}708impl IntoUntyped for IndexableVal {709	fn into_untyped(value: Self) -> Result<Val> {710		match value {711			Self::Str(s) => Ok(Val::string(s)),712			Self::Arr(a) => Ok(Val::Arr(a)),713		}714	}715}716impl FromUntyped for IndexableVal {717	fn from_untyped(value: Val) -> Result<Self> {718		<Self as Typed>::TYPE.check(&value)?;719		value.into_indexable()720	}721}722723pub struct Null;724impl Typed for Null {725	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);726}727impl IntoUntyped for Null {728	fn into_untyped(_: Self) -> Result<Val> {729		Ok(Val::Null)730	}731}732impl FromUntyped for Null {733	fn from_untyped(value: Val) -> Result<Self> {734		<Self as Typed>::TYPE.check(&value)?;735		Ok(Self)736	}737}738739impl<T> Typed for Option<T>740where741	T: Typed,742{743	const TYPE: &'static ComplexValType =744		&ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);745}746impl<T> IntoUntyped for Option<T>747where748	T: Typed + IntoUntyped,749{750	fn into_untyped(typed: Self) -> Result<Val> {751		typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))752	}753}754impl<T> FromUntyped for Option<T>755where756	T: Typed + FromUntyped,757{758	fn from_untyped(untyped: Val) -> Result<Self> {759		if matches!(untyped, Val::Null) {760			Ok(None)761		} else {762			T::from_untyped(untyped).map(Some)763		}764	}765}766767impl Typed for NumValue {768	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);769}770impl IntoUntyped for NumValue {771	fn into_untyped(typed: Self) -> Result<Val> {772		Ok(Val::Num(typed))773	}774}775impl FromUntyped for NumValue {776	fn from_untyped(untyped: Val) -> Result<Self> {777		Self::TYPE.check(&untyped)?;778		match untyped {779			Val::Num(v) => Ok(v),780			_ => unreachable!(),781		}782	}783}