git.delta.rocks / jrsonnet / refs/commits / 06fa714fdb66

difftreelog

style apply clippy suggestions

Yaroslav Bolyukin2022-07-23parent: #50ca1d2.patch.diff
in: master

5 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -143,7 +143,7 @@
 pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, v: *const c_char) {
 	let cstr = CStr::from_ptr(v);
 	let path = PathBuf::from(cstr.to_str().unwrap());
-	let any_resolver = &vm.settings().import_resolver;
+	let any_resolver = vm.import_resolver();
 	let resolver = any_resolver
 		.as_any()
 		.downcast_ref::<NativeImportResolver>()
modifiedcrates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -53,25 +53,25 @@
 
 impl<T: ?Sized> Borrow<T> for TraceBox<T> {
 	fn borrow(&self) -> &T {
-		&*self.0
+		&self.0
 	}
 }
 
 impl<T: ?Sized> BorrowMut<T> for TraceBox<T> {
 	fn borrow_mut(&mut self) -> &mut T {
-		&mut *self.0
+		&mut self.0
 	}
 }
 
 impl<T: ?Sized> AsRef<T> for TraceBox<T> {
 	fn as_ref(&self) -> &T {
-		&*self.0
+		&self.0
 	}
 }
 
 impl<T: ?Sized> AsMut<T> for TraceBox<T> {
 	fn as_mut(&mut self) -> &mut T {
-		&mut *self.0
+		&mut self.0
 	}
 }
 
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -41,7 +41,7 @@
 	}
 
 	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("`as_any($self)` is not supported by dummy resolver")
+		panic!("`as_any(&self)` is not supported by dummy resolver")
 	}
 }
 #[allow(clippy::use_self)]
modifiedcrates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -774,10 +774,7 @@
 			format_arr(s.clone(), "%+-4o", &[Val::Num(8.0)]).unwrap(),
 			"+10 "
 		);
