Questions tagged [bincode]
15 questions
6
votes
1 answer
Using Sled, how do I serialize and deserialize?
I'm playing around with the crate sled and attempting a simple serialization and deserialization exercise using bincode just to get a handle on usage.
While I can get insertion to work, attempting to get a range of results seems more difficult. Here…

Russell Myers
- 2,009
- 1
- 15
- 29
3
votes
1 answer
Deserialize Vec with nonstandard length encoding
I am trying to use serde together with bincode to de-serialize an arbitrary bitcoin network message. Given that the payload is handled ubiquitously as a byte array, how do I de-serialize it when the length is unknown at compile-time? bincode does by…

Kevin
- 3,096
- 2
- 8
- 37
3
votes
1 answer
How does serde/bincode serialize byte arrays?
This code serializes an array of 32 bytes exactly as I want:
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
struct Hash([u8; 32]);
let hash = Hash([1u8; 32]);
let hash_bin =…

fadedbee
- 42,671
- 44
- 178
- 308
3
votes
1 answer
Does serde skip attribute actually skip an enum variant?
I have an enum like this one:
#[derive(Debug, Deserialize, Serialize)]
enum E {
A(i32),
#[serde(skip)]
B(bool),
C(char),
D(Vec),
}
Then I try to do the following with bincode crate:
fn main() {
let data = E::C('A');
…

Count Zero
- 495
- 6
- 15
2
votes
1 answer
How to Serialize Enum in Rust with Bincode While Retaining Enum Discriminant Instead of Index?
I've been working on serializing an enum in Rust using bincode, but I am facing an issue where I receive the index of the enum variant instead of its assigned discriminant. Here's an example of the enum I'm trying to serialize:
#[derive(Debug,…

Reed
- 499
- 2
- 8
2
votes
0 answers
Serde bincode custom deserialization scheme
I am trying to implement an advanced serialization/deserialization scheme for a particular struct using serde and bincode crates. To demonstrate, the following code works:
use serde::{Serialize, Deserialize};
use bincode::{DefaultOptions,…

Kevin
- 3,096
- 2
- 8
- 37
2
votes
1 answer
Make serde only produce hex strings for human-readable serialiser?
I'm currently using serde-hex.
use serde_hex::{SerHex,StrictPfx,CompactPfx};
#[derive(Debug,PartialEq,Eq,Serialize,Deserialize)]
struct Foo {
#[serde(with = "SerHex::")]
bar: [u8;4],
#[serde(with = "SerHex::")]
…

fadedbee
- 42,671
- 44
- 178
- 308
2
votes
0 answers
Serialize and deserialize unknown JSON to binary
I'm writing a microservice that takes a json object as input. This json object is only partially known and therefore the struct to which I map it looks like this:
#[derive(Serialize, Deserialize)]
pub struct Incoming {
uri: String,
payload:…

AdaShoelace
- 342
- 1
- 14
1
vote
1 answer
Rust, serde Deserialize and Higher Rank Trait Bounds For<`a>
I am trying to have a deeper understanding of how rust works. I am trying to do some serializing and deserializing to save and load a struct with a generic type. I got it to work, but I don't understand the HRTB and why they made the code…

fudgeBreadstein
- 69
- 5
1
vote
1 answer
Including a None in a bincode deserialization will throw an Error despite being contained in an Option variable
I want to write a struct with corresponding (de)serialization from and to bincode. As a MRE this is the struct:
use serde::{Deserialize, Serialize, Deserializer, Serializer};
use serde_with::skip_serializing_none;
use bincode;
use…

JoL
- 47
- 6
1
vote
1 answer
Why doesn't bincode detect an error if I deserialize data into the wrong type?
Why don't I get an error from bincode when I try to deserialize binary data into the wrong type?
use bincode; // 1.3.1
use serde::{Deserialize, Serialize}; // { version = "1.0", features = ["derive"] }
#[derive(Serialize, Deserialize, Copy, Clone,…

Maksim
- 41
- 1
- 7
1
vote
2 answers
Rust bincode enum binary serialisation
I'm fairly new to Rust and am using bincode in my project. The encoding of enum variants is an issue that I've been trying to deal with as I am interfacing with an existing server. The old project was written in C and the compilier option of…

Nocker
- 189
- 3
- 10
0
votes
1 answer
How to avoid breaking serialized data where enum variants are going to be modified in the future?
Serializing with Bincode, data contains an enum like this:
enum E {
A = 0,
B = 1,
C = 2,
}
The variant E::A was never used, so I thought removing it wouldn't cause a problem:
enum E {
B = 1,
C = 2,
}
Apparently the serializer does…

exlinx
- 19
- 5
0
votes
1 answer
Failed to fill whole buffer with rust read_exact
I have a little code snippet where I'm trying to write struct to a file and then read it. I have seen other similar posts where the asker has forgotten to zero initialise the buffer they are trying to read into. I have made sure not to do this, but…

m.tracey
- 173
- 1
- 10
0
votes
1 answer
Save/load of struct works or fails depending on member vector length
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…

Foveng
- 47
- 5