git.delta.rocks / jrsonnet / refs/commits / 86671948a995

difftreelog

feat macro name interning

vtqzklkwYaroslav Bolyukin2026-03-22parent: #795a53d.patch.diff
in: master

9 files changed

modifiedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -11,6 +11,10 @@
 workspace = true
 
 [features]
+default = [
+    "exp-regex",
+]
+
 experimental = [
     "exp-preserve-order",
     "exp-destruct",
@@ -18,7 +22,6 @@
     "exp-object-iteration",
     "exp-bigint",
     "exp-apply",
-    "exp-regex",
 ]
 # Use mimalloc as allocator
 mimalloc = ["mimallocator"]
modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/arr/mod.rs
1use std::{2	any::Any,3	fmt::{self},4	num::NonZeroU32,5	rc::Rc,6};78use jrsonnet_gcmodule::{cc_dyn, Cc};9use jrsonnet_interner::IBytes;10use jrsonnet_parser::{Expr, Spanned};1112use crate::{function::NativeFn, Context, Result, Thunk, Val};1314mod spec;15pub use spec::{ArrayLike, *};1617cc_dyn!(18	#[doc = "Represents a Jsonnet array value."]19	#[derive(Clone)]20	ArrValue,21	ArrayLike,22	pub fn new() {...}23);24impl fmt::Debug for ArrValue {25	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {26		self.0.fmt(f)27	}28}2930pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}31impl<I, T> ArrayLikeIter<T> for I where32	I: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator33{34}3536impl ArrValue {37	pub fn empty() -> Self {38		Self::new(RangeArray::empty())39	}4041	pub fn expr(ctx: Context, exprs: Rc<Vec<Spanned<Expr>>>) -> Self {42		Self::new(ExprArray::new(ctx, exprs))43	}4445	pub fn lazy(thunks: Vec<Thunk<Val>>) -> Self {46		Self::new(LazyArray(thunks))47	}4849	pub fn eager(values: Vec<Val>) -> Self {50		Self::new(EagerArray(values))51	}5253	pub fn repeated(data: Self, repeats: usize) -> Option<Self> {54		Some(Self::new(RepeatedArray::new(data, repeats)?))55	}5657	pub fn bytes(bytes: IBytes) -> Self {58		Self::new(BytesArray(bytes))59	}60	pub fn chars(chars: impl Iterator<Item = char>) -> Self {61		Self::new(CharArray(chars.collect()))62	}6364	#[must_use]65	pub fn map(self, mapper: NativeFn!((Val) -> Val)) -> Self {66		Self::new(<MappedArray>::new(self, ArrayMapper::Plain(mapper)))67	}6869	#[must_use]70	pub fn map_with_index(self, mapper: NativeFn!((u32, Val) -> Val)) -> Self {71		Self::new(<MappedArray>::new(self, ArrayMapper::WithIndex(mapper)))72	}7374	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {75		// TODO: ArrValue::Picked(inner, indexes) for large arrays76		let mut out = Vec::new();77		for i in self.iter() {78			let i = i?;79			if filter(&i)? {80				out.push(i);81			}82		}83		Ok(Self::eager(out))84	}8586	pub fn extended(a: Self, b: Self) -> Self {87		// TODO: benchmark for an optimal value, currently just a arbitrary choice88		const ARR_EXTEND_THRESHOLD: usize = 100;8990		if a.is_empty() {91			b92		} else if b.is_empty() {93			a94		} else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {95			Self::new(ExtendedArray::new(a, b))96		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {97			let mut out = Vec::with_capacity(a.len() + b.len());98			out.extend(a);99			out.extend(b);100			Self::eager(out)101		} else {102			let mut out = Vec::with_capacity(a.len() + b.len());103			out.extend(a.iter_lazy());104			out.extend(b.iter_lazy());105			Self::lazy(out)106		}107	}108109	pub fn range_exclusive(a: i32, b: i32) -> Self {110		Self::new(RangeArray::new_exclusive(a, b))111	}112	pub fn range_inclusive(a: i32, b: i32) -> Self {113		Self::new(RangeArray::new_inclusive(a, b))114	}115116	#[must_use]117	pub fn slice(self, index: Option<i32>, end: Option<i32>, step: Option<NonZeroU32>) -> Self {118		let get_idx = |pos: Option<i32>, len: usize, default| match pos {119			Some(v) if v < 0 => len.saturating_sub((-v) as usize),120			Some(v) => (v as usize).min(len),121			None => default,122		};123		let index = get_idx(index, self.len(), 0);124		let end = get_idx(end, self.len(), self.len());125		let step = step.unwrap_or_else(|| NonZeroU32::new(1).expect("1 != 0"));126127		if index >= end {128			return Self::empty();129		}130131		Self::new(SliceArray {132			inner: self,133			from: index as u32,134			to: end as u32,135			step: step.get(),136		})137	}138139	/// Array length.140	pub fn len(&self) -> usize {141		self.0.len()142	}143144	/// Is array contains no elements?145	pub fn is_empty(&self) -> bool {146		self.0.is_empty()147	}148149	/// Get array element by index, evaluating it, if it is lazy.150	///151	/// Returns `None` on out-of-bounds condition.152	pub fn get(&self, index: usize) -> Result<Option<Val>> {153		self.0.get(index)154	}155156	/// Returns None if get is either non cheap, or out of bounds157	/// Note that non-cheap access includes errorable values158	///159	/// Prefer it to `get_lazy`, but use `get` when you can.160	fn get_cheap(&self, index: usize) -> Option<Val> {161		self.0.get_cheap(index)162	}163164	/// Get array element by index, without evaluation.165	///166	/// Returns `None` on out-of-bounds condition.167	pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {168		self.0.get_lazy(index)169	}170171	pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {172		(0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))173	}174175	/// Iterate over elements, returning lazy values.176	pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {177		(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))178	}179180	/// Prefer it over `iter_lazy`, but do not use it where `iter` will do.181	pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {182		if self.is_cheap() {183			Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))184		} else {185			None186		}187	}188189	/// Return a reversed view on current array.190	#[must_use]191	pub fn reversed(self) -> Self {192		Self::new(ReverseArray(self))193	}194195	pub fn ptr_eq(a: &Self, b: &Self) -> bool {196		Cc::ptr_eq(&a.0, &b.0)197	}198199	/// Is this vec supports `.get_cheap()?`200	pub fn is_cheap(&self) -> bool {201		self.0.is_cheap()202	}203204	pub fn as_any(&self) -> &dyn Any {205		&self.0206	}207}208impl From<Vec<Val>> for ArrValue {209	fn from(value: Vec<Val>) -> Self {210		Self::eager(value)211	}212}213impl From<Vec<Thunk<Val>>> for ArrValue {214	fn from(value: Vec<Thunk<Val>>) -> Self {215		Self::lazy(value)216	}217}218impl FromIterator<Val> for ArrValue {219	fn from_iter<T: IntoIterator<Item = Val>>(iter: T) -> Self {220		Self::eager(iter.into_iter().collect())221	}222}223impl ArrayLike for ArrValue {224	fn len(&self) -> usize {225		self.0.len()226	}227228	fn get(&self, index: usize) -> Result<Option<Val>> {229		self.0.get(index)230	}231232	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {233		self.0.get_lazy(index)234	}235236	fn get_cheap(&self, index: usize) -> Option<Val> {237		self.0.get_cheap(index)238	}239240	fn is_cheap(&self) -> bool {241		self.0.is_cheap()242	}243}
after · crates/jrsonnet-evaluator/src/arr/mod.rs
1use std::{2	any::Any,3	fmt::{self},4	num::NonZeroU32,5	rc::Rc,6};78use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};9use jrsonnet_interner::IBytes;10use jrsonnet_parser::{Expr, Spanned};1112use crate::{function::NativeFn, typed::Typed, Context, Result, Thunk, Val};1314mod spec;15pub use spec::{ArrayLike, *};1617cc_dyn!(18	#[doc = "Represents a Jsonnet array value."]19	#[derive(Clone)]20	ArrValue,21	ArrayLike,22	pub fn new() {...}23);24impl fmt::Debug for ArrValue {25	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {26		self.0.fmt(f)27	}28}2930pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}31impl<I, T> ArrayLikeIter<T> for I where32	I: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator33{34}3536impl ArrValue {37	pub fn empty() -> Self {38		Self::new(RangeArray::empty())39	}4041	pub fn expr(ctx: Context, exprs: Rc<Vec<Spanned<Expr>>>) -> Self {42		Self::new(ExprArray::new(ctx, exprs))43	}4445	pub fn lazy(thunks: Vec<Thunk<Val>>) -> Self {46		Self::new(LazyArray(thunks))47	}4849	pub fn eager(values: Vec<Val>) -> Self {50		Self::new(EagerArray(values))51	}5253	pub fn repeated(data: Self, repeats: usize) -> Option<Self> {54		Some(Self::new(RepeatedArray::new(data, repeats)?))55	}5657	pub fn bytes(bytes: IBytes) -> Self {58		Self::new(BytesArray(bytes))59	}60	pub fn chars(chars: impl Iterator<Item = char>) -> Self {61		Self::new(CharArray(chars.collect()))62	}6364	#[must_use]65	pub fn map(self, mapper: NativeFn!((Val) -> Val)) -> Self {66		Self::new(<MappedArray>::new(self, ArrayMapper::Plain(mapper)))67	}6869	#[must_use]70	pub fn map_with_index(self, mapper: NativeFn!((u32, Val) -> Val)) -> Self {71		Self::new(<MappedArray>::new(self, ArrayMapper::WithIndex(mapper)))72	}7374	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {75		// TODO: ArrValue::Picked(inner, indexes) for large arrays76		let mut out = Vec::new();77		for i in self.iter() {78			let i = i?;79			if filter(&i)? {80				out.push(i);81			}82		}83		Ok(Self::eager(out))84	}8586	pub fn extended(a: Self, b: Self) -> Self {87		// TODO: benchmark for an optimal value, currently just a arbitrary choice88		const ARR_EXTEND_THRESHOLD: usize = 100;8990		if a.is_empty() {91			b92		} else if b.is_empty() {93			a94		} else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {95			Self::new(ExtendedArray::new(a, b))96		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {97			let mut out = Vec::with_capacity(a.len() + b.len());98			out.extend(a);99			out.extend(b);100			Self::eager(out)101		} else {102			let mut out = Vec::with_capacity(a.len() + b.len());103			out.extend(a.iter_lazy());104			out.extend(b.iter_lazy());105			Self::lazy(out)106		}107	}108109	pub fn range_exclusive(a: i32, b: i32) -> Self {110		Self::new(RangeArray::new_exclusive(a, b))111	}112	pub fn range_inclusive(a: i32, b: i32) -> Self {113		Self::new(RangeArray::new_inclusive(a, b))114	}115116	#[must_use]117	pub fn slice(self, index: Option<i32>, end: Option<i32>, step: Option<NonZeroU32>) -> Self {118		let get_idx = |pos: Option<i32>, len: usize, default| match pos {119			Some(v) if v < 0 => len.saturating_sub((-v) as usize),120			Some(v) => (v as usize).min(len),121			None => default,122		};123		let index = get_idx(index, self.len(), 0);124		let end = get_idx(end, self.len(), self.len());125		let step = step.unwrap_or_else(|| NonZeroU32::new(1).expect("1 != 0"));126127		if index >= end {128			return Self::empty();129		}130131		Self::new(SliceArray {132			inner: self,133			from: index as u32,134			to: end as u32,135			step: step.get(),136		})137	}138139	/// Array length.140	pub fn len(&self) -> usize {141		self.0.len()142	}143144	/// Is array contains no elements?145	pub fn is_empty(&self) -> bool {146		self.0.is_empty()147	}148149	/// Get array element by index, evaluating it, if it is lazy.150	///151	/// Returns `None` on out-of-bounds condition.152	pub fn get(&self, index: usize) -> Result<Option<Val>> {153		self.0.get(index)154	}155156	/// Returns None if get is either non cheap, or out of bounds157	/// Note that non-cheap access includes errorable values158	///159	/// Prefer it to `get_lazy`, but use `get` when you can.160	fn get_cheap(&self, index: usize) -> Option<Val> {161		self.0.get_cheap(index)162	}163164	/// Get array element by index, without evaluation.165	///166	/// Returns `None` on out-of-bounds condition.167	pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {168		self.0.get_lazy(index)169	}170171	pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {172		(0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))173	}174175	/// Iterate over elements, returning lazy values.176	pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {177		(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))178	}179180	/// Prefer it over `iter_lazy`, but do not use it where `iter` will do.181	pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {182		if self.is_cheap() {183			Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))184		} else {185			None186		}187	}188189	/// Return a reversed view on current array.190	#[must_use]191	pub fn reversed(self) -> Self {192		Self::new(ReverseArray(self))193	}194195	pub fn ptr_eq(a: &Self, b: &Self) -> bool {196		Cc::ptr_eq(&a.0, &b.0)197	}198199	/// Is this vec supports `.get_cheap()?`200	pub fn is_cheap(&self) -> bool {201		self.0.is_cheap()202	}203204	pub fn as_any(&self) -> &dyn Any {205		&self.0206	}207}208impl From<Vec<Val>> for ArrValue {209	fn from(value: Vec<Val>) -> Self {210		Self::eager(value)211	}212}213impl From<Vec<Thunk<Val>>> for ArrValue {214	fn from(value: Vec<Thunk<Val>>) -> Self {215		Self::lazy(value)216	}217}218impl FromIterator<Val> for ArrValue {219	fn from_iter<T: IntoIterator<Item = Val>>(iter: T) -> Self {220		Self::eager(iter.into_iter().collect())221	}222}223impl ArrayLike for ArrValue {224	fn len(&self) -> usize {225		self.0.len()226	}227228	fn get(&self, index: usize) -> Result<Option<Val>> {229		self.0.get(index)230	}231232	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {233		self.0.get_lazy(index)234	}235236	fn get_cheap(&self, index: usize) -> Option<Val> {237		self.0.get_cheap(index)238	}239240	fn is_cheap(&self) -> bool {241		self.0.is_cheap()242	}243}244
modifiedcrates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj/mod.rs
+++ b/crates/jrsonnet-evaluator/src/obj/mod.rs
@@ -12,11 +12,12 @@
 use educe::Educe;
 use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace, Weak};
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{Span, Visibility};
+use jrsonnet_parser::Span;
 use rustc_hash::{FxHashMap, FxHashSet};
 
 mod oop;
 
