Skip to content

Commit b0f2220

Browse files
authored
Merge pull request #411 from 01mf02/termid-ref-exn
Store `TermId` by reference in `Exn`.
2 parents 1ea6180 + bf6a87a commit b0f2220

File tree

9 files changed

+52
-48
lines changed

9 files changed

+52
-48
lines changed

jaq-core/src/exn.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Exceptions and errors.
22
3-
use crate::RcList;
3+
use crate::{compile::TermId, filter::Vars, RcList};
44
use alloc::{boxed::Box, string::String, string::ToString, vec::Vec};
55
use core::fmt::{self, Display};
66

@@ -11,16 +11,16 @@ use core::fmt::{self, Display};
1111
///
1212
/// Use [`crate::val::unwrap_valr`] to convert a [`crate::val::ValX`] to an error.
1313
#[derive(Clone, Debug)]
14-
pub struct Exn<V>(pub(crate) Inner<V>);
14+
pub struct Exn<'a, V>(pub(crate) Inner<'a, V>);
1515

1616
#[derive(Clone, Debug)]
17-
pub(crate) enum Inner<V> {
17+
pub(crate) enum Inner<'a, V> {
1818
Err(Box<Error<V>>),
1919
/// Tail-recursive call.
2020
///
2121
/// This is used internally to execute tail-recursive filters.
2222
/// If this can be observed by users, then this is a bug.
23-
TailCall(Box<(crate::compile::TermId, crate::filter::Vars<V>, CallInput<V>)>),
23+
TailCall(Box<(&'a TermId, Vars<V>, CallInput<V>)>),
2424
Break(usize),
2525
}
2626

@@ -46,7 +46,7 @@ impl<V> CallInput<V> {
4646
}
4747
}
4848

49-
impl<V> Exn<V> {
49+
impl<V> Exn<'_, V> {
5050
/// If the exception is an error, yield it, else yield the exception.
5151
pub(crate) fn get_err(self) -> Result<Error<V>, Self> {
5252
match self.0 {
@@ -56,7 +56,7 @@ impl<V> Exn<V> {
5656
}
5757
}
5858

59-
impl<V> From<Error<V>> for Exn<V> {
59+
impl<V> From<Error<V>> for Exn<'_, V> {
6060
fn from(e: Error<V>) -> Self {
6161
Exn(Inner::Err(Box::new(e)))
6262
}

jaq-core/src/filter.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ fn bind_run<'a, D: DataT, T: Clone + 'a>(
229229
run: IdRunFn<'a, D, T>,
230230
) -> ValXs<'a, T, D::V<'a>> {
231231
match pat {
232-
Pattern::Var => run(*r, (cv.0.cons_var(y), cv.1)),
232+
Pattern::Var => run(r, (cv.0.cons_var(y), cv.1)),
233233
Pattern::Idx(pats) => {
234-
let r = move |ctx, vp| run(*r, (ctx, vp));
234+
let r = move |ctx, vp| run(r, (ctx, vp));
235235
flat_map_then_with(bind_pats(pats, cv.0.clone(), (cv.0, y)), cv.1, r)
236236
}
237237
}
@@ -249,7 +249,7 @@ fn label_run<'a, D: DataT, T: 'a>(
249249
}))
250250
}
251251

252-
fn try_catch_run<'a, T: 'a, V: 'a, I: Iterator<Item = ValX<T, V>> + 'a>(
252+
fn try_catch_run<'a, T: 'a, V: 'a, I: Iterator<Item = ValX<'a, T, V>> + 'a>(
253253
mut ys: ValXs<'a, T, V>,
254254
f: impl Fn(Error<V>) -> I + 'a,
255255
) -> ValXs<'a, T, V> {
@@ -267,23 +267,23 @@ fn try_catch_run<'a, T: 'a, V: 'a, I: Iterator<Item = ValX<T, V>> + 'a>(
267267
}
268268

269269
fn fold_run<'a, D: DataT, T: Clone + 'a>(
270-
xs: impl Iterator<Item = ValX<Ctx<'a, D>, D::V<'a>>> + Clone + 'a,
270+
xs: impl Iterator<Item = ValX<'a, Ctx<'a, D>, D::V<'a>>> + Clone + 'a,
271271
cv: Cv<'a, D, T>,
272272
init: &'a Id,
273273
update: &'a Id,
274274
fold_type: &'a Fold<Id>,
275275
run: IdRunFn<'a, D, T>,
276276
) -> ValXs<'a, T, D::V<'a>> {
277-
let init = run(*init, cv.clone());
278-
let update = move |ctx, v| run(*update, (ctx, v));
277+
let init = run(init, cv.clone());
278+
let update = move |ctx, v| run(update, (ctx, v));
279279
let inner = |_, y: &T| Some(y.clone());
280280
let inner_proj = |ctx, y: &T| Some((ctx, y.clone()));
281281
flat_map_then_with(init, xs, move |i, xs| match fold_type {
282282
Fold::Reduce => Box::new(fold(xs, i, update, |_| (), |_, _| None, Some)),
283283
Fold::Foreach(None) => Box::new(fold(xs, i, update, |_| (), inner, |_| None)),
284284
Fold::Foreach(Some(proj)) => flat_map_then(
285285
fold(xs, i, update, |ctx| ctx.clone(), inner_proj, |_| None),
286-
move |(ctx, y)| run(*proj, (ctx, y)),
286+
move |(ctx, y)| run(proj, (ctx, y)),
287287
),
288288
})
289289
}
@@ -302,7 +302,7 @@ where
302302
type Pairs<'a, T> = box_iter::BoxIter<'a, (T, T)>;
303303

304304
/// Run `self` and `r` and return the cartesian product of their outputs.
305-
fn cartesian<'a, D: DataT>(l: &'a Id, r: &'a Id, cv: Cv<'a, D>) -> Pairs<'a, ValX<D::V<'a>>> {
305+
fn cartesian<'a, D: DataT>(l: &'a Id, r: &'a Id, cv: Cv<'a, D>) -> Pairs<'a, ValX<'a, D::V<'a>>> {
306306
flat_map_with(l.run(cv.clone()), cv, move |l, cv| {
307307
map_with(r.run(cv), l, |r, l| (l, r))
308308
})
@@ -312,7 +312,7 @@ fn fold_update<'a, D: DataT>(
312312
fold_type: &'a Fold<Id>,
313313
path: &'a Id,
314314
v: D::V<'a>,
315-
mut xs: impl Iterator<Item = ValX<Ctx<'a, D>, D::V<'a>>> + Clone + 'a,
315+
mut xs: impl Iterator<Item = ValX<'a, Ctx<'a, D>, D::V<'a>>> + Clone + 'a,
316316
f: BoxUpdate<'a, D::V<'a>>,
317317
) -> ValXs<'a, D::V<'a>> {
318318
let ctx = match xs.next() {
@@ -351,10 +351,10 @@ fn def_run<'a, D: DataT, T: 'a>(
351351
from: fn(exn::CallInput<D::V<'a>>) -> T,
352352
) -> ValXs<'a, T, D::V<'a>> {
353353
use core::ops::ControlFlow;
354-
let outs = |cvs| flat_map_then(cvs, move |cv| run(*id, cv));
354+
let outs = |cvs| flat_map_then(cvs, move |cv| run(id, cv));
355355
let catch = |all: bool| {
356356
move |r| match r {
357-
Err(Exn(exn::Inner::TailCall(tc))) if all || tc.0 == *id => {
357+
Err(Exn(exn::Inner::TailCall(tc))) if all || tc.0 == id => {
358358
ControlFlow::Continue(run(tc.0, (with_vars(tc.1), from(tc.2))))
359359
}
360360
Ok(_) | Err(_) => ControlFlow::Break(r),
@@ -367,7 +367,7 @@ fn def_run<'a, D: DataT, T: 'a>(
367367
CallType::CatchAll => Box::new(crate::Stack::new([outs(cvs)].into(), catch(true))),
368368
CallType::Throw => Box::new(cvs.map(move |cv| {
369369
cv.and_then(|cv| {
370-
let tc = (*id, cv.0.vars, into(cv.1));
370+
let tc = (id, cv.0.vars, into(cv.1));
371371
Err(Exn(exn::Inner::TailCall(Box::new(tc))))
372372
})
373373
})),
@@ -423,7 +423,7 @@ pub struct Native<D: DataT + ?Sized> {
423423
update: UpdatePtr<D>,
424424
}
425425

426-
type IdRunFn<'a, D, T> = fn(Id, Cv<'a, D, T>) -> ValXs<'a, T, <D as DataT>::V<'a>>;
426+
type IdRunFn<'a, D, T> = fn(&Id, Cv<'a, D, T>) -> ValXs<'a, T, <D as DataT>::V<'a>>;
427427

428428
/// Run function pointer (see [`Id::run`]).
429429
pub type RunPtr<D> = for<'a> fn(Cv<'a, D>) -> ValXs<'a, <D as DataT>::V<'a>>;
@@ -461,7 +461,7 @@ impl<D: DataT> Native<D> {
461461

462462
impl Id {
463463
/// `f.run((c, v))` returns the output of `v | f` in the context `c`.
464-
pub fn run<'a, D: DataT>(self, cv: Cv<'a, D>) -> ValXs<'a, D::V<'a>> {
464+
pub fn run<'a, D: DataT>(&self, cv: Cv<'a, D>) -> ValXs<'a, D::V<'a>> {
465465
use core::iter::once;
466466
match &cv.0.lut().terms[self.0] {
467467
Ast::Id => box_once(Ok(cv.1)),
@@ -575,7 +575,7 @@ impl Id {
575575
///
576576
/// In particular, `v | path(f)` in context `c` yields the same paths as
577577
/// `f.paths((c, (v, Default::default())))`.
578-
pub fn paths<'a, D: DataT>(self, cv: Cvp<'a, D>) -> ValPathXs<'a, D::V<'a>> {
578+
pub fn paths<'a, D: DataT>(&self, cv: Cvp<'a, D>) -> ValPathXs<'a, D::V<'a>> {
579579
let err = |v| box_once(Err(Exn::from(Error::path_expr(v))));
580580
let proj_cv = |cv: &Cvp<'a, D>| (cv.0.clone(), cv.1 .0.clone());
581581
let proj_val = |(val, _path): &(D::V<'a>, _)| val.clone();
@@ -658,7 +658,7 @@ impl Id {
658658

659659
/// `p.update((c, v), f)` returns the output of `v | p |= f` in the context `c`.
660660
pub fn update<'a, D: DataT>(
661-
self,
661+
&self,
662662
cv: Cv<'a, D>,
663663
f: BoxUpdate<'a, D::V<'a>>,
664664
) -> ValXs<'a, D::V<'a>> {

jaq-core/src/path.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a, V: ValT + 'a> Path<V> {
8383
run(self.0.into_iter(), vp, |part, vp| part.paths(vp))
8484
}
8585

86-
pub(crate) fn update<F>(mut self, v: V, f: F) -> ValX<V>
86+
pub(crate) fn update<F>(mut self, v: V, f: F) -> ValX<'a, V>
8787
where
8888
F: Fn(V) -> ValXs<'a, V>,
8989
{
@@ -110,7 +110,7 @@ fn run<'a, V: 'a, T: 'a>(
110110
}
111111
}
112112

113-
fn update<'a, V: ValT, P, F>(mut iter: P, last: (Part<V>, Opt), v: V, f: &F) -> ValX<V>
113+
fn update<'a, V: ValT + 'a, P, F>(mut iter: P, last: (Part<V>, Opt), v: V, f: &F) -> ValX<'a, V>
114114
where
115115
P: Iterator<Item = (Part<V>, Opt)> + Clone,
116116
F: Fn(V) -> ValXs<'a, V>,
@@ -146,10 +146,10 @@ impl<'a, V: ValT + 'a> Part<V> {
146146
}
147147
}
148148

149-
fn update<F, I>(&self, v: V, opt: Opt, f: F) -> ValX<V>
149+
fn update<F, I>(&self, v: V, opt: Opt, f: F) -> ValX<'a, V>
150150
where
151151
F: Fn(V) -> I,
152-
I: Iterator<Item = ValX<V>>,
152+
I: Iterator<Item = ValX<'a, V>>,
153153
{
154154
match self {
155155
Self::Index(idx) => v.map_index(idx, opt, f),

jaq-core/src/val.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pub type ValRs<'a, T, V = T> = BoxIter<'a, ValR<T, V>>;
1919
/// Value or eXception.
2020
///
2121
/// Use [`unwrap_valr`] to convert to [`ValR`].
22-
pub type ValX<T, V = T> = Result<T, crate::Exn<V>>;
22+
pub type ValX<'a, T, V = T> = Result<T, crate::Exn<'a, V>>;
2323
/// Stream of values and eXceptions.
24-
pub type ValXs<'a, T, V = T> = BoxIter<'a, ValX<T, V>>;
24+
pub type ValXs<'a, T, V = T> = BoxIter<'a, ValX<'a, T, V>>;
2525

2626
/// Convert a value exception [`ValX`] into a value result [`ValR`].
2727
///
@@ -101,35 +101,35 @@ pub trait ValT:
101101
///
102102
/// - If `opt` is [`Opt::Essential`], return an error.
103103
/// - If `opt` is [`Opt::Optional`] , return the input value.
104-
fn map_values<I: Iterator<Item = ValX<Self>>>(
104+
fn map_values<'a, I: Iterator<Item = ValX<'a, Self>>>(
105105
self,
106106
opt: Opt,
107107
f: impl Fn(Self) -> I,
108-
) -> ValX<Self>;
108+
) -> ValX<'a, Self>;
109109

110110
/// Map a function over the child of the value at the given index.
111111
///
112112
/// This is used by `.[k] |= f`.
113113
///
114114
/// See [`Self::map_values`] for the behaviour of `opt`.
115-
fn map_index<I: Iterator<Item = ValX<Self>>>(
115+
fn map_index<'a, I: Iterator<Item = ValX<'a, Self>>>(
116116
self,
117117
index: &Self,
118118
opt: Opt,
119119
f: impl Fn(Self) -> I,
120-
) -> ValX<Self>;
120+
) -> ValX<'a, Self>;
121121

122122
/// Map a function over the slice of the value with the given range.
123123
///
124124
/// This is used by `.[s:e] |= f`, `.[s:] |= f`, and `.[:e] |= f`.
125125
///
126126
/// See [`Self::map_values`] for the behaviour of `opt`.
127-
fn map_range<I: Iterator<Item = ValX<Self>>>(
127+
fn map_range<'a, I: Iterator<Item = ValX<'a, Self>>>(
128128
self,
129129
range: Range<&Self>,
130130
opt: Opt,
131131
f: impl Fn(Self) -> I,
132-
) -> ValX<Self>;
132+
) -> ValX<'a, Self>;
133133

134134
/// Return a boolean representation of the value.
135135
///

jaq-fmts/src/read/funs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn funs<D: for<'a> DataT<V<'a> = Val>>() -> Box<[Filter<RunPtr<D>>]> {
3838
}
3939

4040
/// Box Map, Map Error.
41-
fn bmme<'a>(iter: BoxIter<'a, ValR>) -> BoxIter<'a, ValX> {
41+
fn bmme<'a>(iter: BoxIter<'a, ValR>) -> BoxIter<'a, ValX<'a>> {
4242
Box::new(iter.map(|r| r.map_err(Exn::from)))
4343
}
4444

jaq-json/src/funs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Val {
114114
}
115115

116116
/// Box Map, Map Error.
117-
fn bmme<'a>(iter: BoxIter<'a, ValR>) -> BoxIter<'a, ValX> {
117+
fn bmme<'a>(iter: BoxIter<'a, ValR>) -> BoxIter<'a, ValX<'a>> {
118118
Box::new(iter.map(|r| r.map_err(Exn::from)))
119119
}
120120

jaq-json/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub type Error = jaq_core::Error<Val>;
121121
/// A value or an eRror.
122122
pub type ValR = jaq_core::ValR<Val>;
123123
/// A value or an eXception.
124-
pub type ValX = jaq_core::ValX<Val>;
124+
pub type ValX<'a> = jaq_core::ValX<'a, Val>;
125125

126126
// This is part of the Rust standard library since 1.76:
127127
// <https://doc.rust-lang.org/std/rc/struct.Rc.html#method.unwrap_or_clone>.
@@ -176,7 +176,11 @@ impl jaq_core::ValT for Val {
176176
}
177177
}
178178

179-
fn map_values<I: Iterator<Item = ValX>>(self, opt: path::Opt, f: impl Fn(Self) -> I) -> ValX {
179+
fn map_values<'a, I: Iterator<Item = ValX<'a>>>(
180+
self,
181+
opt: path::Opt,
182+
f: impl Fn(Self) -> I,
183+
) -> ValX<'a> {
180184
match self {
181185
Self::Arr(a) => {
182186
let iter = rc_unwrap_or_clone(a).into_iter().flat_map(f);
@@ -191,12 +195,12 @@ impl jaq_core::ValT for Val {
191195
}
192196
}
193197

194-
fn map_index<I: Iterator<Item = ValX>>(
198+
fn map_index<'a, I: Iterator<Item = ValX<'a>>>(
195199
mut self,
196200
index: &Self,
197201
opt: path::Opt,
198202
f: impl Fn(Self) -> I,
199-
) -> ValX {
203+
) -> ValX<'a> {
200204
if let (Val::BStr(_) | Val::TStr(_) | Val::Arr(_), Val::Obj(o)) = (&self, index) {
201205
let range = o.get(&Val::utf8_str("start"))..o.get(&Val::utf8_str("end"));
202206
return self.map_range(range, opt, f);
@@ -243,12 +247,12 @@ impl jaq_core::ValT for Val {
243247
}
244248
}
245249

246-
fn map_range<I: Iterator<Item = ValX>>(
250+
fn map_range<'a, I: Iterator<Item = ValX<'a>>>(
247251
mut self,
248252
range: val::Range<&Self>,
249253
opt: path::Opt,
250254
f: impl Fn(Self) -> I,
251-
) -> ValX {
255+
) -> ValX<'a> {
252256
let fs = |b: Bytes, range, skip_take: SkipTakeFn, from: ValBytesFn, into: BytesValFn| {
253257
let (skip, take) = match Self::range_int(range) {
254258
Ok(range) => skip_take(range, &b),

jaq-std/src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ where
4646
])
4747
}
4848

49-
fn inputs<'a, D: DataT>(cv: Cv<'a, D>) -> impl Iterator<Item = ValX<D::V<'a>>> + 'a
49+
fn inputs<'a, D: DataT>(cv: Cv<'a, D>) -> impl Iterator<Item = ValX<'a, D::V<'a>>> + 'a
5050
where
5151
D::Data<'a>: HasInputs<'a, D::V<'a>>,
5252
{

jaq-std/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ trait ValTx: ValT + Sized {
173173
}
174174

175175
/// Apply a function to an array.
176-
fn try_mutate_arr<F>(self, f: F) -> ValX<Self>
176+
fn try_mutate_arr<'a, F>(self, f: F) -> ValX<'a, Self>
177177
where
178-
F: FnOnce(&mut Vec<Self>) -> Result<(), Exn<Self>>,
178+
F: FnOnce(&mut Vec<Self>) -> Result<(), Exn<'a, Self>>,
179179
{
180180
let mut a = self.into_vec()?;
181181
f(&mut a)?;
@@ -237,7 +237,7 @@ trait ValTx: ValT + Sized {
237237
impl<T: ValT> ValTx for T {}
238238

239239
/// Sort array by the given function.
240-
fn sort_by<'a, V: ValT>(xs: &mut [V], f: impl Fn(V) -> ValXs<'a, V>) -> Result<(), Exn<V>> {
240+
fn sort_by<'a, V: ValT>(xs: &mut [V], f: impl Fn(V) -> ValXs<'a, V>) -> Result<(), Exn<'a, V>> {
241241
// Some(e) iff an error has previously occurred
242242
let mut err = None;
243243
xs.sort_by_cached_key(|x| {
@@ -256,7 +256,7 @@ fn sort_by<'a, V: ValT>(xs: &mut [V], f: impl Fn(V) -> ValXs<'a, V>) -> Result<(
256256
}
257257

258258
/// Group an array by the given function.
259-
fn group_by<'a, V: ValT>(xs: Vec<V>, f: impl Fn(V) -> ValXs<'a, V>) -> ValX<V> {
259+
fn group_by<'a, V: ValT>(xs: Vec<V>, f: impl Fn(V) -> ValXs<'a, V>) -> ValX<'a, V> {
260260
let mut yx: Vec<(Vec<V>, V)> = xs
261261
.into_iter()
262262
.map(|x| Ok((f(x.clone()).collect::<Result<_, _>>()?, x)))
@@ -284,7 +284,7 @@ fn group_by<'a, V: ValT>(xs: Vec<V>, f: impl Fn(V) -> ValXs<'a, V>) -> ValX<V> {
284284
}
285285

286286
/// Get the minimum or maximum element from an array according to the given function.
287-
fn cmp_by<'a, V: Clone, F, R>(xs: Vec<V>, f: F, replace: R) -> Result<Option<V>, Exn<V>>
287+
fn cmp_by<'a, V: Clone, F, R>(xs: Vec<V>, f: F, replace: R) -> Result<Option<V>, Exn<'a, V>>
288288
where
289289
F: Fn(V) -> ValXs<'a, V>,
290290
R: Fn(&[V], &[V]) -> bool,

0 commit comments

Comments
 (0)