Trait Encode

Source
pub trait Encode {
    // Required method
    fn encode(&self, buf: &mut Vec<u8>);

    // Provided method
    fn stringify(&self) -> String { ... }
}
Expand description

Encode Trait

This trait is used for formatting a field’s value. Encoding support has already been added for all scalar types and many standard collections. If you wish to format your own type, just implement this trait.

pub struct CustomStruct {
    pub key1: i32,
    pub key2: bool,
    pub key3: String,
}

impl logkit::Encode for CustomStruct {
    #[inline]
    fn encode(&self, buf: &mut Vec<u8>) {
        // format your struct into buf
        unimplemented!()
    }
}

The Encode Trait

Required Methods§

Source

fn encode(&self, buf: &mut Vec<u8>)

Encode object to bytes and append it to buf

Provided Methods§

Source

fn stringify(&self) -> String

Encode object to string

Implementations on Foreign Types§

Source§

impl Encode for &str

Encode Str

use encoder::json::Encode;
 
assert_eq!("Hello".stringify(), r#""Hello""#);
assert_eq!("Bonjour".stringify(), r#""Bonjour""#);
assert_eq!("你好".stringify(), r#""你好""#);
assert_eq!("こんにちは".stringify(), r#""こんにちは""#);
assert_eq!("สวัสดี".stringify(), r#""สวัสดี""#);
 
let mut buf = vec![];
"Hello".encode(&mut buf);
"Bonjour".encode(&mut buf);
"你好".encode(&mut buf);
"こんにちは".encode(&mut buf);
"สวัสดี".encode(&mut buf);
assert_eq!(String::from_utf8_lossy(&buf), r#""Hello""Bonjour""你好""こんにちは""สวัสดี""#);
Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for Option<&dyn Encode>

Encode Option 2

use encoder::json::Encode;
 
let mut buf = vec![];
let mut opt: Option<&dyn Encode> = Some(&1);
opt.encode(&mut buf);
opt = None;
opt.encode(&mut buf);
assert_eq!(opt.stringify(), r#"null"#);
assert_eq!(String::from_utf8_lossy(&buf), r#"1null"#);
Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for bool

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for char

Encode Char

use encoder::json::Encode;
 
assert_eq!('a'.stringify(), r#""a""#);
assert_eq!('b'.stringify(), r#""b""#);
assert_eq!('c'.stringify(), r#""c""#);
 
let mut buf = vec![];
'a'.encode(&mut buf);
'b'.encode(&mut buf);
'c'.encode(&mut buf);
assert_eq!(String::from_utf8_lossy(&buf), r#""a""b""c""#);
Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for f32

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for f64

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for i8

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for i16

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for i32

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for i64

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for i128

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for isize

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for u8

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for u16

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for u32

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for u64

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for u128

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for usize

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for BTreeSet<&dyn Encode>

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for VecDeque<&dyn Encode>

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for String

Encode String

use encoder::json::Encode;
 
assert_eq!(String::from("\"\\/\n\r\t").stringify(), r#""\"\\/\n\r\t""#);
 
let mut buf = vec![];
String::from("\"\\/\n\r\t").encode(&mut buf);
assert_eq!(String::from_utf8_lossy(&buf), r#""\"\\/\n\r\t""#);
Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for Vec<&dyn Encode>

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for HashSet<&dyn Encode>

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for IndexSet<&dyn Encode>

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl Encode for [&dyn Encode]

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<K> Encode for BTreeMap<K, &dyn Encode>
where K: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<K> Encode for HashMap<K, &dyn Encode>
where K: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<K> Encode for IndexMap<K, &dyn Encode>
where K: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<K, V> Encode for BTreeMap<K, V>
where K: Encode, V: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<K, V> Encode for HashMap<K, V>
where K: Encode, V: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<K, V> Encode for IndexMap<K, V>
where K: Encode, V: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<T> Encode for Option<T>
where T: Encode,

Encode Option 1

use encoder::json::Encode;
 
let mut buf = vec![];
let mut opt = Some(1);
opt.encode(&mut buf);
opt = None;
opt.encode(&mut buf);
assert_eq!(opt.stringify(), r#"null"#);
assert_eq!(String::from_utf8_lossy(&buf), r#"1null"#);
Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<T> Encode for [T]
where T: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<T> Encode for BTreeSet<T>
where T: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<T> Encode for VecDeque<T>
where T: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<T> Encode for Vec<T>
where T: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<T> Encode for HashSet<T>
where T: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Source§

impl<T> Encode for IndexSet<T>
where T: Encode,

Source§

fn encode(&self, buf: &mut Vec<u8>)

Implementors§