-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Update storage.rs * Create storage_hash_map_key_value.rs * Update storage_hash_map_key_value.rs * Update pr.yaml
- Loading branch information
1 parent
a9cc7c6
commit 929c664
Showing
3 changed files
with
59 additions
and
1 deletion.
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
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,57 @@ | ||
use super::serialize::Serialize; | ||
|
||
#[derive(Debug, Default, PartialEq)] | ||
pub(crate) struct StorageHashMapKeyValue<K: Serialize, T: Serialize> { | ||
key: K, | ||
value: T, | ||
} | ||
|
||
impl<K: Serialize, T: Serialize> Serialize for StorageHashMapKeyValue<K, T> { | ||
fn deserialize(bytes: &[u8]) -> Result<Self, crate::DbError> { | ||
Ok(Self { | ||
key: K::deserialize(&bytes[0..])?, | ||
value: T::deserialize(&bytes[std::mem::size_of::<K>()..])?, | ||
}) | ||
} | ||
|
||
fn serialize(&self) -> Vec<u8> { | ||
let mut data = Vec::<u8>::new(); | ||
data.reserve(std::mem::size_of::<K>() + std::mem::size_of::<T>()); | ||
data.append(&mut self.key.serialize()); | ||
data.append(&mut self.value.serialize()); | ||
|
||
data | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn derived_from_debug() { | ||
let key_value = StorageHashMapKeyValue::<i64, i64>::default(); | ||
|
||
format!("{:?}", key_value); | ||
} | ||
|
||
#[test] | ||
fn derived_from_default() { | ||
let key_value = StorageHashMapKeyValue::<i64, i64>::default(); | ||
|
||
assert_eq!(key_value.key, 0); | ||
assert_eq!(key_value.value, 0); | ||
} | ||
|
||
#[test] | ||
fn i64_i64() { | ||
let key_value = StorageHashMapKeyValue { | ||
key: 1_i64, | ||
value: 10_i64, | ||
}; | ||
let bytes = key_value.serialize(); | ||
let other = StorageHashMapKeyValue::deserialize(&bytes); | ||
|
||
assert_eq!(other, Ok(key_value)); | ||
} | ||
} |