RingBuffer

Struct RingBuffer 

Source
pub struct RingBuffer<T: Send + Sized + 'static> { /* private fields */ }
Expand description

Ring buffer itself.

Implementations§

Source§

impl<T: Send + Sized + 'static> RingBuffer<T>

Source

pub fn new(capacity: usize) -> Self

Creates a new instance of a ring buffer.

Examples found in repository?
examples/simple.rs (line 9)
6fn main() {
7    let collector = Collector::new();
8
9    let rb = RingBuffer::<i32>::new(2);
10    let (mut prod, mut cons) = rb.split(&collector.handle());
11
12    prod.push(0).unwrap();
13    prod.push(1).unwrap();
14    assert_eq!(prod.push(2), Err(2));
15
16    assert_eq!(cons.pop().unwrap(), 0);
17
18    prod.push(2).unwrap();
19
20    assert_eq!(cons.pop().unwrap(), 1);
21    assert_eq!(cons.pop().unwrap(), 2);
22    assert_eq!(cons.pop(), None);
23}
More examples
Hide additional examples
examples/message.rs (line 13)
10fn main() {
11    let collector = Collector::new();
12
13    let buf = RingBuffer::<u8>::new(10);
14    let (mut prod, mut cons) = buf.split(&collector.handle());
15
16    let smsg = "The quick brown fox jumps over the lazy dog";
17
18    let pjh = thread::spawn(move || {
19        println!("-> sending message: '{}'", smsg);
20
21        let zero = [0];
22        let mut bytes = smsg.as_bytes().chain(&zero[..]);
23        loop {
24            if prod.is_full() {
25                println!("-> buffer is full, waiting");
26                thread::sleep(Duration::from_millis(1));
27            } else {
28                let n = prod.read_from(&mut bytes, None).unwrap();
29                if n == 0 {
30                    break;
31                }
32                println!("-> {} bytes sent", n);
33            }
34        }
35
36        println!("-> message sent");
37    });
38
39    let cjh = thread::spawn(move || {
40        println!("<- receiving message");
41
42        let mut bytes = Vec::<u8>::new();
43        loop {
44            if cons.is_empty() {
45                if bytes.ends_with(&[0]) {
46                    break;
47                } else {
48                    println!("<- buffer is empty, waiting");
49                    thread::sleep(Duration::from_millis(1));
50                }
51            } else {
52                let n = cons.write_into(&mut bytes, None).unwrap();
53                println!("<- {} bytes received", n);
54            }
55        }
56
57        assert_eq!(bytes.pop().unwrap(), 0);
58        let msg = String::from_utf8(bytes).unwrap();
59        println!("<- message received: '{}'", msg);
60
61        msg
62    });
63
64    pjh.join().unwrap();
65    let rmsg = cjh.join().unwrap();
66
67    assert_eq!(smsg, rmsg);
68}
Source

pub fn split(self, handle: &Handle) -> (Producer<T>, Consumer<T>)

Splits ring buffer into producer and consumer.

Examples found in repository?
examples/simple.rs (line 10)
6fn main() {
7    let collector = Collector::new();
8
9    let rb = RingBuffer::<i32>::new(2);
10    let (mut prod, mut cons) = rb.split(&collector.handle());
11
12    prod.push(0).unwrap();
13    prod.push(1).unwrap();
14    assert_eq!(prod.push(2), Err(2));
15
16    assert_eq!(cons.pop().unwrap(), 0);
17
18    prod.push(2).unwrap();
19
20    assert_eq!(cons.pop().unwrap(), 1);
21    assert_eq!(cons.pop().unwrap(), 2);
22    assert_eq!(cons.pop(), None);
23}
More examples
Hide additional examples
examples/message.rs (line 14)
10fn main() {
11    let collector = Collector::new();
12
13    let buf = RingBuffer::<u8>::new(10);
14    let (mut prod, mut cons) = buf.split(&collector.handle());
15
16    let smsg = "The quick brown fox jumps over the lazy dog";
17
18    let pjh = thread::spawn(move || {
19        println!("-> sending message: '{}'", smsg);
20
21        let zero = [0];
22        let mut bytes = smsg.as_bytes().chain(&zero[..]);
23        loop {
24            if prod.is_full() {
25                println!("-> buffer is full, waiting");
26                thread::sleep(Duration::from_millis(1));
27            } else {
28                let n = prod.read_from(&mut bytes, None).unwrap();
29                if n == 0 {
30                    break;
31                }
32                println!("-> {} bytes sent", n);
33            }
34        }
35
36        println!("-> message sent");
37    });
38
39    let cjh = thread::spawn(move || {
40        println!("<- receiving message");
41
42        let mut bytes = Vec::<u8>::new();
43        loop {
44            if cons.is_empty() {
45                if bytes.ends_with(&[0]) {
46                    break;
47                } else {
48                    println!("<- buffer is empty, waiting");
49                    thread::sleep(Duration::from_millis(1));
50                }
51            } else {
52                let n = cons.write_into(&mut bytes, None).unwrap();
53                println!("<- {} bytes received", n);
54            }
55        }
56
57        assert_eq!(bytes.pop().unwrap(), 0);
58        let msg = String::from_utf8(bytes).unwrap();
59        println!("<- message received: '{}'", msg);
60
61        msg
62    });
63
64    pjh.join().unwrap();
65    let rmsg = cjh.join().unwrap();
66
67    assert_eq!(smsg, rmsg);
68}
Source

pub fn capacity(&self) -> usize

Returns capacity of the ring buffer.

Source

pub fn is_empty(&self) -> bool

Checks if the ring buffer is empty.

Source

pub fn is_full(&self) -> bool

Checks if the ring buffer is full.

Source

pub fn len(&self) -> usize

The length of the data in the buffer.

Source

pub fn remaining(&self) -> usize

The remaining space in the buffer.

Trait Implementations§

Source§

impl<T: Send + Sized + 'static> Drop for RingBuffer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for RingBuffer<T>

§

impl<T> !RefUnwindSafe for RingBuffer<T>

§

impl<T> Send for RingBuffer<T>

§

impl<T> Sync for RingBuffer<T>

§

impl<T> Unpin for RingBuffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for RingBuffer<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.