From 126aa33b9c97a870a6756276f4034c1fdc05f370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kuras?= Date: Fri, 17 Sep 2021 13:34:29 +0200 Subject: [PATCH 1/2] Added implementation of timestamp key --- packages/storage-plus/src/keys.rs | 41 +++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/storage-plus/src/keys.rs b/packages/storage-plus/src/keys.rs index 9bb5a8742..05d43dbbf 100644 --- a/packages/storage-plus/src/keys.rs +++ b/packages/storage-plus/src/keys.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::Addr; +use cosmwasm_std::{Addr, Timestamp}; use std::marker::PhantomData; use crate::helpers::namespaces_with_key; @@ -250,7 +250,8 @@ impl From for IntKey { impl From> for IntKey { fn from(wrap: Vec) -> Self { - // TODO: assert proper length + assert_eq!(wrap.len(), std::mem::size_of::()); + IntKey { wrapped: wrap, data: PhantomData, @@ -264,6 +265,42 @@ impl From> for Vec { } } +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct TimestampKey(U64Key); + +impl TimestampKey { + pub fn new(ts: Timestamp) -> Self { + Self(ts.nanos().into()) + } +} + +impl<'a> PrimaryKey<'a> for TimestampKey { + type Prefix = (); + type SubPrefix = (); + + fn key(&self) -> Vec<&[u8]> { + self.0.key() + } +} + +impl<'a> Prefixer<'a> for TimestampKey { + fn prefix(&self) -> Vec<&[u8]> { + self.0.key() + } +} + +impl From> for TimestampKey { + fn from(val: Vec) -> Self { + Self(val.into()) + } +} + +impl From for TimestampKey { + fn from(val: Timestamp) -> Self { + Self::new(val) + } +} + #[cfg(test)] mod test { use super::*; From 67cad494f13f26a3b95f28c4efa0d9823626493c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kuras?= Date: Mon, 20 Sep 2021 10:04:11 +0200 Subject: [PATCH 2/2] Remove assertion --- packages/storage-plus/src/keys.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/storage-plus/src/keys.rs b/packages/storage-plus/src/keys.rs index 05d43dbbf..4c4f2a515 100644 --- a/packages/storage-plus/src/keys.rs +++ b/packages/storage-plus/src/keys.rs @@ -250,8 +250,8 @@ impl From for IntKey { impl From> for IntKey { fn from(wrap: Vec) -> Self { - assert_eq!(wrap.len(), std::mem::size_of::()); - + // TODO: Consider properly handling case, when `wrap` has length not conforming for the + // wrapped integer type. IntKey { wrapped: wrap, data: PhantomData,