Debian Codegraph @

README.md

raw symbols SCIP
1 # ringbuf
2
3 [![Crates.io][crates_badge]][crates]
4 [![Docs.rs][docs_badge]][docs]
5 [![Github Actions][github_badge]][github]
6 [![License][license_badge]][license]
7
8 [crates_badge]: https://img.shields.io/crates/v/ringbuf.svg
9 [docs_badge]: https://docs.rs/ringbuf/badge.svg
10 [github_badge]: https://github.com/agerasev/ringbuf/actions/workflows/test.yml/badge.svg
11 [license_badge]: https://img.shields.io/crates/l/ringbuf.svg
12
13 [crates]: https://crates.io/crates/ringbuf
14 [docs]: https://docs.rs/ringbuf
15 [github]: https://github.com/agerasev/ringbuf/actions/workflows/test.yml
16 [license]: #license
17
18 Lock-free SPSC FIFO ring buffer with direct access to inner data.
19
20 ## Features
21
22 + Lock-free operations - they succeed or fail immediately without blocking or waiting.
23 + Arbitrary item type (not only `Copy`).
24 + Items can be inserted and removed one by one or many at once.
25 + Thread-safe direct access to the internal ring buffer memory.
26 + `Read` and `Write` implementation.
27 + Overwriting mode support.
28 + Can be used without `std` and even without `alloc` (using only statically-allocated memory).
29 + [Experimental `async`/`.await` support](https://github.com/agerasev/async-ringbuf).
30
31 ## Usage
32
33 At first you need to create the ring buffer itself. `HeapRb` is recommended but you may [choose another one](#types).
34
35 After the ring buffer is created it may be splitted into pair of `Producer` and `Consumer`.
36 `Producer` is used to insert items to the ring buffer, `Consumer` - to remove items from it.
37 For `SharedRb` and its derivatives they can be used in different threads.
38
39 ## Types
40
41 There are several types of ring buffers provided:
42
43 + `LocalRb`. Only for single-threaded use.
44 + `SharedRb`. Can be shared between threads. Its derivatives:
45 + `HeapRb`. Contents are stored in dynamic memory. *Recommended for use in most cases.*
46 + `StaticRb`. Contents can be stored in statically-allocated memory.
47
48 ## Performance
49
50 `SharedRb` needs to synchronize CPU cache between CPU cores. This synchronization has some overhead.
51 To avoid multiple unnecessary synchronizations you may use postponed mode of operation (see description for `Producer` and `Consumer`)
52 or methods that operates many items at once (`Producer::push_slice`/`Producer::push_iter`, `Consumer::pop_slice`, etc.).
53
54 For single-threaded usage `LocalRb` is recommended because it is faster than `SharedRb` due to absence of CPU cache synchronization.
55
56 ### Benchmarks
57
58 You may see typical performance of different methods in benchmarks:
59
60 ```bash
61 cargo +nightly bench --features bench
62 ```
63
64 Nightly toolchain is required.
65
66 ## Examples
67
68 ### Simple
69
70 ```rust
71 use ringbuf::HeapRb;
72
73 # fn main() {
74 let rb = HeapRb::<i32>::new(2);
75 let (mut prod, mut cons) = rb.split();
76
77 prod.push(0).unwrap();
78 prod.push(1).unwrap();
79 assert_eq!(prod.push(2), Err(2));
80
81 assert_eq!(cons.pop(), Some(0));
82
83 prod.push(2).unwrap();
84
85 assert_eq!(cons.pop(), Some(1));
86 assert_eq!(cons.pop(), Some(2));
87 assert_eq!(cons.pop(), None);
88 # }
89 ```
90
91 ### No heap
92
93 ```rust
94 use ringbuf::StaticRb;
95
96 # fn main() {
97 const RB_SIZE: usize = 1;
98 let mut rb = StaticRb::<i32, RB_SIZE>::default();
99 let (mut prod, mut cons) = rb.split_ref();
100
101 assert_eq!(prod.push(123), Ok(()));
102 assert_eq!(prod.push(321), Err(321));
103
104 assert_eq!(cons.pop(), Some(123));
105 assert_eq!(cons.pop(), None);
106 # }
107 ```
108
109 ## Overwrite
110
111 Ring buffer can be used in overwriting mode when insertion overwrites the latest element if the buffer is full.
112
113 ```rust
114 use ringbuf::{HeapRb, Rb};
115
116 # fn main() {
117 let mut rb = HeapRb::<i32>::new(2);
118
119 assert_eq!(rb.push_overwrite(0), None);
120 assert_eq!(rb.push_overwrite(1), None);
121 assert_eq!(rb.push_overwrite(2), Some(0));
122
123 assert_eq!(rb.pop(), Some(1));
124 assert_eq!(rb.pop(), Some(2));
125 assert_eq!(rb.pop(), None);
126 # }
127 ```
128
129 Note that [`push_overwrite`](`Rb::push_overwrite`) requires exclusive access to the ring buffer
130 so to perform it concurrently you need to guard the ring buffer with [`Mutex`](`std::sync::Mutex`) or some other lock.
131
132 ## `async`/`.await`
133
134 There is an experimental crate [`async-ringbuf`](https://github.com/agerasev/async-ringbuf)
135 which is built on top of `ringbuf` and implements asynchronous ring buffer operations.
136
137 ## License
138
139 Licensed under either of
140
141 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
142 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
143
144 at your option.
145
146 ### Contribution
147
148 Unless you explicitly state otherwise, any contribution intentionally submitted
149 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
150 additional terms or conditions.