-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoding: add
Key
and Value
traits with decode/encode default impls
- Loading branch information
1 parent
763caa8
commit d204a21
Showing
12 changed files
with
152 additions
and
130 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
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,64 @@ | ||
//! Binary data encodings. | ||
//! | ||
//! - keycode: used for keys in the key/value store. | ||
//! - bincode: used for values in the key/value store and network protocols. | ||
//! * keycode: used for keys in the key/value store. | ||
//! * bincode: used for values in the key/value store and network protocols. | ||
|
||
pub mod bincode; | ||
pub mod keycode; | ||
|
||
use crate::error::Result; | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
|
||
/// Adds automatic Keycode encode/decode methods to key enums. These are | ||
/// primarily meant for keys stored in key/value storage engines. | ||
/// | ||
/// TODO: consider making this DeserializeOwned instead. | ||
pub trait Key<'de>: Serialize + Deserialize<'de> { | ||
/// Decodes a key from a byte slice using Keycode. | ||
fn decode(bytes: &'de [u8]) -> Result<Self> { | ||
keycode::deserialize(bytes) | ||
} | ||
|
||
/// Encodes a key to a byte vector using Keycode. | ||
fn encode(&self) -> Result<Vec<u8>> { | ||
keycode::serialize(self) | ||
} | ||
} | ||
|
||
/// Adds automatic Bincode encode/decode methods to value types. These are used | ||
/// not only for values in key/value storage engines, but also for e.g. network | ||
/// protocol messages and other values. | ||
pub trait Value: Serialize + DeserializeOwned { | ||
/// Decodes a value from a byte slice using Bincode. | ||
fn decode(bytes: &[u8]) -> Result<Self> { | ||
bincode::deserialize(bytes) | ||
} | ||
|
||
/// Decodes a value from a reader using Bincode. | ||
fn decode_from<R: std::io::Read>(reader: R) -> Result<Self> { | ||
bincode::deserialize_from(reader) | ||
} | ||
|
||
/// Decodes a value from a reader using Bincode, or returns None if the | ||
/// reader is closed. | ||
fn maybe_decode_from<R: std::io::Read>(reader: R) -> Result<Option<Self>> { | ||
bincode::maybe_deserialize_from(reader) | ||
} | ||
|
||
/// Encodes a value to a byte vector using Bincode. | ||
fn encode(&self) -> Result<Vec<u8>> { | ||
bincode::serialize(self) | ||
} | ||
|
||
/// Encodes a value into a writer using Bincode. | ||
fn encode_into<W: std::io::Write>(&self, writer: W) -> Result<()> { | ||
bincode::serialize_into(writer, self) | ||
} | ||
} | ||
|
||
/// Blanket implementations for various types wrapping a value type. | ||
impl<V: Value + std::cmp::Eq + std::hash::Hash> Value for std::collections::HashSet<V> {} | ||
impl<V: Value> Value for Option<V> {} | ||
impl<V: Value> Value for Result<V> {} | ||
impl<V: Value> Value for Vec<V> {} | ||
impl<V: Value + std::cmp::Eq + std::hash::Hash> Value for Vec<(V, std::collections::HashSet<V>)> {} |
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
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.