Browse Source

Format with latest rustfmt

pull/10/head
Andreas Linz 2 years ago
parent
commit
bedee8087a
Signed by: alinz GPG Key ID: 9BF39809C9DA580C
5 changed files with 37 additions and 36 deletions
  1. +3
    -4
      benches/bench.rs
  2. +1
    -1
      examples/saw.rs
  3. +13
    -17
      src/lib.rs
  4. +1
    -1
      tests/test.rs
  5. +19
    -13
      tests/threads.rs

+ 3
- 4
benches/bench.rs View File

@ -1,15 +1,14 @@
#![feature(test)]
extern crate rand;
extern crate rb;
extern crate test;
extern crate rand;
use rb::{SpscRb, RB, RbProducer, RbConsumer};
use std::thread;
use rand::{Rng, XorShiftRng};
use rb::{RbConsumer, RbProducer, SpscRb, RB};
use std::thread;
use test::Bencher;
#[bench]
/// Benchmark the time it takes to blocking read and write a 1k buffer of f32 elements.
fn bench_passing_a_1k_buffer_blocking(b: &mut Bencher) {

+ 1
- 1
examples/saw.rs View File

@ -1,7 +1,7 @@
extern crate rb;
use std::thread;
use rb::*;
use std::thread;
fn main() {
const SIZE: usize = 128;

+ 13
- 17
src/lib.rs View File

@ -3,8 +3,8 @@ mod tests;
use std::cmp;
use std::fmt;
use std::sync::{Arc, Mutex, Condvar};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
/// Managment interface for the ring buffer.
pub trait RB<T: Clone + Copy + Default> {
@ -286,10 +286,9 @@ impl RbProducer for Producer {
buf[wr_pos..].copy_from_slice(&data[..d]);
buf[..(cnt - d)].copy_from_slice(&data[d..cnt]);
}
self.inspector.write_pos.store(
(wr_pos + cnt) % buf_len,
Ordering::Relaxed,
);
self.inspector
.write_pos
.store((wr_pos + cnt) % buf_len, Ordering::Relaxed);
self.data_available.notify_one();
return Ok(cnt);
@ -317,10 +316,9 @@ impl RbProducer for Producer {
buf[wr_pos..].copy_from_slice(&data[..d]);
buf[..(cnt - d)].copy_from_slice(&data[d..cnt]);
}
self.inspector.write_pos.store(
(wr_pos + cnt) % buf_len,
Ordering::Relaxed,
);
self.inspector
.write_pos
.store((wr_pos + cnt) % buf_len, Ordering::Relaxed);
self.data_available.notify_one();
return Some(cnt);
@ -398,10 +396,9 @@ impl RbConsumer for Consumer {
}
// TODO: Notify all? empty->slots_free
self.inspector.read_pos.store(
(re_pos + cnt) % buf_len,
Ordering::Relaxed,
);
self.inspector
.read_pos
.store((re_pos + cnt) % buf_len, Ordering::Relaxed);
self.slots_free.notify_one();
Ok(cnt)
}
@ -428,10 +425,9 @@ impl RbConsumer for Consumer {
data[d..cnt].copy_from_slice(&buf[..(cnt - d)]);
}
self.inspector.read_pos.store(
(re_pos + cnt) % buf_len,
Ordering::Relaxed,
);
self.inspector
.read_pos
.store((re_pos + cnt) % buf_len, Ordering::Relaxed);
self.slots_free.notify_one();
Some(cnt)
}

+ 1
- 1
tests/test.rs View File

@ -1,6 +1,6 @@
extern crate rb;
use rb::{RB, SpscRb, RbInspector, RbProducer, RbConsumer};
use rb::{RbConsumer, RbInspector, RbProducer, SpscRb, RB};
#[test]
fn test_write() {

+ 19
- 13
tests/threads.rs View File

@ -1,6 +1,6 @@
extern crate rb;
use rb::{RB, SpscRb, RbInspector, RbProducer, RbConsumer};
use rb::{RbConsumer, RbInspector, RbProducer, SpscRb, RB};
use std::thread;
#[test]
@ -14,11 +14,13 @@ fn test_threads() {
let mut out_data = Vec::with_capacity(size);
const WRITE_BUF_SIZE: usize = 32;
thread::spawn(move || for i in 0..(size / WRITE_BUF_SIZE) {
let cnt = producer
.write(&in_data_copy[i * WRITE_BUF_SIZE..(i + 1) * WRITE_BUF_SIZE])
.unwrap();
assert_eq!(cnt, WRITE_BUF_SIZE);
thread::spawn(move || {
for i in 0..(size / WRITE_BUF_SIZE) {
let cnt = producer
.write(&in_data_copy[i * WRITE_BUF_SIZE..(i + 1) * WRITE_BUF_SIZE])
.unwrap();
assert_eq!(cnt, WRITE_BUF_SIZE);
}
});
const READ_BUF_SIZE: usize = 8;
@ -45,11 +47,13 @@ fn test_threads_blocking() {
let mut out_data = Vec::with_capacity(size);
const WRITE_BUF_SIZE: usize = 32;
thread::spawn(move || for i in 0..(size / WRITE_BUF_SIZE) {
let cnt = producer
.write_blocking(&in_data_copy[i * WRITE_BUF_SIZE..(i + 1) * WRITE_BUF_SIZE])
.unwrap();
assert_eq!(cnt, WRITE_BUF_SIZE);
thread::spawn(move || {
for i in 0..(size / WRITE_BUF_SIZE) {
let cnt = producer
.write_blocking(&in_data_copy[i * WRITE_BUF_SIZE..(i + 1) * WRITE_BUF_SIZE])
.unwrap();
assert_eq!(cnt, WRITE_BUF_SIZE);
}
});
const READ_BUF_SIZE: usize = 8;
@ -74,8 +78,10 @@ fn test_threads_count_underflow() {
let consumer = rb.consumer();
let in_data = [0; WRITE_BUF_SIZE];
thread::spawn(move || for _ in 0..ITERATIONS {
producer.write_blocking(&in_data).unwrap();
thread::spawn(move || {
for _ in 0..ITERATIONS {
producer.write_blocking(&in_data).unwrap();
}
});
for _ in 0..ITERATIONS {

Loading…
Cancel
Save