apple-quant-algorithmic 0.3.0

Apple Quant's algorithmic library.
use crate::timestamp::{Timestamp, TimestampType, Timestamped, UtcNs};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BinIndex<const NS_LEN: u64>(u64);

impl<const NS_LEN: u64> BinIndex<NS_LEN> {
	pub(super) fn from_timestamped(
		timestamped: impl Timestamped,
	) -> Self {
		let timestamp = timestamped.timestamp();
		let ns = timestamp.as_timestamp_type();
		let idx = ns / NS_LEN as TimestampType;
		let idx = idx as u64;

		Self(idx)
	}

	pub fn as_timestamp(
		&self,
	) -> Timestamp {
		(*self).into()
	}

	pub fn into_timestamp(
		self,
	) -> Timestamp {
		self.into()
	}
}

impl<const NS_LEN: u64> Timestamped for BinIndex<NS_LEN> {
	fn timestamp(
		&self,
	) -> Timestamp {
		let idx = self.0 as TimestampType;
		let utc_ns = idx * NS_LEN as TimestampType;

		let utc_ns = unsafe {
			UtcNs::new_unchecked(utc_ns)
		};

		utc_ns.into()
	}
}