Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added test to optional serde support pull request #59

Merged
merged 1 commit into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ default = ["std"]
[package.metadata.release]
no-dev-version = true
tag-name = "{{version}}"

[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]
serde_json = "1.0"
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ use core as std;

mod range;

#[cfg(feature = "serde")]
extern crate serde;
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

use std::fmt::Write;
use std::fmt::{Display, Error, Formatter, Binary};

Expand All @@ -51,6 +56,7 @@ fn div_rem(x: usize, d: usize) -> (usize, usize)
/// The bit set has a fixed capacity in terms of enabling bits (and the
/// capacity can grow using the `grow` method).
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct FixedBitSet {
data: Vec<Block>,
/// length in bits
Expand Down Expand Up @@ -1574,3 +1580,15 @@ fn display_trait() {
assert_eq!(format!("{}", fb), "00101000");
assert_eq!(format!("{:#}", fb), "0b00101000");
}

#[test]
#[cfg(feature="serde")]
fn test_serialize() {
let mut fb = FixedBitSet::with_capacity(10);
fb.put(2);
fb.put(3);
fb.put(6);
fb.put(8);
let serialized = serde_json::to_string(&fb).unwrap();
assert_eq!(r#"{"data":[332],"length":10}"#, serialized);
}