diff --git a/crates/store/src/entry.rs b/crates/store/src/entry.rs index c5ce1b947..d2afcfd90 100644 --- a/crates/store/src/entry.rs +++ b/crates/store/src/entry.rs @@ -1,16 +1,16 @@ use crate::key::AsKeyParts; use crate::slice::Slice; -pub trait DataType: Sized { +pub trait DataType<'a>: Sized { type Error; - fn from_slice(slice: Slice) -> Result; - fn as_slice(&self) -> Result; + fn from_slice(slice: Slice<'a>) -> Result; + fn as_slice(&'a self) -> Result, Self::Error>; } pub trait Entry { type Key: AsKeyParts; - type DataType: DataType; + type DataType<'a>: DataType<'a>; fn key(&self) -> &Self::Key; } diff --git a/crates/store/src/handle.rs b/crates/store/src/handle.rs index bb82c6f92..63702685d 100644 --- a/crates/store/src/handle.rs +++ b/crates/store/src/handle.rs @@ -33,23 +33,26 @@ impl<'base, 'k, 'v, L: WriteLayer<'k, 'v>> StoreHandle { } #[derive(Debug, Error)] -pub enum Error { +pub enum Error<'a, E: DataType<'a>> { LayerError(eyre::Report), CodecError(E::Error), } -impl From for Error { +impl<'a, E: DataType<'a>> From for Error<'a, E> { fn from(err: eyre::Report) -> Self { Self::LayerError(err) } } -impl<'k, L: ReadLayer<'k>> StoreHandle { - pub fn has(&self, entry: &'k E) -> Result> { +impl<'a, 'k, L: ReadLayer<'k>> StoreHandle { + pub fn has(&self, entry: &'k E) -> Result>> { Ok(self.inner.has(entry.key())?) } - pub fn get(&self, entry: &'k E) -> Result, Error> { + pub fn get( + &self, + entry: &'k E, + ) -> Result>, Error>> { match self.inner.get(entry.key())? { Some(value) => Ok(Some( E::DataType::from_slice(value).map_err(Error::CodecError)?, @@ -61,23 +64,23 @@ impl<'k, L: ReadLayer<'k>> StoreHandle { pub fn iter>( &'k self, start: &'k E, - ) -> Result, Structured>, Error> { + ) -> Result, Structured>>, Error>> { Ok(self.inner.iter(start.key())?.structured_value()) } } impl<'k, 'v, L: WriteLayer<'k, 'v>> StoreHandle { pub fn put( - &mut self, + &'v mut self, entry: &'k E, - value: &'v E::DataType, - ) -> Result<(), Error> { + value: &'v E::DataType<'v>, + ) -> Result<(), Error>> { self.inner .put(entry.key(), value.as_slice().map_err(Error::CodecError)?) .map_err(Error::LayerError) } - pub fn delete(&mut self, entry: &'k E) -> Result<(), Error> { + pub fn delete(&mut self, entry: &'k E) -> Result<(), Error>> { self.inner.delete(entry.key()).map_err(Error::LayerError) } } diff --git a/crates/store/src/iter.rs b/crates/store/src/iter.rs index 7c6c875d5..84c0bce4d 100644 --- a/crates/store/src/iter.rs +++ b/crates/store/src/iter.rs @@ -51,7 +51,7 @@ impl<'a, V> Iter<'a, Unstructured, V> { } impl<'a, K> Iter<'a, K, Unstructured> { - pub fn structured_value(self) -> Iter<'a, K, Structured> { + pub fn structured_value>(self) -> Iter<'a, K, Structured> { Iter { inner: self.inner, _priv: PhantomData, @@ -158,7 +158,7 @@ impl<'a, K: FromKeyParts> TryIntoKey<'a> for Structured { } } -impl<'a, V: DataType> TryIntoValue<'a> for Structured { +impl<'a, V: DataType<'a>> TryIntoValue<'a> for Structured { type Value = V; type Error = Error; diff --git a/crates/store/src/types.rs b/crates/store/src/types.rs index 19b43e107..b6fa18a15 100644 --- a/crates/store/src/types.rs +++ b/crates/store/src/types.rs @@ -8,12 +8,12 @@ pub use context::{ContextIdentity, ContextMeta, ContextState, ContextTransaction pub use generic::GenericData; pub trait PredefinedEntry: key::AsKeyParts { - type DataType: DataType; + type DataType<'a>: DataType<'a>; } impl Entry for T { type Key = T; - type DataType = T::DataType; + type DataType<'a> = T::DataType<'a>; fn key(&self) -> &Self::Key { self diff --git a/crates/store/src/types/context.rs b/crates/store/src/types/context.rs index d17942bb6..7cae2187d 100644 --- a/crates/store/src/types/context.rs +++ b/crates/store/src/types/context.rs @@ -8,9 +8,12 @@ use crate::slice::Slice; use crate::types::PredefinedEntry; #[derive(Eq, Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize)] -pub struct ContextMeta {} +pub struct ContextMeta { + // todo! make [u8; 32] when application_id<->meta is a separate record + pub application_id: Box, +} -impl DataType for ContextMeta { +impl DataType<'_> for ContextMeta { type Error = io::Error; fn from_slice(slice: Slice) -> Result { @@ -23,32 +26,36 @@ impl DataType for ContextMeta { } impl PredefinedEntry for key::ContextMeta { - type DataType = ContextMeta; + type DataType<'a> = ContextMeta; } -#[derive(Eq, Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize)] -pub struct ContextState {} +#[derive(Eq, Clone, Debug, PartialEq)] +pub struct ContextState<'a> { + pub value: Slice<'a>, +} -impl DataType for ContextState { +impl<'a> DataType<'a> for ContextState<'a> { type Error = io::Error; - fn from_slice(slice: Slice) -> Result { - borsh::from_slice(&slice) + fn from_slice(slice: Slice<'a>) -> Result { + Ok(Self { value: slice }) } - fn as_slice(&self) -> Result { - borsh::to_vec(self).map(Into::into) + fn as_slice(&'a self) -> Result, Self::Error> { + Ok(self.value.as_ref().into()) } } impl PredefinedEntry for key::ContextState { - type DataType = ContextState; + type DataType<'a> = ContextState<'a>; } #[derive(Eq, Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize)] -pub struct ContextIdentity {} +pub struct ContextIdentity { + pub private_key: Option<[u8; 32]>, +} -impl DataType for ContextIdentity { +impl DataType<'_> for ContextIdentity { type Error = io::Error; fn from_slice(slice: Slice) -> Result { @@ -61,13 +68,18 @@ impl DataType for ContextIdentity { } impl PredefinedEntry for key::ContextIdentity { - type DataType = ContextIdentity; + type DataType<'a> = ContextIdentity; } #[derive(Eq, Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize)] -pub struct ContextTransaction {} +pub struct ContextTransaction { + pub context_id: [u8; 32], + pub method: Box, + pub payload: Box<[u8]>, + pub prior_hash: [u8; 32], +} -impl DataType for ContextTransaction { +impl DataType<'_> for ContextTransaction { type Error = io::Error; fn from_slice(slice: Slice) -> Result { @@ -80,5 +92,5 @@ impl DataType for ContextTransaction { } impl PredefinedEntry for key::ContextTransaction { - type DataType = ContextTransaction; + type DataType<'a> = ContextTransaction; } diff --git a/crates/store/src/types/generic.rs b/crates/store/src/types/generic.rs index f1be481b4..64159a782 100644 --- a/crates/store/src/types/generic.rs +++ b/crates/store/src/types/generic.rs @@ -6,20 +6,22 @@ use crate::slice::Slice; use crate::types::PredefinedEntry; #[derive(Eq, Clone, Debug, PartialEq)] -pub struct GenericData(Box<[u8]>); +pub struct GenericData<'a> { + value: Slice<'a>, +} -impl DataType for GenericData { +impl<'a> DataType<'a> for GenericData<'a> { type Error = Infallible; - fn from_slice(slice: Slice) -> Result { - Ok(Self(slice.into_boxed())) + fn from_slice(slice: Slice<'a>) -> Result { + Ok(Self { value: slice }) } - fn as_slice(&self) -> Result { - Ok(self.0.as_ref().into()) + fn as_slice(&'a self) -> Result, Self::Error> { + Ok(self.value.as_ref().into()) } } impl PredefinedEntry for key::Generic { - type DataType = GenericData; + type DataType<'a> = GenericData<'a>; }