I have defined the following structs with custom load/save methods using serde and bincode:
use std::{
fs::File,
io::{Read, Seek, SeekFrom, Write},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Histogram {
pub bars: Vec<f64>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Histograms {
pub num_bars: usize,
pub num_histograms: usize,
pub vec: Vec<Histogram>,
}
fn main() {
println!("Hello, world!");
}
impl Histogram {
pub fn new(num_bars: usize) -> Histogram {
Histogram {
bars: vec![0.0; num_bars],
}
}
}
impl Histograms {
pub fn new(num_histograms: usize, num_bars: usize) -> Histograms {
let histograms = Histograms {
vec: vec![Histogram::new(num_bars); num_histograms],
num_histograms,
num_bars,
};
histograms
}
pub fn save(&self, filename: &str) {
let mut file = File::create(format!("{}{}", "path", filename)).unwrap();
let bin = bincode::serialize(&self).unwrap();
Write::write_all(&mut file, &bin).unwrap();
}
pub fn load(filename: &str) -> Histograms {
let mut file = File::open(format!("{}{}", "path", filename)).unwrap();
let mut vec: Vec<u8> = vec![0; file.seek(SeekFrom::End(0)).unwrap() as usize];
file.seek(SeekFrom::Start(0)).unwrap();
file.read(&mut vec).unwrap();
let result: Histograms = bincode::deserialize(&vec).unwrap();
result
}
}
The strange thing now is, that the following test shows me that save/load works properly if the length of the vec(member of histograms) is small, but it fails(i don't get any error, just the resulting histograms instance is wrong) with large values like for instance 10000000. Precisely i get a value of 5263431 from where on it is not correct anymore.
mod tests {
use core::panic;
use super::*;
#[test]
fn save_load_test() {
let histogramms = Histograms::new(10000000, 50);
histogramms.save("debug_test");
let histogramms1 = Histograms::load("debug_test");
assert_eq!(histogramms.vec.len(), histogramms1.vec.len());
let mut failed = false;
for i in 0..histogramms.vec.len() {
if histogramms.vec[i].bars.len() != histogramms1.vec[i].bars.len() {
failed = true;
println!("first i that failed: {}", i);
println!(
"histogramms.vec.bars.len: {} histogramms1.vec.bars.len: {}",
histogramms.vec[i].bars.len(),
histogramms1.vec[i].bars.len()
);
break;
}
}
if failed {
panic!()
}
}
}
Any ideas what is going wrong?