I have issue when read a bincode object from wal file

hi,
I want write struct in file and read from it

use std::{time::Instant};
use serde_derive::{Serialize, Deserialize};
use simple_wal::LogFile;



fn main() {

    let mut log = LogFile::open("./tab-seg-1").unwrap();
    // ------------------------------------------------



    let now = Instant::now();
    // ============================================

    for elem in 0..1 {
        let mut binobj = object_factory();
        if let Err(e) = log.write(&mut binobj) {
            panic!("{}", e.to_string());
        }
        let _ = log.flush();
    }
    
    log.iter(..).unwrap()
        .for_each(|line| {
            match line {
                Ok(bin_obj) => {
                    let res: Option<Player> = bincode::deserialize_from(&*bin_obj).unwrap();
                    let bo = res.unwrap();
                }
                Err(err) => {
                    panic!("{}", err.to_string())
                }
            }
        });

    // ============================================
    let nnow = Instant::now();
    println!("==> {:?}", nnow.duration_since(now));

}






#[derive(Serialize, Deserialize)]
struct Player {
    phoneid: String,
    age: i32,
    fullname: String

}


fn object_factory() -> Vec<u8> {
    let pl = Player {
        phoneid: "09370156893".to_owned(),
        age: 24,
        fullname: "DanyalMh".to_owned(),
    };

    bincode::serialize(&pl).unwrap()
}

but result is :

thread 'main' panicked at 'called Result::unwrap() on an Err value: InvalidTagEncoding(11)', src/main.rs:29:84
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

You are serializing a Player, but trying to deserialize an Option<Player>. You need to use the same type for serialization and deserialization when using bincode.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.