Skip to content

Instantly share code, notes, and snippets.

@vanhtuan0409
Created November 24, 2020 08:52
Show Gist options
  • Save vanhtuan0409/adeb80bc2b90dde37ed20a4a64814106 to your computer and use it in GitHub Desktop.
Save vanhtuan0409/adeb80bc2b90dde37ed20a4a64814106 to your computer and use it in GitHub Desktop.
use serde::{Deserialize, Serialize};
use std::fmt;
use std::io::Cursor;
#[derive(Serialize, Deserialize)]
struct Entry {
key: Vec<u8>,
value: Vec<u8>,
}
impl fmt::Debug for Entry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Entry")
.field("key", &String::from_utf8_lossy(&self.key))
.field("value", &String::from_utf8_lossy(&self.value))
.finish()
}
}
fn get_entry(index: usize) -> Entry {
let key = format!("foo{}", index);
let value = format!("bar{}", index);
Entry {
key: key.as_bytes().to_vec(),
value: value.as_bytes().to_vec(),
}
}
fn main() {
let mut buf = Cursor::new(Vec::new());
for i in 0..10 {
let entry = get_entry(i);
bincode::serialize_into(&mut buf, &entry).unwrap();
}
buf.set_position(0);
while let Ok(decoded) = bincode::deserialize_from::<_, Entry>(&mut buf) {
println!("{:?}", decoded)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment