#DuckDuckfedi - if I wanted to store a small state file to disk in my Rust app (tiny), what's the most effecient option these days?
Replying to @arch@floofy.tech
Seems like bincode was the go-to for this, and maybe postcard is the best next option?
Not picky, just curious what people usually use.
Replying to @arch@floofy.tech
@arch TBH, JSONMutexDB scales to a few dozen customers before you have to care
Replying to @arch@floofy.tech
@arch how small are we talking? I’m using postcard and serde to store configuration state for my embedded project but there’s no reason you can’t use those in an std environment. It doesn’t get you something easily editable with a text editor, though.
@nepi Two IP addresses and an int, at least for now. Nothing stupid big but I want to keep things small and fast.
Might expand in future of course.
Replying to @arch@floofy.tech
@arch Oh yeah, postcard would be perfect for that. I don’t really have any notes on it, it’s simple and it works for me. And if you use serde you can just change out the format without messing with the struct at all.
Replying to @arch@floofy.tech
@arch@floofy.tech try nanoserde! its like serde, except, well, not as big.
example (forgive me if i misremembered the api)
use nanoserde::{DeBin, SerBin};
use std::fs;
#[derive(SerBin, DeBin, Debug)]
struct Obesity {
falsity: bool,
name: String,
weight: u8,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let x = Obesity {
name: "Obesity".to_string(),
weight: 5,
falsity: true,
};
fs::write("save.dat", x.serialize_bin())?;
let bytes = fs::read("save.dat")?;
let loaded: Obesity = Obesity::deserialize_bin(&bytes)?;
println!("{:?}", loaded);
Ok(())
}Replying to @stag@mk.absturztau.be
@arch@floofy.tech you can also do .serialize_json()and ::deserialize_json() for plaintext

