Why is BitVec is missing the Serialize trait?
Asked Answered
P

1

6

To familiarize myself with Rust, I took it upon myself to write a bloom filter that is backed by a BitVec. Part of that will include a save method that serializes the whole struct using serde and writes it to a file. Unfortunately, I get a compiler error when deriving the Serialize trait:

use bitvec::vec::BitVec;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct BloomFilter {
    n: u64,  // items added
    m: u32,  // slice size
    k: u32,  // number of slices
    buf: BitVec,  // buffer
    state: [u8; 8],  // random state
}
error[E0277]: the trait bound `BitVec: Serialize` is not satisfied
    --> src/bloom.rs:12:10
     |
12   | #[derive(Serialize, Deserialize)]
     |          ^^^^^^^^^ the trait `Serialize` is not implemented for `BitVec`
...
17   |     buf: BitVec,  // buffer
     |     --- required by a bound introduced by this call

My relevant Cargo.toml dependencies:

[dependencies]
bitvec = "1.0.1"
serde = { version = "1.0.196", features = ["derive"] }

This seems odd to me, since the docs for bitvec 1.0.1 mention Serialize (and Deserialize) as one of the traits implemented. Am I making some obvious mistake here?

Piegari answered 13/2 at 21:12 Comment(0)
H
9

A common practice for crates is to put Serialize and Deserialize behind a Cargo feature. Bitvec's features are listed here, and you can enable the serde feature with this command:

cargo add bitvec -F serde

Or you can edit Cargo.toml manually:

[dependencies]
bitvec = { version = "1.0.1", features = ["serde"] }

Right now documentation doesn't let you know when something is behind a feature unless the crate manually indicates it, but hopefully in the future this will be documented automatically.

Healthful answered 13/2 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.