Skip to content

Commit

Permalink
To bytes (#17)
Browse files Browse the repository at this point in the history
* Bump to 2018 edition

This commit bumps the code to use Rust 2018.

* Update Travis script to use the stable version of rustfmt and clippy

* Introduce to_bytes method

Even tho #2 was closed
with the iter implementation, it was not enought to use with
`bitfield-rle` crate.

The crate takes a `AsRef<[u8]>` to encode, and the iterator returns a
set of `bool`. This code uses the `Bitfield.prototype.toBuffer` logic to
create the `to_bytes` method.

* Use write_all to avoid clippy warnings
  • Loading branch information
bltavares authored Feb 17, 2020
1 parent 9753787 commit f4c97ee
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: rust
rust:
- nightly
- stable

before_script: |
rustup component add rustfmt-preview &&
rustup component add clippy-preview
rustup component add rustfmt &&
rustup component add clippy
script: |
cargo fmt -- --check &&
cargo clippy -- -D clippy::all &&
Expand Down
22 changes: 12 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
[package]
name = "sparse-bitfield"
version = "0.10.0"
license = "MIT OR Apache-2.0"
repository = "https://github.com/datrs/sparse-bitfield"
documentation = "https://docs.rs/sparse-bitfield"
description = "Bitfield that allocates a series of small buffers"
authors = ["Yoshua Wuyts <yoshuawuyts@gmail.com>"]
readme = "README.md"
name = 'sparse-bitfield'
version = '0.10.0'
license = 'MIT OR Apache-2.0'
repository = 'https://github.com/datrs/sparse-bitfield'
documentation = 'https://docs.rs/sparse-bitfield'
description = 'Bitfield that allocates a series of small buffers'
authors = ['Yoshua Wuyts <yoshuawuyts@gmail.com>']
readme = 'README.md'
edition = '2018'

[dependencies]
memory-pager = "0.9.0"
memory-pager = '0.9.0'

[dev-dependencies]
proptest = "0.9.0"
proptest = '0.9.5'
failure = '0.1.6'
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
#![cfg_attr(nightly, doc(include = "../README.md"))]
#![cfg_attr(test, deny(warnings))]

extern crate memory_pager;

mod change;
mod iter;

pub use change::Change;
pub use iter::Iter;
pub use crate::change::Change;
pub use crate::iter::Iter;

use memory_pager::Pager;
use std::fs::File;
Expand Down Expand Up @@ -219,6 +217,25 @@ impl Bitfield {
fn page_mask(&self, index: usize) -> usize {
index & (self.page_size() - 1)
}

/// Based on [Bitfield.prototype.toBuffer](https://github.com/mafintosh/sparse-bitfield/blob/master/index.js#L54-L64)
pub fn to_bytes(&self) -> std::io::Result<Vec<u8>> {
use std::io::{Cursor, Write};

let mut all =
Cursor::new(Vec::with_capacity(self.page_len() * self.page_size()));

for index in 0..self.page_len() {
let next = self.pages.get(index);
if let Some(page) = next {
let all_offset = index * self.page_size();
all.set_position(all_offset as u64);
all.write_all(&page)?;
}
}

Ok(all.into_inner())
}
}

/// Create a new instance with a `page_size` of `1kb`.
Expand Down
4 changes: 1 addition & 3 deletions tests/model.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
extern crate sparse_bitfield;
#[macro_use]
extern crate proptest;
use proptest::proptest;

use sparse_bitfield::{Bitfield, Change};

Expand Down
2 changes: 0 additions & 2 deletions tests/regression.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate sparse_bitfield;

use sparse_bitfield::{Bitfield, Change};

// src/lib.rs::is_even was incorrectly flagging 6 as odd.
Expand Down
23 changes: 20 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
extern crate failure;
extern crate sparse_bitfield;

use failure::Error;
use sparse_bitfield::{Bitfield, Change};
use std::fs;
Expand Down Expand Up @@ -81,6 +78,26 @@ fn can_iterate() {
assert_eq!(arr.len(), 8);
}

#[test]
fn can_convert_to_bytes_buffer() {
let mut bits = Bitfield::new(1024);

assert_eq!(bits.to_bytes().unwrap(), vec![]);

bits.set(0, true);

assert_eq!(
&bits.to_bytes().unwrap(),
&bits.pages.get(0).unwrap().as_ref()
);

bits.set(9000, true);

let mut concat_pages = bits.pages.get(0).unwrap().as_ref().to_vec();
concat_pages.extend_from_slice(&bits.pages.get(1).unwrap().as_ref());
assert_eq!(bits.to_bytes().unwrap(), concat_pages);
}

#[test]
fn from_file() -> Result<(), Error> {
let page_size = 10;
Expand Down

0 comments on commit f4c97ee

Please sign in to comment.