pub struct RingBuffer<T: Send + Sized + 'static> { /* private fields */ }
Expand description
Ring buffer itself.
Implementations§
Source§impl<T: Send + Sized + 'static> RingBuffer<T>
impl<T: Send + Sized + 'static> RingBuffer<T>
Sourcepub fn new(capacity: usize) -> Self
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
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}
Sourcepub fn split(self, handle: &Handle) -> (Producer<T>, Consumer<T>)
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
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}
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more