+pub use jrsonnet_parser::Visibility;
 pub use oop::ObjValueBuilder;
 
 use crate::{
@@ -30,7 +31,7 @@
 };
 
 #[cfg(not(feature = "exp-preserve-order"))]
-mod ordering {
+pub mod ordering {
 	#![allow(
 		// This module works as stub for preserve-order feature
 		clippy::unused_self,
@@ -41,6 +42,9 @@
 	#[derive(Clone, Copy, Default, Debug, Trace)]
 	pub struct FieldIndex(());
 	impl FieldIndex {
+		pub fn absolute(_v: u32) -> Self {
+			Self(())
+		}
 		pub const fn next(self) -> Self {
 			Self(())
 		}
@@ -54,7 +58,7 @@
 }
 
 #[cfg(feature = "exp-preserve-order")]
-mod ordering {
+pub mod ordering {
 	use std::cmp::Reverse;
 
 	use jrsonnet_gcmodule::Trace;
@@ -62,6 +66,9 @@
 	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]
 	pub struct FieldIndex(u32);
 	impl FieldIndex {
+		pub fn absolute(v: u32) -> Self {
+			Self(v)
+		}
 		pub fn next(self) -> Self {
 			Self(self.0 + 1)
 		}
@@ -149,7 +156,7 @@
 	Pending,
 }
 
-type EnumFieldsHandler<'a> =
+pub type EnumFieldsHandler<'a> =
 	dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;
 
 pub enum EnumFields {
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -21,6 +21,8 @@
 mod inner;
 use inner::Inner;
 
+mod names;
+
 /// Interned string
 ///
 /// Provides O(1) comparsions and hashing, cheap copy, and cheap conversion to [`IBytes`]
addedcrates/jrsonnet-interner/src/names.rsdiffbeforeafterboth

no changes

modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -13,6 +13,11 @@
 	LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,
 };
 
+use self::typed::derive_typed_inner;
+
+mod typed;
+mod names;
+
 fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>
 where
 	Ident: PartialEq<I>,
@@ -435,278 +440,6 @@
 			}
 		};
 	})
