meos 0.3.0

Rust bindings for MEOS C API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::{
    boxes::TBox,
    collections::number::{NumberSpan, NumberSpanSet},
    temporal::temporal::Temporal,
};

pub trait TNumber: Temporal<TBB = TBox> {
    // ------------------------- Accessors -------------------------------------
    /// Returns the bounding box of `self`.
    ///
    /// # Returns
    /// The bounding box of `self`.
    ///
    /// # Safety
    /// This function uses unsafe code to call the `meos_sys::tbox_tnumber` function.
    fn bounding_box(&self) -> TBox {
        unsafe { TBox::from_inner(meos_sys::tnumber_to_tbox(self.inner())) }
    }

    /// Returns the integral of `self`.
    ///
    /// # Returns
    /// The integral of `self`.
    ///
    /// # Safety
    /// This function uses unsafe code to call the `meos_sys::tnumber_integral` function.
    fn integral(&self) -> f64 {
        unsafe { meos_sys::tnumber_integral(self.inner()) }
    }

    /// Returns the time-weighted average of `self`.
    ///
    /// # Returns
    /// The time-weighted average of `self`.
    ///
    /// # Safety
    /// This function uses unsafe code to call the `meos_sys::tnumber_twavg` function.
    fn time_weighted_average(&self) -> f64 {
        unsafe { meos_sys::tnumber_twavg(self.inner()) }
    }

    // ------------------------- Restrictions ----------------------------------
    /// Returns a new temporal object with the values of `self` where it's not in `span`
    ///
    /// ## Arguments
    /// * `span` - A `IntSpan` or `FloatSpan` to substract the values from
    fn minus_span(&self, span: impl NumberSpan) -> Self {
        Self::from_inner_as_temporal(unsafe {
            meos_sys::tnumber_minus_span(self.inner(), span.inner())
        })
    }

    /// Returns a new temporal object with the values of `self` where it's not in `span_set`
    ///
    /// ## Arguments
    /// * `span_set` - A `IntSpanSet` or `FloatSpanSet` to substract the values from
    fn minus_span_set(&self, span_set: impl NumberSpanSet) -> Self {
        Self::from_inner_as_temporal(unsafe {
            meos_sys::tnumber_minus_spanset(self.inner(), span_set.inner())
        })
    }

    // ------------------------- Operations ------------------------------------
    /// Adds the value(s) of `other` to the value(s) of `self`.
    ///
    /// # Arguments
    /// * `other` - A temporal number to add to the value(s) of `self`.
    fn add(&self, other: &Self) -> Self {
        Self::from_inner_as_temporal(unsafe {
            meos_sys::add_tnumber_tnumber(self.inner(), other.inner())
        })
    }

    /// Substract the value(s) of `other` to the value(s) of `self`.
    ///
    /// # Arguments
    /// * `other` - A temporal number to substract to the value(s) of `self`.
    fn substract(&self, other: &Self) -> Self {
        Self::from_inner_as_temporal(unsafe {
            meos_sys::sub_tnumber_tnumber(self.inner(), other.inner())
        })
    }

    /// Multiplicate the value(s) of `other` by the value(s) of `self`.
    ///
    /// # Arguments
    /// * `other` - A temporal number to multiplicate by the value(s) of `self`.
    fn multiplicate(&self, other: &Self) -> Self {
        Self::from_inner_as_temporal(unsafe {
            meos_sys::mult_tnumber_tnumber(self.inner(), other.inner())
        })
    }

    /// Divide the value(s) of `other` by the value(s) of `self`.
    ///
    /// # Arguments
    /// * `other` - A temporal number to divide by the value(s) of `self`.
    fn divide(&self, other: &Self) -> Self {
        Self::from_inner_as_temporal(unsafe {
            meos_sys::div_tnumber_tnumber(self.inner(), other.inner())
        })
    }

