Created
November 24, 2020 08:52
-
-
Save vanhtuan0409/adeb80bc2b90dde37ed20a4a64814106 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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