use std::sync::atomic::{AtomicU64, Ordering};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum CoreError {
#[error("Invalid or stale handle (index: {index}, generation: {generation})")]
StaleHandle {
index: u32,
generation: u32,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct QuoteId {
pub index: u32,
pub generation: u32,
}
pub trait Quote: Send + Sync {
fn value(&self) -> f64;
fn is_valid(&self) -> bool;
fn revision(&self) -> u64;
}
#[derive(Debug)]
pub struct SimpleQuote {
value: std::sync::RwLock<f64>,
revision: AtomicU64,
}
impl SimpleQuote {
pub fn new(val: f64) -> Self {
Self {
value: std::sync::RwLock::new(val),
revision: AtomicU64::new(0),
}
}
pub fn set_value(&self, new_val: f64) {
let mut lock = self.value.write().expect("RwLock write lock");
*lock = new_val;
self.revision.fetch_add(1, Ordering::SeqCst);
}
}
impl Quote for SimpleQuote {
#[inline]
fn value(&self) -> f64 {
*self.value.read().expect("RwLock read lock")
}
#[inline]
fn is_valid(&self) -> bool {
let v = self.value();
v.is_finite()
}
#[inline]
fn revision(&self) -> u64 {
self.revision.load(Ordering::SeqCst)
}
}