-}
-
-#[derive(Default)]
-#[allow(clippy::struct_excessive_bools)]
-struct TypedAttr {
-	rename: Option<String>,
-	aliases: Vec<String>,
-	flatten: bool,
-	/// flatten(ok) strategy for flattened optionals
-	/// field would be None in case of any parsing error (as in serde)
-	flatten_ok: bool,
-	// Should it be `field+:` instead of `field:`
-	add: bool,
-	// Should it be `field::` instead of `field:`
-	hide: bool,
-}
-impl Parse for TypedAttr {
-	fn parse(input: ParseStream) -> syn::Result<Self> {
-		let mut out = Self::default();
-		loop {
-			let lookahead = input.lookahead1();
-			if lookahead.peek(kw::rename) {
-				input.parse::<kw::rename>()?;
-				input.parse::<Token![=]>()?;
-				let name = input.parse::<LitStr>()?;
-				if out.rename.is_some() {
-					return Err(Error::new(
-						name.span(),
-						"rename attribute may only be specified once",
-					));
-				}
-				out.rename = Some(name.value());
-			} else if lookahead.peek(kw::alias) {
-				input.parse::<kw::alias>()?;
-				input.parse::<Token![=]>()?;
-				let alias = input.parse::<LitStr>()?;
-				out.aliases.push(alias.value());
-			} else if lookahead.peek(kw::flatten) {
-				input.parse::<kw::flatten>()?;
-				out.flatten = true;
-				if input.peek(token::Paren) {
-					let content;
-					parenthesized!(content in input);
-					let lookahead = content.lookahead1();
-					if lookahead.peek(kw::ok) {
-						content.parse::<kw::ok>()?;
-						out.flatten_ok = true;
-					} else {
-						return Err(lookahead.error());
-					}
-				}
-			} else if lookahead.peek(kw::add) {
-				input.parse::<kw::add>()?;
-				out.add = true;
-			} else if lookahead.peek(kw::hide) {
-				input.parse::<kw::hide>()?;
-				out.hide = true;
-			} else if input.is_empty() {
-				break;
-			} else {
-				return Err(lookahead.error());
-			}
-			if input.peek(Token![,]) {
-				input.parse::<Token![,]>()?;
-			} else {
-				break;
-			}
-		}
-		Ok(out)
-	}
-}
-
-struct TypedField {
-	attr: TypedAttr,
-	ident: Ident,
-	ty: Type,
-	is_option: bool,
-	is_lazy: bool,
-}
-impl TypedField {
-	fn parse(field: &syn::Field) -> Result<Self> {
-		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();
-		let Some(ident) = field.ident.clone() else {
-			return Err(Error::new(
-				field.span(),
-				"this field should appear in output object, but it has no visible name",
-			));
-		};
-		let (is_option, ty) = extract_type_from_option(&field.ty)?
-			.map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));
-		if is_option && attr.flatten {
-			if !attr.flatten_ok {
-				return Err(Error::new(
-					field.span(),
-					"strategy should be set when flattening Option",
-				));
-			}
-		} else if attr.flatten_ok {
-			return Err(Error::new(
-				field.span(),
-				"flatten(ok) is only useable on optional fields",
-			));
-		}
-
-		let is_lazy = type_is_path(&ty, "Thunk").is_some();
-
-		Ok(Self {
-			attr,
-			ident,
-			ty,
-			is_option,
-			is_lazy,
-		})
-	}
-	/// None if this field is flattened in jsonnet output
-	fn name(&self) -> Option<String> {
-		if self.attr.flatten {
-			return None;
-		}
-		Some(
-			self.attr
-				.rename
-				.clone()
-				.unwrap_or_else(|| self.ident.to_string()),
-		)
-	}
-
-	fn expand_field(&self) -> Option<TokenStream> {
-		if self.is_option {
-			return None;
-		}
-		let name = self.name()?;
-		let ty = &self.ty;
-		Some(quote! {
-			(#name, <#ty as Typed>::TYPE)
-		})
-	}
-
-	fn expand_parse(&self) -> TokenStream {
-		if self.is_option {
-			self.expand_parse_optional()
-		} else {
-			self.expand_parse_mandatory()
-		}
-	}
-
-	fn expand_parse_optional(&self) -> TokenStream {
-		let ident = &self.ident;
-		let ty = &self.ty;
-
-		// optional flatten is handled in same way as serde
-		if self.attr.flatten {
-			return quote! {
-				#ident: <#ty as TypedObj>::parse(&obj).ok(),
-			};
-		}
-
-		let name = self.name().unwrap();
-		let aliases = &self.attr.aliases;
-
-		quote! {
-			#ident: {
-				let __value = if let Some(__v) = obj.get(#name.into())? {
-					Some(__v)
-				} #(else if let Some(__v) = obj.get(#aliases.into())? {
-					Some(__v)
-				})* else {
-					None
-				};
-
-				__value.map(<#ty as Typed>::from_untyped).transpose()?
-			},
-		}
-	}
-
-	fn expand_parse_mandatory(&self) -> TokenStream {
-		let ident = &self.ident;
-		let ty = &self.ty;
-
-		// optional flatten is handled in same way as serde
-		if self.attr.flatten {
-			return quote! {
-				#ident: <#ty as TypedObj>::parse(&obj)?,
-			};
-		}
-
-		let name = self.name().unwrap();
-		let aliases = &self.attr.aliases;
-
-		let error_text = if aliases.is_empty() {
-			// clippy does not understand name variable usage in quote! macro
-			#[allow(clippy::redundant_clone)]
-			name.clone()
-		} else {
-			format!("{name} (alias {})", aliases.join(", "))
-		};
-
-		quote! {
-			#ident: {
-				let __value = if let Some(__v) = obj.get(#name.into())? {
-					__v
-				} #(else if let Some(__v) = obj.get(#aliases.into())? {
-					__v
-				})* else {
-					return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());
-				};
-
-				<#ty as Typed>::from_untyped(__value)?
-			},
-		}
-	}
-
-	fn expand_serialize(&self) -> TokenStream {
-		let ident = &self.ident;
-		let ty = &self.ty;
-		self.name().map_or_else(
-			|| {
-				if self.is_option {
-					quote! {
-						if let Some(value) = self.#ident {
-							<#ty as TypedObj>::serialize(value, out)?;
-						}
-					}
-				} else {
-					quote! {
-						<#ty as TypedObj>::serialize(self.#ident, out)?;
-					}
-				}
-			},
-			|name| {
-				let hide = if self.attr.hide {
-					quote! {.hide()}
-				} else {
-					quote! {}
-				};
-				let add = if self.attr.add {
-					quote! {.add()}
-				} else {
-					quote! {}
-				};
-				let value = if self.is_lazy {
-					quote! {
-						out.field(#name)
-							#hide
-							#add
-							.try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
-					}
-				} else {
-					quote! {
-						out.field(#name)
-							#hide
-							#add
-							.try_value(<#ty as Typed>::into_untyped(value)?)?;
-					}
-				};
-				if self.is_option {
-					quote! {
-						if let Some(value) = self.#ident {
-							#value
-						}
-					}
-				} else {
-					quote! {
-						{
-							let value = self.#ident;
-							#value
-						}
-					}
-				}
-			},
-		)
-	}
 }
 
 #[proc_macro_derive(Typed, attributes(typed))]