    // ------------------------- Aggregations ----------------------------------
    /// Returns the absolute value of `self`.
    ///
    /// # Returns
    /// The absolute value of `self`.
    fn abs(&self) -> Self {
        Self::from_inner_as_temporal(unsafe { meos_sys::tnumber_abs(self.inner()) })
    }

    /// Returns the change in value between successive pairs of `self`.
    ///
    /// # Returns
    /// The change in value between successive pairs of `self`.
    fn delta_value(&self) -> Self {
        Self::from_inner_as_temporal(unsafe { meos_sys::tnumber_delta_value(self.inner()) })
    }

    /// Returns the distance between `self` and `other`.
    ///
    /// # Arguments
    /// * `other` - A temporal number to compute the distance to.
    fn distance(&self, other: &Self) -> Self {
        Self::from_inner_as_temporal(unsafe {
            meos_sys::tdistance_tnumber_tnumber(self.inner(), other.inner())
        })
    }

    /// Returns the nearest approach distance between `self` and `other`.
    ///
    /// # Arguments
    /// * `other` - A temporal number to compute the nearest approach distance to.
    fn nearest_approach_distance(&self, other: &Self) -> Self::Type;
}

/// Generates the neccessary code to implement the temporal trait for the appropriate type
///
/// ## Parameters:
///    - `type`: The actual Rust type to implement the traits to
///    - `temporal_type`: Whether it's Instant, Sequence, or `SequenceSet`
///    - `base_type`: The base Rust type, i32 or f64.
///    - `basic_type`: Whether it's Int or Float.
macro_rules! impl_temporal_for_tnumber {
    ($type:ty, $temporal_type:ident, $base_type:ty, $basic_type:ident) => {
        paste::paste! {
            impl Collection for $type {
                impl_collection!(tnumber, $base_type);

                fn contains(&self, content: &Self::Type) -> bool {
                    [<$basic_type SpanSet>]::from_inner(unsafe { meos_sys::tnumber_valuespans(self.inner()) })
                        .contains(content)
                }
            }
            impl_simple_traits_for_temporal!($type, with_drop);


            impl TNumber for $type {
                fn nearest_approach_distance(&self, other: &Self) -> Self::Type {
                    unsafe { meos_sys::[<nad_ t $basic_type:lower _ t $basic_type:lower>](self.inner(), other.inner()) }
                }
            }

            impl OrderedTemporal for $type {
                fn min_value(&self) -> Self::Type {
                    unsafe { meos_sys::[<t $basic_type:lower _min_value>](self.inner()) }
                }

                fn max_value(&self) -> Self::Type {
                    unsafe { meos_sys::[<t $basic_type:lower _max_value>](self.inner()) }
                }

                impl_ordered_temporal_functions!([<$basic_type:lower>]);
            }
            impl Temporal for $type {
                type TI = [<T $basic_type Instant>];
                type TS = [<T $basic_type Sequence>];
                type TSS = [<T $basic_type SequenceSet>];
                type TBB = TBox;
                type Enum = [<T $basic_type>];
                type TBoolType = [<TBool $temporal_type>];

                fn from_inner_as_temporal(inner: *mut meos_sys::Temporal) -> Self {
                    Self {
                        #[allow(clippy::cast_ptr_alignment)]
                        _inner: ptr::NonNull::new(inner.cast::<meos_sys::[<T $temporal_type>]>()).expect("Null pointers not allowed"),
                    }
                }

                fn inner(&self) -> *const meos_sys::Temporal {
                    self._inner.as_ptr() as *const meos_sys::Temporal
                }

                fn bounding_box(&self) -> Self::TBB {
                    TNumber::bounding_box(self)
                }

                fn values(&self) -> Vec<Self::Type> {
                    let mut count = 0;
                    unsafe {
                        let values = meos_sys::[<t $basic_type:lower _values>](self.inner(), ptr::addr_of_mut!(count));

                        std::slice::from_raw_parts(values, count as usize).to_vec()
                    }
                }

                fn start_value(&self) -> Self::Type {
                    unsafe { meos_sys::[<t $basic_type:lower _start_value>](self.inner()) }
                }

                fn end_value(&self) -> Self::Type {
                    unsafe { meos_sys::[<t $basic_type:lower _end_value>](self.inner()) }
                }

                fn value_at_timestamp<Tz: TimeZone>(
                    &self,
                    timestamp: DateTime<Tz>,
                ) -> Option<Self::Type> {
                    let mut result = 0.into();
                    unsafe {
                        let success = meos_sys::[<t $basic_type:lower _value_at_timestamptz>](
                            self.inner(),
                            to_meos_timestamp(&timestamp),
                            true,
                            ptr::addr_of_mut!(result),
                        );
                        if success {
                            Some(result)
                        } else {
                            None
                        }
                    }
                }

                fn at_value(&self, value: &Self::Type) -> Option<Self::Enum> {
                    let result = unsafe { meos_sys::[<t $basic_type:lower _at_value>](self.inner(), *value) };
                    if !result.is_null() {
                        Some(factory::<Self::Enum>(result))
                    } else {
                        None
                    }
                }

                fn at_values(&self, values: &[Self::Type]) -> Option<Self::Enum> {
                    unsafe {
                        let set = meos_sys::[<$basic_type:lower set_make>](values.as_ptr(), values.len() as i32);
                        let result = meos_sys::temporal_at_values(self.inner(), set);
                        if !result.is_null() {
                            Some(factory::<Self::Enum>(result))
                        } else {
                            None
                        }
                    }
                }

                fn minus_value(&self, value: Self::Type) -> Self::Enum {
                    factory::<Self::Enum>(unsafe {
                        meos_sys::[<t $basic_type:lower _minus_value>](self.inner(), value)
                    })
                }

                fn minus_values(&self, values: &[Self::Type]) -> Self::Enum {
                    factory::<Self::Enum>(unsafe {
                        let set = meos_sys::[<$basic_type:lower set_make>](values.as_ptr(), values.len() as i32);
                        meos_sys::temporal_minus_values(self.inner(), set)
                    })
                }

                fn temporal_equal_value(&self, value: &Self::Type) -> Self::TBoolType {
                    Self::TBoolType::from_inner_as_temporal(unsafe {
                        meos_sys::[<teq_t $basic_type:lower _ $basic_type:lower>](self.inner(), *value)
                    })
                }

                fn temporal_not_equal_value(&self, value: &Self::Type) -> Self::TBoolType {
                    Self::TBoolType::from_inner_as_temporal(unsafe {
                        meos_sys::[<tne_t $basic_type:lower _ $basic_type:lower>](self.inner(), *value)
                    })
                }

                impl_always_and_ever_value_equality_functions!([<$basic_type:lower>]);
            }
        }
    }
}

