1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Enable experimental features for documentation.
/*
Copyright (c) 2024 NickelAnge.Studio
Email mathieu.grenier@nickelange.studio
Git https://github.com/NickelAngeStudio/nsrb
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFcircularEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//! Nifty Simple Ring Buffer (aka circular buffer) is a [`no_std`](https://docs.rust-embedded.org/book/intro/no-std.html) crate that provides the [`ring!`] and [`manx!`] macros to easily create
//! circular buffer data structure on the stack.
//!
//! ### Example
//! ```
//! // Import crate with #[macro_use]
//! ##[macro_use] extern crate nsrb;
//!
//! // Use ring! macro to create circular buffer structure.
//! nsrb::ring!(pub(crate) ExampleRB[usize; 10]);
//!
//! // You can implement and access buffer inner variables if needed.
//! impl ExampleRB {
//! pub fn head(&self) -> usize {
//! self.head
//! }
//! }
//!
//! fn main() {
//! // Use struct in code.
//! let mut rb = ExampleRB::new();
//! rb.push(5);
//! assert_eq!(*rb.pop().unwrap(), 5);
//! assert_eq!(rb.head(), 1); // Using newly implemented method.
//! }
//! ``````
/// Smallest size a ring buffer can be. Default : 2.
///
/// Can be removed via the `no_limit` feature.
pub const NSRB_LOWER_LIMIT : usize = 2;
/// Largest size a ring buffer can be. Default : [u16::MAX].
///
/// Can be removed via the `no_limit` feature.
pub const NSRB_UPPER_LIMIT : usize = u16MAX as usize;
/*
//! You can also create [optimized](https://en.wikipedia.org/wiki/Circular_buffer#Optimization)
//! [unchecked](https://doc.rust-lang.org/beta/book/ch03-02-data-types.html#integer-overflow) [u8] / [u16] [`ring!`]
//! and [`manx!`] buffer if performance is needed by using the `@unchecked(u8)` and `@unchecked(u16)`
//! modifiers. Refer to each macro documentation for more information.
*/
/*
#[macro_export]
macro_rules! ring {
($name : ident, $type : ty, $size : expr) => {
$crate::ring_core!(,$name, $type,$size,);
};
($name : ident, $type : ty, $size : expr, $header : meta) => {
$crate::ring_core!(,$name, $type,$size,$header);
};
($visibility : vis, $name : ident, $type : ty, $size : expr) => {
$crate::ring_core!($visibility,$name, $type,$size,);
};
($visibility : vis, $name : ident, $type : ty, $size : expr, $header : meta) => {
$crate::ring_core!($visibility,$name, $type,$size,$header);
};
(@unchecked(u8) $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u8) $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u8) $visibility : vis, $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u8) $visibility : vis, $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u16) $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
(@unchecked(u16) $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
(@unchecked(u16) $visibility : vis, $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
(@unchecked(u16) $visibility : vis, $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
}
//ring!(pub, Toto, usize, 10, derive(Debug));
/// Create a [manx](https://www.approxion.com/circular-adventures-ix-the-poor-ring-buffer-that-had-no-tail/)
/// buffer. See [`manx syntax`](macro.manx.html#syntax) for usage.
#[macro_export]
macro_rules! manx {
($name : ident, $type : ty, $size : expr) => {
$crate::manx_core!(,$name, $type,$size);
};
($visibility : vis, $name : ident, $type : ty, $size : expr) => {
$crate::manx_core!($visibility,$name, $type,$size);
};
(@unchecked(u8) $name : ident, $type : ty) =>{
$crate::manx_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u16) $name : ident, $type : ty) =>{
$crate::manx_core!(@unchecked(u16) ,$name, $type);
};
}
ring2!(Toto[usize; 10]);
ring2!(pub(crate) Toto2[usize; 10]);
ring2!(#[derive(Debug)] Toto3[usize; 10]);
ring2!(pub(crate) Toto4[usize; 10]);
ring2!{ #[doc="Foo"]
#[derive(Debug,Copy,Clone)]
pub Toto5[usize; 10] }
ring2!(@unchecked(u8) Toto7[usize]);
ring2!{ @unchecked(u8) #[doc="Foo"]
#[derive(Debug,Copy,Clone)]
pub Toto8[usize] }
#[macro_export]
macro_rules! ring2 {
/*
($name : ident, $type : ty, $size : expr) => {
$crate::ring_core!(,$name, $type,$size,);
};
($name : ident, $type : ty, $size : expr, $header : meta) => {
$crate::ring_core!(,$name, $type,$size,$header);
};
($visibility : vis, $name : ident, $type : ty, $size : expr) => {
$crate::ring_core!($visibility,$name, $type,$size,);
};
*/
($(#[$attr:meta])* $visibility : vis $name : ident[$type : ty; $size : expr]) => {
//$crate::ring_core!($visibility,$name, $type,$size,$header);
$(
#[$attr]
)*
#[allow(dead_code)]
$visibility struct $name { tail : usize, head : usize, buffer : [$type; $size], }
};
(@unchecked(u8) $(#[$attr:meta])* $visibility : vis $name : ident[$type : ty]) => {
//$crate::ring_core!($visibility,$name, $type,$size,$header);
$(
#[$attr]
)*
#[allow(dead_code)]
$visibility struct $name { tail : usize, head : usize, buffer : [$type; u8::MAX as usize], }
};
(@unchecked(u8) $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u8) $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u8) $visibility : vis, $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u8) $visibility : vis, $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u8) ,$name, $type);
};
(@unchecked(u16) $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
(@unchecked(u16) $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
(@unchecked(u16) $visibility : vis, $name : ident, $type : ty) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
(@unchecked(u16) $visibility : vis, $name : ident, $type : ty, $header : meta) =>{
$crate::ring_core!(@unchecked(u16) ,$name, $type);
};
}
*/