Serialize

Trait Serialize 

Source
pub trait Serialize {
    // Required method
    fn serialize(&self) -> Result<Vec<u8>, PubNubError>;
}
Expand description

Serialize values

This trait provides a serialize method for the Pubnub protocol.

You can implement this trait for your own types, or use the provided implementations for Into<Vec<u8>>.

§Examples

use pubnub::core::{Serialize, PubNubError};

struct Foo {
  bar: String,
}
  
impl Serialize for Foo {
  fn serialize(&self) -> Result<Vec<u8>, PubNubError> {
    Ok(format!("{{\"bar\":\"{}\"}}", self.bar).into_bytes())
  }
}

let bytes = Foo { bar: "baz".into() };
assert_eq!(bytes.serialize().unwrap(), b"{\"bar\":\"baz\"}".to_vec());

Required Methods§

Source

fn serialize(&self) -> Result<Vec<u8>, PubNubError>

Serialize the value

§Errors

Should return an PubNubError::SerializeError if the value cannot be serialized.

§Examples
use pubnub::core::{Serialize, PubNubError};

struct Foo;

impl Serialize for Foo {
   fn serialize(&self) -> Result<Vec<u8>, PubNubError> {
        Ok(vec![1, 2, 3])
   }
}

Implementors§

Source§

impl<S> Serialize for S
where S: Serialize,