#![allow(unreachable_code, unused)]
mod cell;
mod config;
mod depq;
mod map;
mod once;
mod primitives;
mod set;
mod sync;
mod vec;
mod when;
pub use {
cell::{Cell, CellReader, CellWriter},
config::CollectionConfig,
depq::{
BoundedPriorityQueue,
PriorityQueue,
PriorityQueueReader,
PriorityQueueWriter,
UnboundedPriorityQueue,
},
map::{Map, MapReader, MapWriter},
once::{Once, OnceReader, OnceWriter},
primitives::{StoreId, Version},
set::{Set, SetReader, SetWriter},
sync::Config as SyncConfig,
vec::{Vec, VecReader, VecWriter},
when::When,
};
use crate::primitives::EncodeError;
const WRITER: bool = true;
const READER: bool = false;
#[derive(Debug, thiserror::Error)]
pub enum Error<T> {
#[error("Offline")]
Offline(T),
#[error("Encoding: {1}")]
Encoding(T, EncodeError),
#[error("Network is down")]
NetworkDown,
}
pub trait CollectionReader {
type Reader;
fn reader(network: &crate::Network) -> Self::Reader;
fn online_reader(
network: &crate::Network,
) -> impl Future<Output = Self::Reader> + Send + Sync + 'static;
}
pub trait CollectionWriter {
type Writer;
fn writer(network: &crate::Network) -> Self::Writer;
fn online_writer(
network: &crate::Network,
) -> impl Future<Output = Self::Writer> + Send + Sync + 'static;
}
pub type ReaderOf<C> = <C as CollectionReader>::Reader;
pub type WriterOf<C> = <C as CollectionWriter>::Writer;
#[macro_export]
macro_rules! collection {
(#[$($meta:tt)*] $($rest:tt)*) => {
$crate::collection! { @attrs [#[$($meta)*]] $($rest)* }
};
(@attrs [$($attrs:tt)*] #[$($meta:tt)*] $($rest:tt)*) => {
$crate::collection! { @attrs [$($attrs)* #[$($meta)*]] $($rest)* }
};
(@attrs [$($attrs:tt)*] $($rest:tt)*) => {
$crate::__collection_impl! { @$crate; $($attrs)* $($rest)* }
};
($($tt:tt)*) => {
$crate::__collection_impl! { @$crate; $($tt)* }
};
}
pub trait CollectionFromDef {
type Reader;
type Writer;
fn reader(network: &crate::Network, store_id: StoreId) -> Self::Reader {
Self::reader_with_config(network, store_id, CollectionConfig::default())
}
fn writer(network: &crate::Network, store_id: StoreId) -> Self::Writer {
Self::writer_with_config(network, store_id, CollectionConfig::default())
}
fn reader_with_config(
network: &crate::Network,
store_id: StoreId,
config: CollectionConfig,
) -> Self::Reader;
fn writer_with_config(
network: &crate::Network,
store_id: StoreId,
config: CollectionConfig,
) -> Self::Writer;
}
#[derive(Clone, Copy)]
pub struct CollectionDef<C: CollectionFromDef> {
pub store_id: StoreId,
config: Option<fn() -> CollectionConfig>,
_marker: core::marker::PhantomData<fn(&C)>,
}
impl<C: CollectionFromDef> CollectionDef<C> {
pub const fn new(store_id: StoreId) -> Self {
Self {
store_id,
config: None,
_marker: core::marker::PhantomData,
}
}
#[must_use]
pub const fn with_config(mut self, config: fn() -> CollectionConfig) -> Self {
self.config = Some(config);
self
}
pub const fn from_reader(reader_def: &ReaderDef<C>) -> Self {
Self {
store_id: reader_def.store_id,
config: reader_def.config,
_marker: core::marker::PhantomData,
}
}
pub const fn from_writer(writer_def: &WriterDef<C>) -> Self {
Self {
store_id: writer_def.store_id,
config: writer_def.config,
_marker: core::marker::PhantomData,
}
}
pub const fn as_reader(&self) -> ReaderDef<C> {
ReaderDef {
store_id: self.store_id,
config: self.config,
_marker: core::marker::PhantomData,
}
}
pub const fn as_writer(&self) -> WriterDef<C> {
WriterDef {
store_id: self.store_id,
config: self.config,
_marker: core::marker::PhantomData,
}
}
#[inline]
pub fn reader(&self, network: &crate::Network) -> C::Reader {
self.config.map_or_else(
|| C::reader(network, self.store_id),
|f| C::reader_with_config(network, self.store_id, f()),
)
}
#[inline]
pub fn writer(&self, network: &crate::Network) -> C::Writer {
self.config.map_or_else(
|| C::writer(network, self.store_id),
|f| C::writer_with_config(network, self.store_id, f()),
)
}
}
#[derive(Clone, Copy)]
pub struct ReaderDef<C: CollectionFromDef> {
pub store_id: StoreId,
config: Option<fn() -> CollectionConfig>,
_marker: core::marker::PhantomData<fn(&C)>,
}
impl<C: CollectionFromDef> ReaderDef<C> {
pub const fn new(store_id: StoreId) -> Self {
Self {
store_id,
config: None,
_marker: core::marker::PhantomData,
}
}
#[must_use]
pub const fn with_config(mut self, config: fn() -> CollectionConfig) -> Self {
self.config = Some(config);
self
}
pub fn open(&self, network: &crate::Network) -> C::Reader {
self.config.map_or_else(
|| C::reader(network, self.store_id),
|f| C::reader_with_config(network, self.store_id, f()),
)
}
}
#[derive(Clone, Copy)]
pub struct WriterDef<C: CollectionFromDef> {
pub store_id: StoreId,
config: Option<fn() -> CollectionConfig>,
_marker: core::marker::PhantomData<fn(&C)>,
}
impl<C: CollectionFromDef> WriterDef<C> {
pub const fn new(store_id: StoreId) -> Self {
Self {
store_id,
config: None,
_marker: core::marker::PhantomData,
}
}
#[must_use]
pub const fn with_config(mut self, config: fn() -> CollectionConfig) -> Self {
self.config = Some(config);
self
}
pub fn open(&self, network: &crate::Network) -> C::Writer {
self.config.map_or_else(
|| C::writer(network, self.store_id),
|f| C::writer_with_config(network, self.store_id, f()),
)
}
}