Skip to content

Commit

Permalink
Implement Key, Value traits for String, SizedString (#238)
Browse files Browse the repository at this point in the history
* Implement Key, Value traits for String, SizedString
  • Loading branch information
yanganto committed Oct 15, 2021
1 parent 1aed34c commit 456d386
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion SewUp.wiki
Submodule SewUp.wiki updated from 663e75 to 5f3ac3
31 changes: 31 additions & 0 deletions sewup/src/kv/traits/key.rs
Expand Up @@ -161,3 +161,34 @@ macro_rules! primitive_key {
}

primitive_key!(u8, u16, u32, u64, usize);

impl Key for String {
fn from_row_key(x: &Row) -> Result<Self> {
Ok(x.to_utf8_string()?)
}
fn to_row_key(&self) -> Result<Row> {
Ok(self.into())
}
}

macro_rules! sized_string_key {
( $($n:expr),* ) => {
$(
impl Key for [Raw; $n] {
fn from_row_key(r: &Row) -> Result<Self> {
let mut buffer: [Raw; $n] = Default::default();
buffer.copy_from_slice(&r.inner[0..$n]);
Ok(buffer)
}
fn to_row_key(&self) -> Result<Row> {
Ok(self.to_vec().into())
}
}
)*
}
}

sized_string_key!(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32
);
33 changes: 32 additions & 1 deletion sewup/src/kv/traits/value.rs
Expand Up @@ -112,7 +112,7 @@ macro_rules! primitive_value {
}

fn from_row_value(row: &Row) -> Result<Self> {
let r: Raw = TryFrom::try_from(row).expect("primitive key should be 1 Raw");
let r: Raw = TryFrom::try_from(row).expect("primitive value should be 1 Raw");
Ok(r.into())
}
}
Expand All @@ -121,3 +121,34 @@ macro_rules! primitive_value {
}

primitive_value!(u8, u16, u32, u64, usize);

impl Value for String {
fn to_row_value(&self) -> Result<Row> {
Ok(self.into())
}
fn from_row_value(row: &Row) -> Result<Self> {
Ok(row.to_utf8_string()?)
}
}

macro_rules! sized_string_value {
( $($n:expr),* ) => {
$(
impl Value for [Raw; $n] {
fn to_row_value(&self) -> Result<Row> {
Ok(self.to_vec().into())
}
fn from_row_value(row: &Row) -> Result<Self> {
let mut buffer: [Raw; $n] = Default::default();
buffer.copy_from_slice(&row.inner[0..$n]);
Ok(buffer)
}
}
)*
}
}

sized_string_value!(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32
);
27 changes: 26 additions & 1 deletion sewup/src/types/row.rs
Expand Up @@ -7,7 +7,7 @@ use crate::types::*;
/// A list of `Raw`, which helps you store much bigger data than a `Raw`
#[derive(Clone, Serialize, Deserialize)]
pub struct Row {
pub(super) inner: Vec<Raw>,
pub(crate) inner: Vec<Raw>,
_buffer: Vec<u8>,
}

Expand Down Expand Up @@ -219,3 +219,28 @@ impl std::borrow::Borrow<[u8]> for &Row {
self._buffer.as_ref()
}
}

macro_rules! from_array {
($($n:expr),*) => {
$(
impl From<&[Raw; $n]> for Row {
fn from(v: &[Raw; $n]) -> Self {
Self::from(v.to_vec())
}
}

impl Into<[Raw; $n]> for Row {
fn into(self) -> [Raw; $n]{
let mut buffer : [Raw; $n] = Default::default();
buffer.copy_from_slice(&self.inner[0..$n]);
buffer
}
}
)*
}
}

from_array!(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32
);

0 comments on commit 456d386

Please sign in to comment.