Expand description

Types

This module contains a collection of enumerations and traits that are used for converting multiple types, either primitve or user-defined, into Skyhash serializable items.

Implementing a Skyhash serializable type

If you have an object that can be turned into a Vec<u8> or a sequence of Vec<u8> objects, then your type can be serialized by Skyhash (this might change in the future with more types being supported).

Here is a simple example:

use skytable::actions::Actions;
use skytable::types::{IntoSkyhashAction, IntoSkyhashBytes, GetIterator, RawString};
use skytable::Query;

/// Our custom element that adds "cool" to the end of every string when serialized
struct CoolString(String);

impl IntoSkyhashBytes for CoolString {
    fn as_bytes(&self) -> Vec<u8> {
        let mut st = self.0.to_string();
        // add cool
        st.push_str("cool");
        st.into_bytes()
    }
}

/// Our custom sequence of `CoolString` objects
struct CoolStringCollection(Vec<CoolString>);

impl IntoSkyhashAction for CoolStringCollection {
    fn push_into_query(&self, query: &mut Query) {
        self.0.iter().for_each(|item| {
            query.push(RawString::from(item.as_bytes()))
});
    }
    fn incr_len_by(&self) -> usize {
        self.0.len()
    }
}

// And finally implement `GetIterator` for use with some actions that need them

impl GetIterator<CoolString> for CoolStringCollection {
    fn get_iter(&self) -> std::slice::Iter<'_, CoolString> {
        self.0.iter()
    }
}

// You can now directly append your custom element to queries

let mut q = Query::new();
let cool = CoolString(String::from("sayan is "));
q.push(cool);
let other_cools = CoolStringCollection(vec![
    CoolString("ferris is ".to_owned()),
    CoolString("llvm is ".to_owned())
]);
q.push(other_cools);
assert_eq!(q, Query::from(vec!["sayan is cool", "ferris is cool", "llvm is cool"]));

Implementing Skyhash deserializable types

See the guide in FromSkyhashBytes to see how you can implement Skyhash deserializable types.

Structs

A raw string

Enums

Array types

A flat element. This corresponds to the types that can be present in a flat array as defined by the Skyhash protocol

Result of an mksnap action

Traits

Implementing this trait enables Skyhash elements to be converted into Rust types. This trait is already implemented for most primitive types, but for your own custom types, you’ll need to implement it yourself.

Implement this trait for methods in actions that need them. See the module level documentation for more information

Anything that implements this trait can directly add itself to the bytes part of a Query object

Anything that implements this trait can be turned into a String. This trait is implemented for most primitive types by default using std’s ToString trait.