I need to implement the Iterator trait:
- whose
next()returns a genericDateTime<Tz>type, but - acts as a wrapper over an inner iterator in which the timestamp is stored in nanoseconds.
Remarks:
- condition (1) is enforced by the fact that the implementer is an
enumvariant; - condition (2) is given by the fact that the inner iterator is over a
polar(dataframe) struct that stores timestamps as integers;
Here is my attemp:
use chrono::{TimeZone, DateTime, NativeDateTime, Utc};
use polars::prelude::*; // dataframe crate providing:NumericChunkIterDispatch, TimestampNanosecondType
pub struct TimestampScheduleIterator<'a, Tz: TimeZone> {
iterator: NumericChunkIterDispatch<'a, TimestampNanosecondType>,
}
impl<'a, Tz> TimestampScheduleIterator<'a, Tz>
where
Tz: TimeZone,
{
impl<'a, Tz> Iterator for TimestampScheduleIterator<'a, Tz>
where
Tz: TimeZone,
{
type Item = DateTime<Tz>;
fn next(&mut self) -> Option<DateTime<Tz>> {
// conversion helper (from `polar`'s `conversions_utils.rs`)
fn timestamp_nanoseconds_as_datetime(v: i64) -> NaiveDateTime {
// some nanoseconds will be truncated down as integer division rounds downwards
let seconds = v.clone() / 1_000_000_000;
// we can use that to compute the remaining nanoseconds
let nanoseconds = (v - (seconds * 1_000_000_000)) as u32;
NaiveDateTime::from_timestamp(seconds, nanoseconds)
}
if let Some(ns) = self.iterator.next() {
let naive = timestamp_nanoseconds_as_datetime(ns.unwrap());
let dt = Utc.from_utc_datetime(&naive);
Some(dt)
}
else { None }
}
}
Compilation generates the following error:
error[E0308]: mismatched types
--> src/accounting/event/schedule.rs:200:18
|
168 | impl<'a, Tz> Iterator for TimestampScheduleIterator<'a, Tz>
| -- this type parameter
...
200 | Some(dt)
| ^^ expected type parameter `Tz`, found struct `chrono::Utc`
|
= note: expected struct `DateTime<Tz>`
found struct `DateTime<chrono::Utc>`
Help much appreciated!