dbn 0.73.0

Library for working with Databento Binary Encoding (DBN)
Documentation
use std::mem;

use crate::{record::as_u8_slice, v3, HasRType, Record, RecordHeader, RecordMut, WithTsOut};

impl<T: v3::HasRType> Record for WithTsOut<T> {
    type Header = RecordHeader;

    fn record_size(&self) -> usize {
        self.rec.record_size()
    }

    fn rtype(&self) -> crate::Result<crate::RType> {
        self.rec.rtype()
    }

    fn raw_rtype(&self) -> u16 {
        self.rec.raw_rtype()
    }

    fn publisher_id(&self) -> u16 {
        self.rec.publisher_id()
    }

    fn publisher(&self) -> crate::Result<crate::Publisher> {
        self.rec.publisher()
    }

    fn instrument_id(&self) -> u64 {
        self.rec.instrument_id()
    }

    fn raw_ts_event(&self) -> u64 {
        self.rec.raw_ts_event()
    }

    fn raw_index_ts(&self) -> u64 {
        self.rec.raw_index_ts()
    }
}

impl<T: v3::HasRType + RecordMut> RecordMut for WithTsOut<T> {
    fn header_mut(&mut self) -> &mut RecordHeader {
        self.rec.header_mut()
    }
}

impl<T: v3::HasRType> HasRType for WithTsOut<T> {
    fn has_rtype(rtype: u16) -> bool {
        T::has_rtype(rtype)
    }
}

impl<T> AsRef<[u8]> for WithTsOut<T>
where
    T: HasRType,
{
    fn as_ref(&self) -> &[u8] {
        unsafe { as_u8_slice(self) }
    }
}

impl<T: v3::HasRType + RecordMut> WithTsOut<T> {
    /// Creates a new record with `ts_out`. Updates the `length` property in
    /// [`RecordHeader`] to ensure the additional field is accounted for.
    pub fn new(rec: T, ts_out: u64) -> Self {
        let mut res = Self { rec, ts_out };
        res.header_mut().length = (mem::size_of_val(&res) / RecordHeader::LENGTH_MULTIPLIER) as u8;
        res
    }
}

impl<T: v3::HasRType> WithTsOut<T> {
    /// Parses the raw live gateway send timestamp into a datetime.
    pub fn ts_out(&self) -> time::OffsetDateTime {
        // u64::MAX is within maximum allowable range
        time::OffsetDateTime::from_unix_timestamp_nanos(self.ts_out as i128).unwrap()
    }
}