use std::ops::Deref;
use crate::param::AccountId;
use crate::pretrade::PreTradePolicy;
use crate::storage::{self, LockingPolicyFactory};
pub trait SyncMode: 'static {
type Strong<T: 'static>: Clone + Deref<Target = T>;
type Weak<T: 'static>: 'static;
type StorageLockingPolicyFactory: LockingPolicyFactory
+ storage::CreateStorageFor<AccountId>
+ 'static;
type PreTradePolicyObject<
Order: 'static,
ExecutionReport: 'static,
AccountAdjustment: 'static,
>: PreTradePolicy<Order, ExecutionReport, AccountAdjustment, Self>
+ ?Sized
+ 'static;
fn new_strong<T: 'static>(inner: T) -> Self::Strong<T>;
fn downgrade<T: 'static>(s: &Self::Strong<T>) -> Self::Weak<T>;
fn upgrade<T: 'static>(w: &Self::Weak<T>) -> Option<Self::Strong<T>>;
fn storage_locking_policy_factory(&self) -> Self::StorageLockingPolicyFactory;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct LocalSync;
impl SyncMode for LocalSync {
type Strong<T: 'static> = std::rc::Rc<T>;
type Weak<T: 'static> = std::rc::Weak<T>;
type StorageLockingPolicyFactory = crate::storage::NoLocking;
type PreTradePolicyObject<
Order: 'static,
ExecutionReport: 'static,
AccountAdjustment: 'static,
> = dyn PreTradePolicy<Order, ExecutionReport, AccountAdjustment, LocalSync>;
fn new_strong<T: 'static>(inner: T) -> Self::Strong<T> {
std::rc::Rc::new(inner)
}
fn downgrade<T: 'static>(s: &Self::Strong<T>) -> Self::Weak<T> {
std::rc::Rc::downgrade(s)
}
fn upgrade<T: 'static>(w: &Self::Weak<T>) -> Option<Self::Strong<T>> {
w.upgrade()
}
fn storage_locking_policy_factory(&self) -> Self::StorageLockingPolicyFactory {
Default::default()
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct FullSync;
impl SyncMode for FullSync {
type Strong<T: 'static> = std::sync::Arc<T>;
type Weak<T: 'static> = std::sync::Weak<T>;
type StorageLockingPolicyFactory = crate::storage::FullLocking;
type PreTradePolicyObject<
Order: 'static,
ExecutionReport: 'static,
AccountAdjustment: 'static,
> = dyn PreTradePolicy<Order, ExecutionReport, AccountAdjustment, FullSync> + Send + Sync;
fn new_strong<T: 'static>(inner: T) -> Self::Strong<T> {
std::sync::Arc::new(inner)
}
fn downgrade<T: 'static>(s: &Self::Strong<T>) -> Self::Weak<T> {
std::sync::Arc::downgrade(s)
}
fn upgrade<T: 'static>(w: &Self::Weak<T>) -> Option<Self::Strong<T>> {
w.upgrade()
}
fn storage_locking_policy_factory(&self) -> Self::StorageLockingPolicyFactory {
Default::default()
}
}
pub struct AccountSyncHandle<T: ?Sized>(
std::sync::Arc<T>,
std::marker::PhantomData<std::cell::Cell<()>>,
);
impl<T: ?Sized> Clone for AccountSyncHandle<T> {
fn clone(&self) -> Self {
Self(std::sync::Arc::clone(&self.0), std::marker::PhantomData)
}
}
impl<T: ?Sized> Deref for AccountSyncHandle<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
unsafe impl<T: ?Sized + Send> Send for AccountSyncHandle<T> {}
pub struct AccountSyncHandleWeak<T: ?Sized>(
std::sync::Weak<T>,
std::marker::PhantomData<std::cell::Cell<()>>,
);
impl<T: ?Sized> Clone for AccountSyncHandleWeak<T> {
fn clone(&self) -> Self {
Self(std::sync::Weak::clone(&self.0), std::marker::PhantomData)
}
}
unsafe impl<T: ?Sized + Send> Send for AccountSyncHandleWeak<T> {}
#[derive(Debug, Default, Clone, Copy)]
pub struct AccountSync;
impl SyncMode for AccountSync {
type Strong<T: 'static> = AccountSyncHandle<T>;
type Weak<T: 'static> = AccountSyncHandleWeak<T>;
type StorageLockingPolicyFactory = crate::storage::IndexLocking<crate::AccountKeyConstraint>;
type PreTradePolicyObject<
Order: 'static,
ExecutionReport: 'static,
AccountAdjustment: 'static,
> = dyn PreTradePolicy<Order, ExecutionReport, AccountAdjustment, AccountSync> + Send;
fn new_strong<T: 'static>(inner: T) -> Self::Strong<T> {
AccountSyncHandle(std::sync::Arc::new(inner), std::marker::PhantomData)
}
fn downgrade<T: 'static>(s: &Self::Strong<T>) -> Self::Weak<T> {
AccountSyncHandleWeak(std::sync::Arc::downgrade(&s.0), std::marker::PhantomData)
}
fn upgrade<T: 'static>(w: &Self::Weak<T>) -> Option<Self::Strong<T>> {
w.0.upgrade()
.map(|inner| AccountSyncHandle(inner, std::marker::PhantomData))
}
fn storage_locking_policy_factory(&self) -> Self::StorageLockingPolicyFactory {
Default::default()
}
}