-		assert_eq!(
-			format_arr(s.clone(), "%+-04o", &[Val::Num(8.0)]).unwrap(),
-			"+10 "
-		);
+		assert_eq!(format_arr(s, "%+-04o", &[Val::Num(8.0)]).unwrap(), "+10 ");
 	}
 
 	#[test]
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/typed/conversions.rs
1use std::ops::Deref;23use jrsonnet_gcmodule::Cc;4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9	error::{Error::*, Result},10	function::{FuncDesc, FuncVal},11	throw,12	typed::CheckType,13	val::{ArrValue, IndexableVal},14	ObjValue, ObjValueBuilder, State, Val,15};1617pub trait TypedObj: Typed {18	fn serialize(self, s: State, out: &mut ObjValueBuilder) -> Result<()>;19	fn parse(obj: &ObjValue, s: State) -> Result<Self>;20	fn into_object(self, s: State) -> Result<ObjValue> {21		let mut builder = ObjValueBuilder::new();22		self.serialize(s, &mut builder)?;23		Ok(builder.build())24	}25}2627pub trait Typed: Sized {28	const TYPE: &'static ComplexValType;29	fn into_untyped(typed: Self, s: State) -> Result<Val>;30	fn from_untyped(untyped: Val, s: State) -> Result<Self>;31}3233macro_rules! impl_int {34	($($ty:ty)*) => {$(35		impl Typed for $ty {36			const TYPE: &'static ComplexValType =37				&ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));38			fn from_untyped(value: Val, s: State) -> Result<Self> {39				<Self as Typed>::TYPE.check(s, &value)?;40				match value {41					Val::Num(n) => {42						#[allow(clippy::float_cmp)]43						if n.trunc() != n {44							throw!(RuntimeError(45								format!(46									"cannot convert number with fractional part to {}",47									stringify!($ty)48								)49								.into()50							))51						}52						Ok(n as Self)53					}54					_ => unreachable!(),55				}56			}57			fn into_untyped(value: Self, _: State) -> Result<Val> {58				Ok(Val::Num(value as f64))59			}60		}61	)*};62}6364impl_int!(i8 u8 i16 u16 i32 u32);6566macro_rules! impl_bounded_int {67	($($name:ident = $ty:ty)*) => {$(68		#[derive(Clone, Copy)]69		pub struct $name<const MIN: $ty, const MAX: $ty>($ty);70		impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {71			pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {72				if value >= MIN && value <= MAX {73					Some(Self(value))74				} else {75					None76				}77			}78			pub const fn value(self) -> $ty {79				self.080			}81		}82		impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {83			type Target = $ty;84			fn deref(&self) -> &Self::Target {85				&self.086			}87		}8889		impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {90			const TYPE: &'static ComplexValType =91				&ComplexValType::BoundedNumber(92					Some(MIN as f64),93					Some(MAX as f64),94				);9596			fn from_untyped(value: Val, s: State) -> Result<Self> {97				<Self as Typed>::TYPE.check(s, &value)?;98				match value {99					Val::Num(n) => {100						#[allow(clippy::float_cmp)]101						if n.trunc() != n {102							throw!(RuntimeError(103								format!(104									"cannot convert number with fractional part to {}",105									stringify!($ty)106								)107								.into()108							))109						}110						Ok(Self(n as $ty))111					}112					_ => unreachable!(),113				}114			}115116			fn into_untyped(value: Self, _: State) -> Result<Val> {117				Ok(Val::Num(value.0 as f64))118			}119		}120	)*};121}122123impl_bounded_int!(124	BoundedI8 = i8125	BoundedI16 = i16126	BoundedI32 = i32127	BoundedI64 = i64128	BoundedUsize = usize129);130131impl Typed for f64 {132	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);133134	fn into_untyped(value: Self, _: State) -> Result<Val> {135		Ok(Val::Num(value))136	}137138	fn from_untyped(value: Val, s: State) -> Result<Self> {139		<Self as Typed>::TYPE.check(s, &value)?;140		match value {141			Val::Num(n) => Ok(n),142			_ => unreachable!(),143		}144	}145}146147pub struct PositiveF64(pub f64);148impl Typed for PositiveF64 {149	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);150151	fn into_untyped(value: Self, _: State) -> Result<Val> {152		Ok(Val::Num(value.0))153	}154155	fn from_untyped(value: Val, s: State) -> Result<Self> {156		<Self as Typed>::TYPE.check(s, &value)?;157		match value {158			Val::Num(n) => Ok(Self(n)),159			_ => unreachable!(),160		}161	}162}163impl Typed for usize {164	// It is possible to store 54 bits of precision in f64, but leaving u32::MAX here for compatibility165	const TYPE: &'static ComplexValType =166		&ComplexValType::BoundedNumber(Some(0.0), Some(u32::MAX as f64));167168	fn into_untyped(value: Self, _: State) -> Result<Val> {169		if value > u32::MAX as Self {170			throw!(RuntimeError("number is too large".into()))171		}172		Ok(Val::Num(value as f64))173	}174175	fn from_untyped(value: Val, s: State) -> Result<Self> {176		<Self as Typed>::TYPE.check(s, &value)?;177		match value {178			Val::Num(n) => {179				#[allow(clippy::float_cmp)]180				if n.trunc() != n {181					throw!(RuntimeError(182						"cannot convert number with fractional part to usize".into()183					))184				}185				Ok(n as Self)186			}187			_ => unreachable!(),188		}189	}190}191192impl Typed for IStr {193	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);194195	fn into_untyped(value: Self, _: State) -> Result<Val> {196		Ok(Val::Str(value))197	}198199	fn from_untyped(value: Val, s: State) -> Result<Self> {200		<Self as Typed>::TYPE.check(s, &value)?;201		match value {202			Val::Str(s) => Ok(s),203			_ => unreachable!(),204		}205	}206}207208impl Typed for String {209	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);210211	fn into_untyped(value: Self, _: State) -> Result<Val> {212		Ok(Val::Str(value.into()))213	}214215	fn from_untyped(value: Val, s: State) -> Result<Self> {216		<Self as Typed>::TYPE.check(s, &value)?;217		match value {218			Val::Str(s) => Ok(s.to_string()),219			_ => unreachable!(),220		}221	}222}223224impl Typed for char {225	const TYPE: &'static ComplexValType = &ComplexValType::Char;226227	fn into_untyped(value: Self, _: State) -> Result<Val> {228		Ok(Val::Str(value.to_string().into()))229	}230231	fn from_untyped(value: Val, s: State) -> Result<Self> {232		<Self as Typed>::TYPE.check(s, &value)?;233		match value {234			Val::Str(s) => Ok(s.chars().next().unwrap()),235			_ => unreachable!(),236		}237	}238}239240impl<T> Typed for Vec<T>241where242	T: Typed,243{244	const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);245246	fn into_untyped(value: Self, s: State) -> Result<Val> {247		let mut o = Vec::with_capacity(value.len());248		for i in value {249			o.push(T::into_untyped(i, s.clone())?);250		}251		Ok(Val::Arr(o.into()))252	}253254	fn from_untyped(value: Val, s: State) -> Result<Self> {255		<Self as Typed>::TYPE.check(s.clone(), &value)?;256		match value {257			Val::Arr(a) => {258				let mut o = Self::with_capacity(a.len());259				for i in a.iter(s.clone()) {260					o.push(T::from_untyped(i?, s.clone())?);261				}262				Ok(o)263			}264			_ => unreachable!(),265		}266	}267}268269/// To be used in Vec<Any>270/// Regular Val can't be used here, because it has wrong `TryFrom::Error` type271#[derive(Clone)]272pub struct Any(pub Val);273274impl Typed for Any {275	const TYPE: &'static ComplexValType = &ComplexValType::Any;276277	fn into_untyped(value: Self, _: State) -> Result<Val> {278		Ok(value.0)279	}280281	fn from_untyped(value: Val, _: State) -> Result<Self> {282		Ok(Self(value))283	}284}285286/// Specialization, provides faster `TryFrom<VecVal>` for Val287pub struct VecVal(pub Cc<Vec<Val>>);288289impl Typed for VecVal {290	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);291292	fn into_untyped(value: Self, _: State) -> Result<Val> {293		Ok(Val::Arr(ArrValue::Eager(value.0)))294	}295296	fn from_untyped(value: Val, s: State) -> Result<Self> {297		<Self as Typed>::TYPE.check(s.clone(), &value)?;298		match value {299			Val::Arr(a) => Ok(Self(a.evaluated(s)?)),300			_ => unreachable!(),301		}302	}303}304305/// Specialization306impl Typed for IBytes {307	const TYPE: &'static ComplexValType =308		&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));309310	fn into_untyped(value: Self, _: State) -> Result<Val> {311		Ok(Val::Arr(ArrValue::Bytes(value)))312	}313314	fn from_untyped(value: Val, s: State) -> Result<Self> {315		if let Val::Arr(ArrValue::Bytes(bytes)) = value {316			return Ok(bytes);317		}318		<Self as Typed>::TYPE.check(s.clone(), &value)?;319		match value {320			Val::Arr(a) => {321				let mut out = Vec::with_capacity(a.len());322				for e in a.iter(s.clone()) {323					let r = e?;324					out.push(u8::from_untyped(r, s.clone())?);325				}326				Ok(out.as_slice().into())327			}328			_ => unreachable!(),329		}330	}331}332333pub struct M1;334impl Typed for M1 {335	const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));336337	fn into_untyped(_: Self, _: State) -> Result<Val> {338		Ok(Val::Num(-1.0))339	}340341	fn from_untyped(value: Val, s: State) -> Result<Self> {342		<Self as Typed>::TYPE.check(s, &value)?;343		Ok(Self)344	}345}346347macro_rules! decl_either {348	($($name: ident, $($id: ident)*);*) => {$(349		pub enum $name<$($id),*> {350			$($id($id)),*351		}352		impl<$($id),*> Typed for $name<$($id),*>353		where354			$($id: Typed,)*355		{356			const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);357358			fn into_untyped(value: Self, s: State) -> Result<Val> {359				match value {$(360					$name::$id(v) => $id::into_untyped(v, s)361				),*}362			}363364			fn from_untyped(value: Val, s: State) -> Result<Self> {365				$(366					if $id::TYPE.check(s.clone(), &value).is_ok() {367						$id::from_untyped(value, s.clone()).map(Self::$id)368					} else369				)* {370					<Self as Typed>::TYPE.check(s, &value)?;371					unreachable!()372				}373			}374		}375	)*}376}377decl_either!(378	Either1, A;379	Either2, A B;380	Either3, A B C;381	Either4, A B C D;382	Either5, A B C D E;383	Either6, A B C D E F;384	Either7, A B C D E F G385);386#[macro_export]387macro_rules! Either {388	($a:ty) => {Either1<$a>};389	($a:ty, $b:ty) => {Either2<$a, $b>};390	($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};391	($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};392	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};393	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};394	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};395}396397pub type MyType = Either![u32, f64, String];398399impl Typed for ArrValue {400	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);401402	fn into_untyped(value: Self, _: State) -> Result<Val> {403		Ok(Val::Arr(value))404	}405406	fn from_untyped(value: Val, s: State) -> Result<Self> {407		<Self as Typed>::TYPE.check(s, &value)?;408		match value {409			Val::Arr(a) => Ok(a),410			_ => unreachable!(),411		}412	}413}414415impl Typed for FuncVal {416	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);417418	fn into_untyped(value: Self, _: State) -> Result<Val> {419		Ok(Val::Func(value))420	}421422	fn from_untyped(value: Val, s: State) -> Result<Self> {423		<Self as Typed>::TYPE.check(s, &value)?;424		match value {425			Val::Func(a) => Ok(a),426			_ => unreachable!(),427		}428	}429}430431impl Typed for Cc<FuncDesc> {432	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);433434	fn into_untyped(value: Self, _: State) -> Result<Val> {435		Ok(Val::Func(FuncVal::Normal(value)))436	}437438	fn from_untyped(value: Val, s: State) -> Result<Self> {439		<Self as Typed>::TYPE.check(s, &value)?;440		match value {441			Val::Func(FuncVal::Normal(desc)) => Ok(desc),442			Val::Func(_) => throw!(RuntimeError("expected normal function, not builtin".into())),443			_ => unreachable!(),444		}445	}446}447448impl Typed for ObjValue {449	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);450451	fn into_untyped(value: Self, _: State) -> Result<Val> {452		Ok(Val::Obj(value))453	}454455	fn from_untyped(value: Val, s: State) -> Result<Self> {456		<Self as Typed>::TYPE.check(s, &value)?;457		match value {458			Val::Obj(a) => Ok(a),459			_ => unreachable!(),460		}461	}462}463464impl Typed for bool {465	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);466467	fn into_untyped(value: Self, _: State) -> Result<Val> {468		Ok(Val::Bool(value))469	}470471	fn from_untyped(value: Val, s: State) -> Result<Self> {472		<Self as Typed>::TYPE.check(s, &value)?;473		match value {474			Val::Bool(a) => Ok(a),475			_ => unreachable!(),476		}477	}478}479impl Typed for IndexableVal {480	const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[481		&ComplexValType::Simple(ValType::Arr),482		&ComplexValType::Simple(ValType::Str),483	]);484485	fn into_untyped(value: Self, _: State) -> Result<Val> {486		match value {487			IndexableVal::Str(s) => Ok(Val::Str(s)),488			IndexableVal::Arr(a) => Ok(Val::Arr(a)),489		}490	}491492	fn from_untyped(value: Val, s: State) -> Result<Self> {493		<Self as Typed>::TYPE.check(s, &value)?;494		value.into_indexable()495	}496}497498pub struct Null;499impl Typed for Null {500	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);501502	fn into_untyped(_: Self, _: State) -> Result<Val> {503		Ok(Val::Null)504	}505506	fn from_untyped(value: Val, s: State) -> Result<Self> {507		<Self as Typed>::TYPE.check(s, &value)?;508		Ok(Self)509	}510}