tree_buf/internal/
options.rs

1// https://docs.rs/ndarray-zfp-rs/0.1.0/ndarray_zfp_rs/trait.Zfp.htmls
2
3// TODO: This would be the place to specify in-place padded/aligned encoding when desired.
4// I'm not sure that's as useful, since it moves out of where tree-buf competes into different
5// territory (eg: FlatBuffers). Though Flatbuffers by way of example doesn't allow for in-place encoding,
6//
7
8// TODO: Option for encode sample size. This could be a method which actually returns the sample array
9
10macro_rules! options {
11    ($Options:ident, $Default:ident, $Override:ident, $Hierarchy:ident, {$($name:ident: $T:ty = $fallback:expr),*}) => {
12        pub trait $Options: Send + Sync {
13            $(
14                #[inline(always)]
15                fn $name(&self) -> $T { $fallback }
16            )*
17        }
18
19        pub struct $Default;
20        impl $Options for $Default { }
21
22        pub trait $Override: Send + Sync {
23            $(
24                #[inline(always)]
25                fn $name(&self) -> Option<$T> { None }
26            )*
27        }
28
29        impl<T0: $Options, T1: $Override> $Options for $Hierarchy<T0, T1> {
30            $(
31                #[inline(always)]
32                fn $name(&self) -> $T {
33                    self.overrides.$name().unwrap_or_else(|| self.fallback.$name())
34                }
35            )*
36        }
37
38        struct $Hierarchy<T0, T1> {
39            fallback: T0,
40            overrides: T1,
41        }
42    };
43}
44
45// TODO: Option for parallel
46// TODO: Put scratch in options?
47options!(EncodeOptions, EncodeOptionsDefault, EncodeOptionsOverride, EncodeOptionsHierarchy, {
48    lossy_float_tolerance: Option<i32> = None
49});
50
51options!(DecodeOptions, DecodeOptionsDefault, DecodeOptionsOverride, DecodeOptionsHierarchy, {
52    parallel: bool = true
53});
54
55pub struct EnableParallel;
56impl DecodeOptionsOverride for EnableParallel {
57    #[inline(always)]
58    fn parallel(&self) -> Option<bool> {
59        Some(true)
60    }
61}
62
63pub struct DisableParallel;
64impl DecodeOptionsOverride for DisableParallel {
65    #[inline(always)]
66    fn parallel(&self) -> Option<bool> {
67        Some(false)
68    }
69}
70
71pub struct LosslessFloat;
72impl EncodeOptionsOverride for LosslessFloat {
73    #[inline(always)]
74    fn lossy_float_tolerance(&self) -> Option<Option<i32>> {
75        Some(None)
76    }
77}
78
79pub struct LossyFloatTolerance(pub i32);
80impl EncodeOptionsOverride for LossyFloatTolerance {
81    #[inline(always)]
82    fn lossy_float_tolerance(&self) -> Option<Option<i32>> {
83        Some(Some(self.0))
84    }
85}
86
87// TODO: Move the remainder here into the macro
88pub fn override_encode_options(options: impl EncodeOptions, overrides: impl EncodeOptionsOverride) -> impl EncodeOptions {
89    EncodeOptionsHierarchy { fallback: options, overrides }
90}
91
92#[macro_export]
93macro_rules! encode_options {
94    ($($opts:expr),*) => {
95        {
96            let options = $crate::options::EncodeOptionsDefault;
97            $(
98                let options = $crate::options::override_encode_options(options, $opts);
99            )*
100            options
101        }
102    }
103}
104
105pub fn override_decode_options(options: impl DecodeOptions, overrides: impl DecodeOptionsOverride) -> impl DecodeOptions {
106    DecodeOptionsHierarchy { fallback: options, overrides }
107}
108
109#[macro_export]
110macro_rules! decode_options {
111    ($($opts:expr),*) => {
112        {
113            let options = $crate::options::DecodeOptionsDefault;
114            $(
115                let options = $crate::options::override_decode_options(options, $opts);
116            )*
117            options
118        }
119    }
120}