@@ -717,79 +450,6 @@
 		Ok(v) => v.into(),
 		Err(e) => e.to_compile_error().into(),
 	}
-}
-
-fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
-	let syn::Data::Struct(data) = &input.data else {
-		return Err(Error::new(input.span(), "only structs supported"));
-	};
-
-	let ident = &input.ident;
-	let fields = data
-		.fields
-		.iter()
-		.map(TypedField::parse)
-		.collect::<Result<Vec<_>>>()?;
-
-	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
-
-	let typed = {
-		let fields = fields
-			.iter()
-			.filter_map(TypedField::expand_field)
-			.collect::<Vec<_>>();
-		quote! {
-			impl #impl_generics Typed for #ident #ty_generics #where_clause {
-				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[
-					#(#fields,)*
-				]);
-
-				fn from_untyped(value: Val) -> JrResult<Self> {
-					let obj = value.as_obj().expect("shape is correct");
-					Self::parse(&obj)
-				}
-
-				fn into_untyped(value: Self) -> JrResult<Val> {
-					let mut out = ObjValueBuilder::new();
-					value.serialize(&mut out)?;
-					Ok(Val::Obj(out.build()))
-				}
-
-			}
-		}
-	};
-
-	let fields_parse = fields.iter().map(TypedField::expand_parse);
-	let fields_serialize = fields
-		.iter()
-		.map(TypedField::expand_serialize)
-		.collect::<Vec<_>>();
-
-	Ok(quote! {
-		const _: () = {
-			use ::jrsonnet_evaluator::{
-				typed::{ComplexValType, Typed, TypedObj, CheckType},
-				Val, State,
-				error::{ErrorKind, Result as JrResult},
-				ObjValueBuilder, ObjValue,
-			};
-
-			#typed
-
-			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {
-				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {
-					#(#fields_serialize)*
-
-					Ok(())
-				}
-				fn parse(obj: &ObjValue) -> JrResult<Self> {
-					Ok(Self {
-						#(#fields_parse)*
-					})
-				}
-			}
-		};
-	})
 }
 
 struct FormatInput {
addedcrates/jrsonnet-macros/src/names.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-macros/src/names.rs
@@ -0,0 +1,32 @@
+use proc_macro2::TokenStream;
+use quote::quote;
+use std::cell::RefCell;
+
+#[derive(Default)]
+pub struct Names {
+	names: Vec<String>,
+}
+
+impl Names {
+	pub fn intern(&mut self, s: impl AsRef<str>) -> usize {
+		let s = s.as_ref();
+		if let Some(pos) = self.names.iter().position(|v| v == s) {
+			return pos;
+		}
+		let pos = self.names.len();
+		self.names.push(s.to_owned());
+		pos
+	}
+
+	pub fn expand(&self) -> TokenStream {
+		let len = self.names.len();
+		let name = self.names.iter();
+		quote! {
+			thread_local! {
+				static NAMES: [::jrsonnet_evaluator::IStr; #len] = [
+					#(::jrsonnet_evaluator::IStr::from(#name),)*
+				];
+			}
+		}
+	}
+}
addedcrates/jrsonnet-macros/src/typed.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-macros/src/typed.rs
@@ -0,0 +1,373 @@
+use crate::names::Names;
+use crate::{extract_type_from_option, kw, parse_attr, type_is_path};
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::parse::{Parse, ParseStream};
+use syn::spanned::Spanned as _;
+use syn::{parenthesized, token, DeriveInput, Error, Ident, LitStr, Result, Token, Type};
+
+#[derive(Default)]
+#[allow(clippy::struct_excessive_bools)]
+struct TypedAttr {
+	rename: Option<String>,
+	aliases: Vec<String>,
+	flatten: bool,
+	/// flatten(ok) strategy for flattened optionals
+	/// field would be None in case of any parsing error (as in serde)
+	flatten_ok: bool,
+	// Should it be `field+:` instead of `field:`
+	add: bool,
+	// Should it be `field::` instead of `field:`
+	hide: bool,
+}
+impl Parse for TypedAttr {
+	fn parse(input: ParseStream) -> syn::Result<Self> {
+		let mut out = Self::default();
+		loop {
+			let lookahead = input.lookahead1();
+			if lookahead.peek(kw::rename) {
+				input.parse::<kw::rename>()?;
+				input.parse::<Token![=]>()?;
+				let name = input.parse::<LitStr>()?;
+				if out.rename.is_some() {
+					return Err(Error::new(
+						name.span(),
+						"rename attribute may only be specified once",
+					));
+				}
+				out.rename = Some(name.value());
+			} else if lookahead.peek(kw::alias) {
+				input.parse::<kw::alias>()?;
+				input.parse::<Token![=]>()?;
+				let alias = input.parse::<LitStr>()?;
+				out.aliases.push(alias.value());
+			} else if lookahead.peek(kw::flatten) {
+				input.parse::<kw::flatten>()?;
+				out.flatten = true;
+				if input.peek(token::Paren) {
+					let content;
+					parenthesized!(content in input);
+					let lookahead = content.lookahead1();
+					if lookahead.peek(kw::ok) {
+						content.parse::<kw::ok>()?;
+						out.flatten_ok = true;
+					} else {
+						return Err(lookahead.error());
+					}
+				}
+			} else if lookahead.peek(kw::add) {
+				input.parse::<kw::add>()?;
+				out.add = true;
+			} else if lookahead.peek(kw::hide) {
+				input.parse::<kw::hide>()?;
+				out.hide = true;
+			} else if input.is_empty() {
+				break;
+			} else {
+				return Err(lookahead.error());
+			}
+			if input.peek(Token![,]) {
+				input.parse::<Token![,]>()?;
+			} else {
+				break;
+			}
+		}
+		Ok(out)
+	}
+}
+struct TypedField {
+	attr: TypedAttr,
+	ident: Ident,
+	ty: Type,
+	is_option: bool,
+	is_lazy: bool,
+}
+impl TypedField {
+	fn parse(field: &syn::Field) -> Result<Self> {
+		let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();
+		let Some(ident) = field.ident.clone() else {
+			return Err(Error::new(
+				field.span(),
+				"this field should appear in output object, but it has no visible name",
+			));
+		};
+		let (is_option, ty) = extract_type_from_option(&field.ty)?
+			.map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));
+		if is_option && attr.flatten {
+			if !attr.flatten_ok {
+				return Err(Error::new(
+					field.span(),
+					"strategy should be set when flattening Option",
+				));
+			}
+		} else if attr.flatten_ok {
+			return Err(Error::new(
+				field.span(),
+				"flatten(ok) is only useable on optional fields",
+			));
+		}
+
+		let is_lazy = type_is_path(&ty, "Thunk").is_some();
+
+		Ok(Self {
+			attr,
+			ident,
+			ty,
+			is_option,
+			is_lazy,
+		})
+	}
+	/// None if this field is flattened in jsonnet output
+	fn name(&self) -> Option<String> {
+		if self.attr.flatten {
+			return None;
+		}
+		Some(
+			self.attr
+				.rename
+				.clone()
+				.unwrap_or_else(|| self.ident.to_string()),
+		)
+	}
+
+	fn expand_field(&self) -> Option<TokenStream> {
+		if self.is_option {
+			return None;
+		}
+		let name = self.name()?;
+		let ty = &self.ty;
+		Some(quote! {
+			(#name, <#ty as Typed>::TYPE)
+		})
+	}
+
+	fn expand_parse(&self, names: &mut Names) -> TokenStream {
+		if self.is_option {
+			self.expand_parse_optional(names)
+		} else {
+			self.expand_parse_mandatory(names)
+		}
+	}
+
+	fn expand_parse_optional(&self, names: &mut Names) -> TokenStream {
+		let ident = &self.ident;
+		let ty = &self.ty;
+
+		// optional flatten is handled in same way as serde
+		if self.attr.flatten {
+			return quote! {
+				#ident: <#ty as TypedObj>::parse(&obj).ok(),
+			};
+		}
+
+		let name = names.intern(self.name().unwrap());
+		let aliases = self
+			.attr
+			.aliases
+			.iter()
+			.map(|name| names.intern(name))
+			.collect::<Vec<_>>();
+
+		quote! {
+			#ident: {
+				let __value = if let Some(__v) = obj.get(__names[#name].clone())? {
+					Some(__v)
+				} #(else if let Some(__v) = obj.get(__names[#aliases].clone())? {
+					Some(__v)
+				})* else {
+					None
+				};
+
+				__value.map(<#ty as Typed>::from_untyped).transpose()?
+			},
+		}
+	}
+
+	fn expand_parse_mandatory(&self, names: &mut Names) -> TokenStream {
+		let ident = &self.ident;
+		let ty = &self.ty;
+
+		// optional flatten is handled in same way as serde
+		if self.attr.flatten {
+			return quote! {
+				#ident: <#ty as TypedObj>::parse(&obj)?,
+			};
+		}
+
+		let name = self.name().unwrap();
+		let aliases = &self.attr.aliases;
+
+		let error_text = if aliases.is_empty() {
+			// clippy does not understand name variable usage in quote! macro
+			#[allow(clippy::redundant_clone)]
+			name.clone()
+		} else {
+			format!("{name} (alias {})", aliases.join(", "))
+		};
+
+		let error_text = names.intern(error_text);
+		let name = names.intern(name);
+		let aliases = aliases.iter().map(|alias| names.intern(alias));
+
+		quote! {
+			#ident: {
+				let __value = if let Some(__v) = obj.get(__names[#name].clone())? {
+					__v
+				} #(else if let Some(__v) = obj.get(__names[#aliases].clone())? {
+					__v
+				})* else {
+					return Err(ErrorKind::NoSuchField(__names[#error_text].clone(), vec![]).into());
+				};
+
+				<#ty as Typed>::from_untyped(__value)?
+			},
+		}
+	}
+
+	fn expand_serialize(&self, names: &mut Names) -> TokenStream {
+		let ident = &self.ident;
+		let ty = &self.ty;
+		self.name().map_or_else(
+			|| {
+				if self.is_option {
+					quote! {
+						if let Some(value) = self.#ident {
+							<#ty as TypedObj>::serialize(value, out)?;
+						}
+					}
+				} else {
+					quote! {
+						<#ty as TypedObj>::serialize(self.#ident, out)?;
+					}
+				}
+			},
+			|name| {
+				let name = names.intern(name);
+				let hide = if self.attr.hide {
+					quote! {.hide()}
+				} else {
+					quote! {}
+				};
+				let add = if self.attr.add {
+					quote! {.add()}
+				} else {
+					quote! {}
+				};
+				let value = if self.is_lazy {
+					quote! {
+						out.field(__names[#name].clone())
+							#hide
+							#add
+							.try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
+					}
+				} else {
+					quote! {
+						out.field(__names[#name].clone())
+							#hide
+							#add
+							.try_value(<#ty as Typed>::into_untyped(value)?)?;
+					}
+				};
+				if self.is_option {
+					quote! {
+						if let Some(value) = self.#ident {
+							#value
+						}
+					}
+				} else {
+					quote! {
+						{
+							let value = self.#ident;
+							#value
+						}
+					}
+				}
+			},
+		)
+	}
+}
+
+pub fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
+	let syn::Data::Struct(data) = &input.data else {
+		return Err(Error::new(input.span(), "only structs supported"));
+	};
+
+	let ident = &input.ident;
+	let fields = data
+		.fields
+		.iter()
+		.map(TypedField::parse)
+		.collect::<Result<Vec<_>>>()?;
+
+	let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
+
+	let capacity = fields.len();
+
+	let typed = {
+		let fields = fields
+			.iter()
+			.filter_map(TypedField::expand_field)
+			.collect::<Vec<_>>();
+		quote! {
+			impl #impl_generics Typed for #ident #ty_generics #where_clause {
+				const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[
+					#(#fields,)*
+				]);
+
+				fn from_untyped(value: Val) -> JrResult<Self> {
+					let obj = value.as_obj().expect("shape is correct");
+					Self::parse(&obj)
+				}
+
+				fn into_untyped(value: Self) -> JrResult<Val> {
+					let mut out = ObjValueBuilder::with_capacity(#capacity);
+					value.serialize(&mut out)?;
+					Ok(Val::Obj(out.build()))
+				}
+
+			}
+		}
+	};
+
+	let mut names = Names::default();
+
+	let fields_parse = fields
+		.iter()
+		.map(|f| f.expand_parse(&mut names))
+		.collect::<Vec<_>>();
+	let fields_serialize = fields
+		.iter()
+		.map(|f| f.expand_serialize(&mut names))
+		.collect::<Vec<_>>();
+
+	let names_expanded = names.expand();
+	Ok(quote! {
+		const _: () = {
+			use ::jrsonnet_evaluator::{
+				typed::{ComplexValType, Typed, TypedObj, CheckType},
+				Val, State,
+				error::{ErrorKind, Result as JrResult},
+				ObjValueBuilder, ObjValue, IStr,
+			};
+
+			#typed
+
+			#names_expanded
+
+			impl #impl_generics TypedObj for #ident #ty_generics #where_clause {
+				fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {
+					NAMES.with(|__names| {
+						#(#fields_serialize)*
+
+						Ok(())
+					})
+				}
+				fn parse(obj: &ObjValue) -> JrResult<Self> {
+					NAMES.with(|__names| Ok(Self {
+						#(#fields_parse)*
+					}))
+				}
+			}
+		};
+	})
+}
modifiedcrates/jrsonnet-stdlib/src/regex.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/regex.rs
+++ b/crates/jrsonnet-stdlib/src/regex.rs
@@ -4,8 +4,9 @@
 use jrsonnet_evaluator::{
 	error::{ErrorKind::*, Result},
 	rustc_hash::FxBuildHasher,
+	typed::Typed,
 	val::StrValue,
-	IStr, ObjValueBuilder, Val,
+	IStr, ObjValue, ObjValueBuilder,
 };
 use jrsonnet_gcmodule::Acyclic;
 use jrsonnet_macros::builtin;
