-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: set up the storage and encoding crates
- Loading branch information
Showing
11 changed files
with
211 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "storey-encoding" | ||
description = "Interfaces for storey encodings" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
categories.workspace = true | ||
keywords.workspace = true | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
pub trait Encoding { | ||
type EncodeError; | ||
type DecodeError; | ||
} | ||
|
||
pub trait EncodableWith<E: Encoding>: sealed::SealedE<E> { | ||
fn encode(&self) -> Result<Vec<u8>, E::EncodeError>; | ||
} | ||
|
||
pub trait EncodableWithImpl<E: Encoding> { | ||
fn encode_impl(self) -> Result<Vec<u8>, E::EncodeError>; | ||
} | ||
|
||
impl<E: Encoding, T> EncodableWith<E> for T | ||
where | ||
for<'a> Cover<&'a T>: EncodableWithImpl<E>, | ||
{ | ||
fn encode(&self) -> Result<Vec<u8>, <E as Encoding>::EncodeError> { | ||
Cover(self).encode_impl() | ||
} | ||
} | ||
|
||
pub trait DecodableWith<E: Encoding>: Sized + sealed::SealedD<E> { | ||
fn decode(data: &[u8]) -> Result<Self, E::DecodeError>; | ||
} | ||
|
||
pub trait DecodableWithImpl<E: Encoding>: Sized { | ||
fn decode_impl(data: &[u8]) -> Result<Self, E::DecodeError>; | ||
} | ||
|
||
impl<E: Encoding, T> DecodableWith<E> for T | ||
where | ||
Cover<T>: DecodableWithImpl<E>, | ||
{ | ||
fn decode(data: &[u8]) -> Result<Self, <E as Encoding>::DecodeError> { | ||
let wrapper = <Cover<Self>>::decode_impl(data)?; | ||
Ok(wrapper.0) | ||
} | ||
} | ||
|
||
mod sealed { | ||
// This module is private to the crate. It's used to seal the `EncodableWith` and | ||
// `DecodableWith` traits, so that the only way they can be implemented outside | ||
// this crate is through the blanket implementations provided by `EncodableWithImpl` | ||
// and `DecodableWithImpl`. | ||
// | ||
// More information on sealed traits: | ||
// https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed | ||
|
||
use super::*; | ||
|
||
pub trait SealedE<E> {} | ||
pub trait SealedD<E> {} | ||
|
||
impl<E: Encoding, T> SealedE<E> for T where for<'a> Cover<&'a T>: EncodableWithImpl<E> {} | ||
impl<E: Encoding, T> SealedD<E> for T where Cover<T>: DecodableWithImpl<E> {} | ||
} | ||
|
||
pub struct Cover<T>(pub T); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "storey-storage" | ||
description = "Interfaces for storey storage backends" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
categories.workspace = true | ||
keywords.workspace = true | ||
|
||
[dependencies] |
2 changes: 1 addition & 1 deletion
2
crates/storey/src/storage/backend.rs → crates/storey-storage/src/backend.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod backend; | ||
mod storage; | ||
|
||
pub use backend::{StorageBackend, StorageBackendMut}; | ||
pub use storage::{IterableStorage, RevIterableStorage, Storage, StorageMut}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
pub trait Storage { | ||
fn get(&self, key: &[u8]) -> Option<Vec<u8>>; | ||
|
||
fn has(&self, key: &[u8]) -> bool { | ||
self.get(key).is_some() | ||
} | ||
|
||
fn get_meta(&self, _key: &[u8]) -> Option<Vec<u8>>; | ||
fn has_meta(&self, key: &[u8]) -> bool { | ||
self.get_meta(key).is_some() | ||
} | ||
} | ||
|
||
pub trait StorageMut { | ||
fn set(&mut self, key: &[u8], value: &[u8]); | ||
fn remove(&mut self, key: &[u8]); | ||
|
||
fn set_meta(&mut self, _key: &[u8], _value: &[u8]); | ||
fn remove_meta(&mut self, _key: &[u8]); | ||
} | ||
|
||
pub trait IterableStorage { | ||
type KeysIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type ValuesIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type PairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)> | ||
where | ||
Self: 'a; | ||
|
||
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a>; | ||
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a>; | ||
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a>; | ||
} | ||
|
||
impl<T: IterableStorage> IterableStorage for &T { | ||
type KeysIterator<'a> = T::KeysIterator<'a> where Self: 'a; | ||
type ValuesIterator<'a> = T::ValuesIterator<'a> where Self: 'a; | ||
type PairsIterator<'a> = T::PairsIterator<'a> where Self: 'a; | ||
|
||
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a> { | ||
(**self).keys(start, end) | ||
} | ||
|
||
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a> { | ||
(**self).values(start, end) | ||
} | ||
|
||
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a> { | ||
(**self).pairs(start, end) | ||
} | ||
} | ||
|
||
impl<T: IterableStorage> IterableStorage for &mut T { | ||
type KeysIterator<'a> = T::KeysIterator<'a> where Self: 'a; | ||
type ValuesIterator<'a> = T::ValuesIterator<'a> where Self: 'a; | ||
type PairsIterator<'a> = T::PairsIterator<'a> where Self: 'a; | ||
|
||
fn keys<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::KeysIterator<'a> { | ||
(**self).keys(start, end) | ||
} | ||
|
||
fn values<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::ValuesIterator<'a> { | ||
(**self).values(start, end) | ||
} | ||
|
||
fn pairs<'a>(&'a self, start: Option<&[u8]>, end: Option<&[u8]>) -> Self::PairsIterator<'a> { | ||
(**self).pairs(start, end) | ||
} | ||
} | ||
|
||
pub trait RevIterableStorage { | ||
type RevKeysIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type RevValuesIterator<'a>: Iterator<Item = Vec<u8>> | ||
where | ||
Self: 'a; | ||
type RevPairsIterator<'a>: Iterator<Item = (Vec<u8>, Vec<u8>)> | ||
where | ||
Self: 'a; | ||
|
||
fn rev_keys<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevKeysIterator<'a>; | ||
fn rev_values<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevValuesIterator<'a>; | ||
fn rev_pairs<'a>( | ||
&'a self, | ||
start: Option<&[u8]>, | ||
end: Option<&[u8]>, | ||
) -> Self::RevPairsIterator<'a>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod column; | ||
mod item; | ||
mod map; | ||
mod test_mocks; | ||
|
||
use std::marker::PhantomData; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.