macro_rules! impl_meos_enum {
    ($type:ty, $base_type:ty, $basic_type:ident) => {
        paste::paste! {
        impl Collection for $type {
            impl_collection!(tnumber, $base_type);

            fn contains(&self, content: &Self::Type) -> bool {
                [<$basic_type SpanSet>]::from_inner(unsafe { meos_sys::tnumber_valuespans(self.inner()) })
                    .contains(content)
            }
        }
        impl_simple_traits_for_temporal!($type);


        impl TNumber for $type {
            fn nearest_approach_distance(&self, other: &Self) -> Self::Type {
                unsafe { meos_sys::[<nad_ t $basic_type:lower _ t $basic_type:lower>](self.inner(), other.inner()) }
            }
        }

        impl OrderedTemporal for $type {
            fn min_value(&self) -> Self::Type {
                unsafe { meos_sys::[<t $basic_type:lower _min_value>](self.inner()) }
            }

            fn max_value(&self) -> Self::Type {
                unsafe { meos_sys::[<t $basic_type:lower _max_value>](self.inner()) }
            }

            impl_ordered_temporal_functions!([<$basic_type:lower>]);
        }
        impl Temporal for $type {
            type TI = [<T $basic_type Instant>];
            type TS = [<T $basic_type Sequence>];
            type TSS = [<T $basic_type SequenceSet>];
            type TBB = TBox;
            type Enum = [<T $basic_type>];
            type TBoolType = TBool;

            fn from_inner_as_temporal(inner: *mut meos_sys::Temporal) -> Self {
                factory::<Self>(inner)
            }

            fn inner(&self) -> *const meos_sys::Temporal {
                match self {
                    $type::Instant(value) => value.inner(),
                    $type::Sequence(value) => value.inner(),
                    $type::SequenceSet(value) => value.inner(),
                }
            }

            fn bounding_box(&self) -> Self::TBB {
                TNumber::bounding_box(self)
            }

            fn values(&self) -> Vec<Self::Type> {
                let mut count = 0;
                unsafe {
                    let values = meos_sys::[<t $basic_type:lower _values>](self.inner(), ptr::addr_of_mut!(count));

                    std::slice::from_raw_parts(values, count as usize).to_vec()
                }
            }

            fn start_value(&self) -> Self::Type {
                unsafe { meos_sys::[<t $basic_type:lower _start_value>](self.inner()) }
            }

            fn end_value(&self) -> Self::Type {
                unsafe { meos_sys::[<t $basic_type:lower _end_value>](self.inner()) }
            }

            fn value_at_timestamp<Tz: TimeZone>(
                &self,
                timestamp: DateTime<Tz>,
            ) -> Option<Self::Type> {
                let mut result = 0.into();
                unsafe {
                    let success = meos_sys::[<t $basic_type:lower _value_at_timestamptz>](
                        self.inner(),
                        to_meos_timestamp(&timestamp),
                        true,
                        ptr::addr_of_mut!(result),
                    );
                    if success {
                        Some(result)
                    } else {
                        None
                    }
                }
            }

            fn at_value(&self, value: &Self::Type) -> Option<Self::Enum> {
                let result = unsafe { meos_sys::[<t $basic_type:lower _at_value>](self.inner(), *value) };
                if !result.is_null() {
                    Some(factory::<Self::Enum>(result))
                } else {
                    None
                }
            }

            fn at_values(&self, values: &[Self::Type]) -> Option<Self::Enum> {
                unsafe {
                    let set = meos_sys::[<$basic_type:lower set_make>](values.as_ptr(), values.len() as i32);
                    let result = meos_sys::temporal_at_values(self.inner(), set);
                    if !result.is_null() {
                        Some(factory::<Self::Enum>(result))
                    } else {
                        None
                    }
                }
            }

            fn minus_value(&self, value: Self::Type) -> Self::Enum {
                factory::<Self::Enum>(unsafe {
                    meos_sys::[<t $basic_type:lower _minus_value>](self.inner(), value)
                })
            }

            fn minus_values(&self, values: &[Self::Type]) -> Self::Enum {
                factory::<Self::Enum>(unsafe {
                    let set = meos_sys::[<$basic_type:lower set_make>](values.as_ptr(), values.len() as i32);
                    meos_sys::temporal_minus_values(self.inner(), set)
                })
            }

            fn temporal_equal_value(&self, value: &Self::Type) -> Self::TBoolType {
                Self::TBoolType::from_inner_as_temporal(unsafe {
                    meos_sys::[<teq_t $basic_type:lower _ $basic_type:lower>](self.inner(), *value)
                })
            }

            fn temporal_not_equal_value(&self, value: &Self::Type) -> Self::TBoolType {
                Self::TBoolType::from_inner_as_temporal(unsafe {
                    meos_sys::[<tne_t $basic_type:lower _ $basic_type:lower>](self.inner(), *value)
                })
            }

            impl_always_and_ever_value_equality_functions!([<$basic_type:lower>]);
        }
    }
    };
}

pub(crate) use impl_meos_enum;
pub(crate) use impl_temporal_for_tnumber;