Trait skytable::types::FromSkyhashBytes
source · [−]pub trait FromSkyhashBytes: Sized {
fn from_element(element: Element) -> SkyResult<Self>;
}
Expand description
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.
Example implementation
Say we have a key/value table that stores (str, str)
. The value however contains comma
separated values (CSV). Let’s say it looks like: (Name, E-mail, State)
with types
String
, String
and String
respectively. We’d represent our Rust struct and implement
this trait like this:
use skytable::types::FromSkyhashBytes;
use skytable::error::Error;
use skytable::{SkyResult, Element};
pub struct MyCSV {
name: String,
email: String,
state: String,
}
// Implement it
impl FromSkyhashBytes for MyCSV {
fn from_element(element: Element) -> SkyResult<Self> {
if let Element::String(st) = element {
let splits: Vec<&str> = st.split(",").collect();
Ok(
MyCSV {
name: splits[0].to_string(),
email: splits[1].to_string(),
state: splits[2].to_string(),
}
)
} else {
Err(Error::ParseError("Bad element type".to_string()))
}
}
}
// Now use it with actions!
use skytable::sync::Connection;
use skytable::actions::Actions;
let mut con = Connection::new("127.0.0.1", 2003).unwrap();
let mycsv: MyCSV = con.get("mycsv").unwrap();
Now, you can use this as you like to turn Element
s into your own (or primitive) types or
with actions (as shown above)!
Required methods
fn from_element(element: Element) -> SkyResult<Self>
fn from_element(element: Element) -> SkyResult<Self>
Attempt to convert an element to the target type, returning errors if they occur