Skip to main content

jaq_core/
data.rs

1//! Mapping from compile-time data types to run-time data types.
2
3use crate::{Lut, ValT};
4
5/// Data types that filters operate on.
6///
7/// Types that implement this trait allow to specify which data types a filter processes.
8/// In particular, this allows native filters to operate on data types that
9/// *may live only as long as the filter is executed*.
10/// For example, this is required by the `inputs` filter,
11/// whose global data --- namely the inputs to the main filter --- has a lifetime.
12///
13/// ## Motivation
14///
15/// If we have data types that are tied to a particular lifetime and
16/// we would bake those data types into a native filter type,
17/// then native filters could *only be used for data of this particular lifetime*.
18/// For example, suppose that a filter operating on a value type `&'a str`
19/// would have the type `Filter<&'a str>`.
20/// The crucial point is that here, the `'a` is fixed,
21/// so we could only run the filter with `&'a str` for one particular `'a`.
22/// That would mean that if we want to execute the same filter with
23/// data having different `'a` lifetimes
24/// (e.g. for strings coming from different files),
25/// we would need to recompile the filter every time (i.e. for every file).
26///
27/// This trait allows us to avoid this problem by separating the actual data from
28/// the *knowledge* that a filter will operate on certain type of data.
29/// That allows us to run a filter using data types with a lifetime that
30/// depend on the lifetime of the filter execution.
31pub trait DataT: 'static {
32    /// Type of values that filters process.
33    type V<'a>: ValT;
34    /// Global data accessible to native filters.
35    type Data<'a>: Clone + HasLut<'a, Self>;
36}
37
38/// Types that provide an `'a`-lived LUT for the data types given in `D`.
39pub trait HasLut<'a, D: DataT + ?Sized> {
40    /// Return the LUT.
41    fn lut(&self) -> &'a Lut<D>;
42}
43
44/// Filters that process `V` and take only LUT as data.
45///
46/// This restricts `V` to have a `'static` lifetime.
47/// If you want to process `V` with arbitrary lifetimes instead,
48/// you need to define your own data kind and implement [`DataT`] for it.
49///
50/// This type is mostly used for testing.
51pub struct JustLut<V>(core::marker::PhantomData<V>);
52
53impl<V: ValT + 'static> DataT for JustLut<V> {
54    type V<'a> = V;
55    type Data<'a> = &'a Lut<Self>;
56}
57
58impl<'a, D: DataT> HasLut<'a, D> for &'a Lut<D> {
59    fn lut(&self) -> &'a Lut<D> {
60        self
61    }
62}