Skip to content

Fix cargo error in bit-set crate by implementing own minimal BitSet #45

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ readme = "README.md"
keywords = ["usb", "libusb", "hardware", "bindings"]

[dependencies]
bit-set = "0.2.0"
libusb-sys = "0.2.3"
libc = "0.2"

Expand Down
158 changes: 158 additions & 0 deletions src/bit_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
type Item = u32;
const ITEM_BITS: usize = Item::BITS as usize;

#[derive(Clone)]
pub struct BitSet {
nbits: usize,
storage: Vec<Item>,
}

impl<'a> BitSet {
#[inline]
pub fn with_capacity(nbits: usize) -> BitSet {
return BitSet {
nbits,
storage: vec![0 as Item; (nbits + ITEM_BITS - 1) / ITEM_BITS],
};
}

#[inline]
pub fn contains(&self, value: usize) -> ::Result<bool> {
if value < self.nbits {
Ok(self.storage[value / ITEM_BITS] & (1 as Item) << (value % ITEM_BITS) != 0)
} else {
Err(::Error::Overflow)
}
}

#[inline]
pub fn insert(&mut self, value: usize) -> ::Result<()> {
if !self.contains(value)? {
self.storage[value / ITEM_BITS] |= (1 as Item) << (value % ITEM_BITS);
}
Ok(())
}

#[inline]
pub fn remove(&mut self, value: usize) -> ::Result<()> {
if self.contains(value)? {
self.storage[value / ITEM_BITS] &= !((1 as Item) << (value % ITEM_BITS));
}
Ok(())
}

#[inline]
pub fn iter(&'a self) -> Iter<'a> {
return Iter {
data: &self.storage,
offset: 0,
block: self.storage[0],
};
}
}

#[derive(Clone)]
pub struct Iter<'a> {
data: &'a Vec<Item>,
offset: usize,
block: Item,
}

impl<'a> Iterator for Iter<'a> {
type Item = usize;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
while self.block != 0 {
let bit = self.block.trailing_zeros() as Self::Item;
self.block &= self.block - 1;
return Some(self.offset + bit);
}

self.offset += ITEM_BITS;

if self.offset < self.data.len() * ITEM_BITS {
self.block = self.data[self.offset / ITEM_BITS];
} else {
break;
}
}

None
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_checks_capacity() {
let mut set = BitSet::with_capacity(256);

assert!(matches!(set.contains(123), Ok(false)));
assert!(matches!(set.contains(456), Err(::Error::Overflow)));

assert!(matches!(set.insert(123), Ok(_)));
assert!(matches!(set.insert(456), Err(::Error::Overflow)));

assert!(matches!(set.remove(123), Ok(_)));
assert!(matches!(set.remove(456), Err(::Error::Overflow)));
}

#[test]
fn it_inserts() {
let mut set = BitSet::with_capacity(256);

assert!(matches!(set.contains(123), Ok(false)));
assert!(matches!(set.insert(123), Ok(())));
assert!(matches!(set.contains(123), Ok(true)));
}

#[test]
fn it_inserts_existing() {
let mut set = BitSet::with_capacity(256);
set.insert(123).unwrap();

assert!(matches!(set.contains(123), Ok(true)));
assert!(matches!(set.insert(123), Ok(())));
assert!(matches!(set.contains(123), Ok(true)));
}

#[test]
fn it_removes() {
let mut set = BitSet::with_capacity(256);
set.insert(123).unwrap();

assert!(matches!(set.contains(123), Ok(true)));
assert!(matches!(set.remove(123), Ok(())));
assert!(matches!(set.contains(123), Ok(false)));
}

#[test]
fn it_removes_nonexistent() {
let mut set = BitSet::with_capacity(256);

assert!(matches!(set.contains(123), Ok(false)));
assert!(matches!(set.remove(123), Ok(())));
assert!(matches!(set.contains(123), Ok(false)));
}

#[test]
fn it_iterates() {
let mut set = BitSet::with_capacity(256);

let data: Vec<usize> = set.iter().collect();
assert_eq!(data, []);

set.insert(1).unwrap();
set.insert(12).unwrap();
set.insert(123).unwrap();
set.insert(255).unwrap();
set.insert(512).unwrap_err();

let data: Vec<usize> = set.iter().collect();
assert_eq!(data, [1, 12, 123, 255]);
}
}
4 changes: 2 additions & 2 deletions src/device_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ impl<'a> DeviceHandle<'a> {
/// when the device handle goes out of scope.
pub fn claim_interface(&mut self, iface: u8) -> ::Result<()> {
try_unsafe!(libusb_claim_interface(self.handle, iface as c_int));
self.interfaces.insert(iface as usize);
self.interfaces.insert(iface as usize)?;
Ok(())
}

/// Releases a claimed interface.
pub fn release_interface(&mut self, iface: u8) -> ::Result<()> {
try_unsafe!(libusb_release_interface(self.handle, iface as c_int));
self.interfaces.remove(&(iface as usize));
self.interfaces.remove(iface as usize)?;
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! This crate provides a safe wrapper around the native `libusb` library.

extern crate bit_set;
extern crate libusb_sys as libusb;
extern crate libc;

Expand Down Expand Up @@ -39,3 +38,5 @@ mod config_descriptor;
mod interface_descriptor;
mod endpoint_descriptor;
mod language;

mod bit_set;