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?
A common practice for crates is to put
SerializeandDeserializebehind a Cargo feature. Bitvec's features are listed here, and you can enable theserdefeature with this command:Or you can edit Cargo.toml manually:
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.