@@ -20,7 +21,7 @@
 		Self {
 			cache: RefCell::new(LruCache::with_hasher(
 				NonZeroUsize::new(20).unwrap(),
-				FxBuildHasher::default(),
+				FxBuildHasher,
 			)),
 		}
 	}
@@ -40,21 +41,27 @@
 	}
 }
 
-pub fn regex_match_inner(regex: &Regex, str: String) -> Result<Val> {
-	let mut out = ObjValueBuilder::with_capacity(3);
+#[derive(Typed)]
+pub struct RegexMatch {
+	string: IStr,
+	captures: Vec<IStr>,
+	#[typed(rename = "namedCaptures")]
+	named_captures: ObjValue,
+}
 
+fn regex_match_inner(regex: &Regex, str: String) -> Result<Option<RegexMatch>> {
 	let mut captures = Vec::with_capacity(regex.captures_len());
 	let mut named_captures = ObjValueBuilder::with_capacity(regex.capture_names().len());
 
 	let Some(captured) = regex.captures(&str) else {
-		return Ok(Val::Null);
+		return Ok(None);
 	};
 
 	for ele in captured.iter().skip(1) {
 		if let Some(ele) = ele {
-			captures.push(Val::Str(StrValue::Flat(ele.as_str().into())));
+			captures.push(ele.as_str().into());
 		} else {
-			captures.push(Val::Str(StrValue::Flat(IStr::empty())));
+			captures.push(IStr::empty());
 		}
 	}
 	for (i, name) in regex
@@ -67,13 +74,11 @@
 		named_captures.field(name).try_value(capture)?;
 	}
 
-	out.field("string")
-		.value(Val::Str(captured.get(0).unwrap().as_str().into()));
-	out.field("captures").value(Val::Arr(captures.into()));
-	out.field("namedCaptures")
-		.value(Val::Obj(named_captures.build()));
-
-	Ok(Val::Obj(out.build()))
+	Ok(Some(RegexMatch {
+		string: captured.get(0).expect("regex matched").as_str().into(),
+		named_captures: named_captures.build(),
+		captures,
+	}))
 }
 
 #[builtin(fields(
@@ -83,7 +88,7 @@
 	this: &builtin_regex_partial_match,
 	pattern: IStr,
 	str: String,
-) -> Result<Val> {
+) -> Result<Option<RegexMatch>> {
 	let regex = this.cache.parse(pattern)?;
 	regex_match_inner(&regex, str)
 }
@@ -95,7 +100,7 @@
 	this: &builtin_regex_full_match,
 	pattern: StrValue,
 	str: String,
-) -> Result<Val> {
+) -> Result<Option<RegexMatch>> {
 	let pattern = format!("^{pattern}$").into();
 	let regex = this.cache.parse(pattern)?;
 	regex_match_inner(&regex, str)