Function rust_multicodec::encode [] [src]

pub fn encode<T: Serialize>(
    codec: CodecType,
    object: &T
) -> Result<Vec<u8>, String>

Returns the encoded object with a prefix of the given codec. Note that the object must implement Serialize trait from serde's lib

Arguments

  • codec - The codec type, eg. CodecType::JSON
  • object - the object reference to be encoded

Example

extern crate rust_multicodec;
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[derive(Debug)]
struct Person {
    name: String
}

fn main(){
    let to_be_encoded=Person{name:String::from("sanyi")};
    println!("{:?}",rust_multicodec::encode(rust_multicodec::codec::CodecType::JSON, &to_be_encoded));
    // it will print: Ok([129, 30, 123, 34, 110, 97, 109, 101, 34, 58, 34, 115, 97, 110, 121, 105, 34, 125])
}