Skip to content

Cleanup to make clippy and rustfmt happy #29

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
16 changes: 8 additions & 8 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn bench_chan_threaded(b: &mut Bencher) {

let flag_clone = arc_flag.clone();
thread::spawn(move || {
while flag_clone.load(Ordering::Acquire) == false {
while !flag_clone.load(Ordering::Acquire) {
// Try to do as much work as possible without checking the atomic
for _ in 0..400 {
rx.recv().unwrap();
Expand All @@ -70,7 +70,7 @@ fn bench_chan_threaded(b: &mut Bencher) {

b.iter(|| tx.send(1));

let flag_clone = arc_flag.clone();
let flag_clone = arc_flag;
flag_clone.store(true, Ordering::Release);

// We have to loop a minimum of 400 times to guarantee the other thread shuts down
Expand All @@ -86,7 +86,7 @@ fn bench_chan_threaded2(b: &mut Bencher) {

let flag_clone = arc_flag.clone();
thread::spawn(move || {
while flag_clone.load(Ordering::Acquire) == false {
while !flag_clone.load(Ordering::Acquire) {
// Try to do as much work as possible without checking the atomic
for _ in 0..400 {
let _ = tx.send(1);
Expand All @@ -96,7 +96,7 @@ fn bench_chan_threaded2(b: &mut Bencher) {

b.iter(|| rx.recv().unwrap());

let flag_clone = arc_flag.clone();
let flag_clone = arc_flag;
flag_clone.store(true, Ordering::Release);

// We have to loop a minimum of 400 times to guarantee the other thread shuts down
Expand All @@ -122,7 +122,7 @@ fn bench_spsc_threaded(b: &mut Bencher) {

let flag_clone = arc_flag.clone();
thread::spawn(move || {
while flag_clone.load(Ordering::Acquire) == false {
while !flag_clone.load(Ordering::Acquire) {
// Try to do as much work as possible without checking the atomic
for _ in 0..400 {
c.pop();
Expand All @@ -132,7 +132,7 @@ fn bench_spsc_threaded(b: &mut Bencher) {

b.iter(|| p.push(1));

let flag_clone = arc_flag.clone();
let flag_clone = arc_flag;
flag_clone.store(true, Ordering::Release);

// We have to loop a minimum of 400 times to guarantee the other thread shuts down
Expand All @@ -149,7 +149,7 @@ fn bench_spsc_threaded2(b: &mut Bencher) {

let flag_clone = arc_flag.clone();
thread::spawn(move || {
while flag_clone.load(Ordering::Acquire) == false {
while !flag_clone.load(Ordering::Acquire) {
// Try to do as much work as possible without checking the atomic
for _ in 0..400 {
p.push(1);
Expand All @@ -159,7 +159,7 @@ fn bench_spsc_threaded2(b: &mut Bencher) {

b.iter(|| c.pop());

let flag_clone = arc_flag.clone();
let flag_clone = arc_flag;
flag_clone.store(true, Ordering::Release);

// We have to loop a minimum of 400 times to guarantee the other thread shuts down
Expand Down
38 changes: 16 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ impl<T> Buffer<T> {
/// buffer wrapping is handled inside the method.
#[inline]
unsafe fn load(&self, pos: usize) -> &T {
&*self.buffer
.offset((pos & (self.allocated_size - 1)) as isize)
&*self.buffer.add(pos & (self.allocated_size - 1))
}

/// Store a value in the buffer
Expand All @@ -228,8 +227,7 @@ impl<T> Buffer<T> {
/// - Initialized a valid block of memory
#[inline]
unsafe fn store(&self, pos: usize, v: T) {
let end = self.buffer
.offset((pos & (self.allocated_size - 1)) as isize);
let end = self.buffer.add(pos & (self.allocated_size - 1));
ptr::write(&mut *end, v);
}
}
Expand All @@ -242,13 +240,14 @@ impl<T> Drop for Buffer<T> {

// TODO this could be optimized to avoid the atomic operations / book-keeping...but
// since this is the destructor, there shouldn't be any contention... so meh?
while let Some(_) = self.try_pop() {}
while self.try_pop().is_some() {}

unsafe {
let layout = Layout::from_size_align(
self.allocated_size * mem::size_of::<T>(),
mem::align_of::<T>(),
).unwrap();
)
.unwrap();
alloc::dealloc(self.buffer as *mut u8, layout);
}
}
Expand Down Expand Up @@ -328,9 +327,7 @@ pub fn make<T>(capacity: usize) -> (Producer<T>, Consumer<T>) {
Producer {
buffer: arc.clone(),
},
Consumer {
buffer: arc.clone(),
},
Consumer { buffer: arc },
)
}

Expand Down Expand Up @@ -406,7 +403,7 @@ impl<T> Producer<T> {
/// assert!(producer.capacity() == 100);
/// ```
pub fn capacity(&self) -> usize {
(*self.buffer).capacity
self.buffer.capacity
}

/// Returns the current size of the queue
Expand All @@ -424,7 +421,7 @@ impl<T> Producer<T> {
/// assert!(producer.size() == 1);
/// ```
pub fn size(&self) -> usize {
(*self.buffer).tail.load(Ordering::Acquire) - (*self.buffer).head.load(Ordering::Acquire)
self.buffer.tail.load(Ordering::Acquire) - self.buffer.head.load(Ordering::Acquire)
}

/// Returns the available space in the queue
Expand Down Expand Up @@ -528,7 +525,7 @@ impl<T> Consumer<T> {
/// assert!(producer.capacity() == 100);
/// ```
pub fn capacity(&self) -> usize {
(*self.buffer).capacity
self.buffer.capacity
}

/// Returns the current size of the queue
Expand All @@ -548,7 +545,7 @@ impl<T> Consumer<T> {
/// assert!(producer.size() == 9);
/// ```
pub fn size(&self) -> usize {
(*self.buffer).tail.load(Ordering::Acquire) - (*self.buffer).head.load(Ordering::Acquire)
self.buffer.tail.load(Ordering::Acquire) - self.buffer.head.load(Ordering::Acquire)
}
}

Expand Down Expand Up @@ -644,29 +641,27 @@ mod tests {
Some(v) => {
assert!(v == 10);
}
None => assert!(false, "Queue should not have accepted another write!"),
None => panic!("Queue should not have accepted another write!"),
}
}

#[test]
fn test_try_poll() {
let (p, c) = super::make(10);

match c.try_pop() {
Some(_) => assert!(false, "Queue was empty but a value was read!"),
None => {}
if c.try_pop().is_some() {
panic!("Queue was empty but a value was read!");
}

p.push(123);

match c.try_pop() {
Some(v) => assert!(v == 123),
None => assert!(false, "Queue was not empty but poll() returned nothing!"),
None => panic!("Queue was not empty but poll() returned nothing!"),
}

match c.try_pop() {
Some(_) => assert!(false, "Queue was empty but a value was read!"),
None => {}
if c.try_pop().is_some() {
panic!("Queue was empty but a value was read!");
}
}

Expand Down Expand Up @@ -737,5 +732,4 @@ mod tests {
(start.to(end)).num_nanoseconds().unwrap()
);
}

}