mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-30 09:30:00 -07:00
global: snapshot
This commit is contained in:
65
crates/brk_vec/src/traits/any.rs
Normal file
65
crates/brk_vec/src/traits/any.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use std::{io, mem};
|
||||
|
||||
use crate::{Result, StorableVec, STATELESS};
|
||||
|
||||
use super::{StoredIndex, StoredType};
|
||||
|
||||
pub trait AnyStorableVec: Send + Sync {
|
||||
fn file_name(&self) -> String;
|
||||
fn index_type_to_string(&self) -> &str;
|
||||
fn len(&self) -> usize;
|
||||
fn is_empty(&self) -> bool;
|
||||
fn flush(&mut self) -> io::Result<()>;
|
||||
}
|
||||
|
||||
impl<I, T, const MODE: u8> AnyStorableVec for StorableVec<I, T, MODE>
|
||||
where
|
||||
I: StoredIndex,
|
||||
T: StoredType,
|
||||
{
|
||||
fn file_name(&self) -> String {
|
||||
self.file_name()
|
||||
}
|
||||
|
||||
fn index_type_to_string(&self) -> &str {
|
||||
self.index_type_to_string()
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.is_empty()
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.flush()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "json")]
|
||||
pub trait AnyJsonStorableVec: AnyStorableVec {
|
||||
fn collect_range_values(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<serde_json::Value>>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "json")]
|
||||
impl<I, T, const MODE: u8> AnyJsonStorableVec for StorableVec<I, T, MODE>
|
||||
where
|
||||
I: StoredIndex,
|
||||
T: StoredType + serde::Serialize,
|
||||
{
|
||||
fn collect_range_values(&self, from: Option<i64>, to: Option<i64>) -> Result<Vec<serde_json::Value>> {
|
||||
if MODE == STATELESS {
|
||||
Ok(
|
||||
unsafe { mem::transmute::<&StorableVec<I, T, MODE>, &StorableVec<I, T, STATELESS>>(self) }
|
||||
.collect_range(from, to)?
|
||||
.into_iter()
|
||||
.map(|v| serde_json::to_value(v).unwrap())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
} else {
|
||||
todo!("todo ?")
|
||||
}
|
||||
}
|
||||
}
|
||||
11
crates/brk_vec/src/traits/bytes.rs
Normal file
11
crates/brk_vec/src/traits/bytes.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::Result;
|
||||
|
||||
pub trait Bytes: Sized {
|
||||
const LEN: usize = size_of::<Self>();
|
||||
fn to_bytes(&self) -> Arc<[u8]>;
|
||||
fn try_from_bytes(bytes: &[u8]) -> Result<Self>;
|
||||
}
|
||||
|
||||
pub trait UnsafeBytes {}
|
||||
8
crates/brk_vec/src/traits/mod.rs
Normal file
8
crates/brk_vec/src/traits/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
mod any;
|
||||
// mod bytes;
|
||||
mod stored_index;
|
||||
mod stored_type;
|
||||
|
||||
pub use any::*;
|
||||
pub use stored_index::*;
|
||||
pub use stored_type::*;
|
||||
35
crates/brk_vec/src/traits/stored_index.rs
Normal file
35
crates/brk_vec/src/traits/stored_index.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::{fmt::Debug, ops::Add};
|
||||
|
||||
pub trait StoredIndex
|
||||
where
|
||||
Self: Debug
|
||||
+ Default
|
||||
+ Copy
|
||||
+ Clone
|
||||
+ PartialEq
|
||||
+ Eq
|
||||
+ PartialOrd
|
||||
+ Ord
|
||||
+ TryInto<usize>
|
||||
+ From<usize>
|
||||
+ Add<usize, Output = Self>
|
||||
+ Send
|
||||
+ Sync,
|
||||
{
|
||||
}
|
||||
impl<I> StoredIndex for I where
|
||||
I: Debug
|
||||
+ Default
|
||||
+ Copy
|
||||
+ Clone
|
||||
+ PartialEq
|
||||
+ Eq
|
||||
+ PartialOrd
|
||||
+ Ord
|
||||
+ TryInto<usize>
|
||||
+ From<usize>
|
||||
+ Add<usize, Output = Self>
|
||||
+ Send
|
||||
+ Sync
|
||||
{
|
||||
}
|
||||
13
crates/brk_vec/src/traits/stored_type.rs
Normal file
13
crates/brk_vec/src/traits/stored_type.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes};
|
||||
|
||||
pub trait StoredType
|
||||
where
|
||||
Self: Sized + Debug + Clone + TryFromBytes + IntoBytes + Immutable + KnownLayout + Send + Sync,
|
||||
{
|
||||
}
|
||||
impl<T> StoredType for T where
|
||||
T: Sized + Debug + Clone + TryFromBytes + IntoBytes + Immutable + KnownLayout + Send + Sync
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user