Debian Codegraph @

third_party/rust/ringbuf/src/lib.rs

raw
1 //! Lock-free single-producer single-consumer (SPSC) FIFO ring buffer with direct access to inner data.
2 //!
3 //! # Overview
4 //!
5 //! `RingBuffer` is the initial structure representing ring buffer itself.
6 //! Ring buffer can be splitted into pair of `Producer` and `Consumer`.
7 //!
8 //! `Producer` and `Consumer` are used to append/remove elements to/from the ring buffer accordingly. They can be safely sent between threads.
9 //! Operations with `Producer` and `Consumer` are lock-free - they succeed or fail immediately without blocking or waiting.
10 //!
11 //! Elements can be effectively appended/removed one by one or many at once.
12 //! Also data could be loaded/stored directly into/from [`Read`]/[`Write`] instances.
13 //! And finally, there are `unsafe` methods allowing thread-safe direct access in place to the inner memory being appended/removed.
14 //!
15 //! [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
16 //! [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
17 //!
18 //! When building with nightly toolchain it is possible to run benchmarks via `cargo bench --features benchmark`.
19 //!
20 //! # Examples
21 //!
22 //! ## Simple example
23 //!
24 //! ```rust
25 //! # extern crate ringbuf;
26 //! use ringbuf::RingBuffer;
27 //! # fn main() {
28 //! let rb = RingBuffer::<i32>::new(2);
29 //! let (mut prod, mut cons) = rb.split();
30 //!
31 //! prod.push(0).unwrap();
32 //! prod.push(1).unwrap();
33 //! assert_eq!(prod.push(2), Err(2));
34 //!
35 //! assert_eq!(cons.pop().unwrap(), 0);
36 //!
37 //! prod.push(2).unwrap();
38 //!
39 //! assert_eq!(cons.pop().unwrap(), 1);
40 //! assert_eq!(cons.pop().unwrap(), 2);
41 //! assert_eq!(cons.pop(), None);
42 //! # }
43 //! ```
44
45 #![no_std]
46 #![cfg_attr(feature = "benchmark", feature(test))]
47
48 extern crate alloc;
49 #[cfg(feature = "std")]
50 extern crate std;
51
52 #[cfg(feature = "benchmark")]
53 extern crate test;
54
55 #[cfg(feature = "benchmark")]
56 mod benchmark;
57
58 #[cfg(test)]
59 mod tests;
60
61 mod consumer;
62 mod producer;
63 mod ring_buffer;
64
65 pub use consumer::*;
66 pub use producer::*;
67 pub use ring_buffer::*;