slice_ring_buf/referenced.rs
1use core::num::NonZeroUsize;
2use core::fmt::Debug;
3
4use super::inner;
5
6/// A ring buffer implementation optimized for working with slices. Note this pretty
7/// much does the same thing as [`VecDeque`], but with the added ability to index
8/// using negative values, as well as working with buffers allocated on the stack.
9/// This struct can be used without the standard library (`#![no_std]`).
10///
11/// This works the same as [`SliceRB`] except it uses an immutable reference as its
12/// data source instead of an internal Vec.
13///
14/// This struct has no consumer/producer logic, and is meant to be used for DSP or as
15/// a base for other data structures.
16///
17/// This data type is optimized for manipulating data in chunks with slices.
18/// Indexing one element at a time is slow.
19///
20/// The length of this ring buffer cannot be `0`.
21///
22/// ## Example
23/// ```rust
24/// # use slice_ring_buf::SliceRbRef;
25/// let stack_data = [0u32, 1, 2, 3];
26/// let rb_ref = SliceRbRef::new(&stack_data);
27/// assert_eq!(rb_ref[-3], 1);
28///
29/// let (s1, s2) = rb_ref.as_slices_len(2, 3);
30/// assert_eq!(s1, &[2, 3]);
31/// assert_eq!(s2, &[0]);
32/// ```
33///
34/// [`VecDeque`]: https://doc.rust-lang.org/std/collections/struct.VecDeque.html
35/// [`SliceRB`]: struct.SliceRB.html
36pub struct SliceRbRef<'a, T> {
37 data: &'a [T],
38}
39
40impl<'a, T> SliceRbRef<'a, T> {
41 /// Creates a new [`SliceRbRef`] with the given data.
42 ///
43 /// # Example
44 ///
45 /// ```
46 /// # use slice_ring_buf::SliceRbRef;
47 /// let data = [1u32, 2, 3, 4];
48 /// let rb = SliceRbRef::new(&data[..]);
49 ///
50 /// assert_eq!(rb.len().get(), 4);
51 ///
52 /// assert_eq!(rb[0], 1);
53 /// assert_eq!(rb[1], 2);
54 /// assert_eq!(rb[2], 3);
55 /// assert_eq!(rb[3], 4);
56 /// ```
57 ///
58 /// # Panics
59 ///
60 /// * This will panic if the length of `slice` is `0` or is greater
61 /// than `isize::MAX`.
62 #[inline]
63 pub const fn new(slice: &'a [T]) -> Self {
64 assert!(!slice.is_empty());
65 assert!(slice.len() <= isize::MAX as usize);
66
67 Self { data: slice }
68 }
69
70 /// Creates a new [`SliceRbRef`] with the given data without checking
71 /// that the length of the data is greater than `0` and less than or
72 /// equal to `isize::MAX`.
73 ///
74 /// # Example
75 ///
76 /// ```
77 /// # use slice_ring_buf::SliceRbRef;
78 /// let data = [1u32, 2, 3, 4];
79 /// let rb = unsafe { SliceRbRef::new_unchecked(&data[..]) };
80 ///
81 /// assert_eq!(rb.len().get(), 4);
82 ///
83 /// assert_eq!(rb[0], 1);
84 /// assert_eq!(rb[1], 2);
85 /// assert_eq!(rb[2], 3);
86 /// assert_eq!(rb[3], 4);
87 /// ```
88 ///
89 /// # Safety
90 ///
91 /// The length of `slice` must be greater than `0` and less than or
92 /// equal to `isize::MAX`.
93 #[inline]
94 pub const unsafe fn new_unchecked(slice: &'a [T]) -> Self {
95 debug_assert!(!slice.is_empty());
96 debug_assert!(slice.len() <= isize::MAX as usize);
97
98 Self { data: slice }
99 }
100
101 /// Returns the length of the ring buffer.
102 ///
103 /// # Example
104 ///
105 /// ```
106 /// # use slice_ring_buf::SliceRbRef;
107 /// let data = [0u32; 4];
108 /// let rb = SliceRbRef::new(&data[..]);
109 ///
110 /// assert_eq!(rb.len().get(), 4);
111 /// ```
112 pub fn len(&self) -> NonZeroUsize {
113 // SAFETY:
114 // * All constructors ensure that the length is greater than `0`.
115 unsafe { NonZeroUsize::new_unchecked(self.data.len()) }
116 }
117
118 /// Returns the actual index of the ring buffer from the given
119 /// `i` index.
120 ///
121 /// * First, a bounds check will be performed. If it is within bounds,
122 /// then it is simply returned.
123 /// * If it is not in bounds, then performance will
124 /// be limited by the modulo (remainder) operation on an `isize` value.
125 ///
126 /// # Performance
127 ///
128 /// Prefer to manipulate data in bulk with methods that return slices. If you
129 /// need to index multiple elements one at a time, prefer to use
130 /// `SliceRbRef::at(&mut i)` over `SliceRbRef[i]` to reduce the number of
131 /// modulo operations to perform.
132 ///
133 /// # Example
134 ///
135 /// ```
136 /// # use slice_ring_buf::SliceRbRef;
137 /// let data = [0u32; 4];
138 /// let rb = SliceRbRef::new(&data[..]);
139 ///
140 /// assert_eq!(rb.constrain(2), 2);
141 /// assert_eq!(rb.constrain(4), 0);
142 /// assert_eq!(rb.constrain(-3), 1);
143 /// assert_eq!(rb.constrain(7), 3);
144 /// ```
145 pub fn constrain(&self, i: isize) -> isize {
146 inner::constrain(i, self.data.len() as isize)
147 }
148
149 /// Returns two slices that contain all the data in the ring buffer
150 /// starting at the index `start`.
151 ///
152 /// # Returns
153 ///
154 /// * The first slice is the starting chunk of data. This will never be empty.
155 /// * The second slice is the second contiguous chunk of data. This may
156 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
157 /// its internal memory layout.
158 ///
159 /// # Performance
160 ///
161 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
162 ///
163 /// # Example
164 ///
165 /// ```
166 /// # use slice_ring_buf::SliceRbRef;
167 /// let data = [1u32, 2, 3, 4];
168 /// let rb = SliceRbRef::new(&data[..]);
169 ///
170 /// let (s1, s2) = rb.as_slices(-4);
171 /// assert_eq!(s1, &[1, 2, 3, 4]);
172 /// assert_eq!(s2, &[]);
173 ///
174 /// let (s1, s2) = rb.as_slices(3);
175 /// assert_eq!(s1, &[4]);
176 /// assert_eq!(s2, &[1, 2, 3]);
177 /// ```
178 pub fn as_slices(&self, start: isize) -> (&[T], &[T]) {
179 // SAFETY:
180 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
181 unsafe { inner::as_slices(start, self.data) }
182 }
183
184 /// Returns two slices of data in the ring buffer
185 /// starting at the index `start` and with length `len`.
186 ///
187 /// * `start` - The starting index
188 /// * `len` - The length of data to read. If `len` is greater than the
189 /// capacity of the ring buffer, then that capacity will be used instead.
190 ///
191 /// # Returns
192 ///
193 /// * The first slice is the starting chunk of data.
194 /// * The second slice is the second contiguous chunk of data. This may
195 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
196 /// its internal memory layout.
197 ///
198 /// # Performance
199 ///
200 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
201 ///
202 /// # Example
203 ///
204 /// ```
205 /// # use slice_ring_buf::SliceRbRef;
206 /// let data = [1u32, 2, 3, 4];
207 /// let rb = SliceRbRef::new(&data[..]);
208 ///
209 /// let (s1, s2) = rb.as_slices_len(-4, 3);
210 /// assert_eq!(s1, &[1, 2, 3]);
211 /// assert_eq!(s2, &[]);
212 ///
213 /// let (s1, s2) = rb.as_slices_len(3, 5);
214 /// assert_eq!(s1, &[4]);
215 /// assert_eq!(s2, &[1, 2, 3]);
216 /// ```
217 pub fn as_slices_len(&self, start: isize, len: usize) -> (&[T], &[T]) {
218 // SAFETY:
219 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
220 unsafe { inner::as_slices_len(start, len, self.data) }
221 }
222
223 /// Returns two slices of data in the ring buffer
224 /// starting at the index `start` and with length `len`. If `len` is greater
225 /// than the length of the ring buffer, then the buffer's length will be used
226 /// instead, while still preserving the position of the last element.
227 ///
228 /// * `start` - The starting index
229 /// * `len` - The length of data to read. If `len` is greater than the
230 /// length of the ring buffer, then the buffer's length will be used instead, while
231 /// still preserving the position of the last element.
232 ///
233 /// # Returns
234 ///
235 /// * The first slice is the starting chunk of data.
236 /// * The second slice is the second contiguous chunk of data. This may
237 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
238 /// its internal memory layout.
239 ///
240 /// # Performance
241 ///
242 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
243 ///
244 /// # Example
245 ///
246 /// ```
247 /// # use slice_ring_buf::SliceRbRef;
248 /// let data = [1u32, 2, 3, 4];
249 /// let rb = SliceRbRef::new(&data[..]);
250 ///
251 /// let (s1, s2) = rb.as_slices_latest(-4, 3);
252 /// assert_eq!(s1, &[1, 2, 3]);
253 /// assert_eq!(s2, &[]);
254 ///
255 /// let (s1, s2) = rb.as_slices_latest(0, 5);
256 /// assert_eq!(s1, &[2, 3, 4]);
257 /// assert_eq!(s2, &[1]);
258 /// ```
259 pub fn as_slices_latest(&self, start: isize, len: usize) -> (&[T], &[T]) {
260 // SAFETY:
261 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
262 unsafe { inner::as_slices_latest(start, len, self.data) }
263 }
264
265 /// Returns all the data in the buffer. The starting index will
266 /// always be `0`.
267 ///
268 /// # Example
269 ///
270 /// ```
271 /// # use slice_ring_buf::SliceRbRef;
272 /// let data = [1u32, 2, 3, 4];
273 /// let rb = SliceRbRef::new(&data[..]);
274 ///
275 /// let raw_data = rb.raw_data();
276 /// assert_eq!(raw_data, &[1u32, 2, 3, 4]);
277 /// ```
278 pub fn raw_data(&self) -> &[T] {
279 self.data
280 }
281
282 /// Returns an immutable reference the element at the index of type `isize`.
283 ///
284 /// # Performance
285 ///
286 /// Prefer to manipulate data in bulk with methods that return slices. If you
287 /// need to index multiple elements one at a time, prefer to use
288 /// this over `SliceRbRef[i]` to reduce the number of
289 /// modulo operations to perform.
290 ///
291 /// # Example
292 ///
293 /// ```
294 /// # use slice_ring_buf::SliceRbRef;
295 /// let data = [1u32, 2, 3, 4];
296 /// let rb = SliceRbRef::new(&data[..]);
297 ///
298 /// assert_eq!(*rb.get(-3), 2);
299 /// ```
300 #[inline]
301 pub fn get(&self, i: isize) -> &T {
302 // SAFETY:
303 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
304 unsafe { inner::get(i, self.data) }
305 }
306
307 /// Returns an immutable reference to the element at the index of type `isize`
308 /// while also constraining the index `i`. This is more efficient than calling
309 /// both methods individually.
310 ///
311 /// # Performance
312 ///
313 /// Prefer to manipulate data in bulk with methods that return slices. If you
314 /// need to index multiple elements one at a time, prefer to use
315 /// this over `SliceRbRef[i]` to reduce the number of
316 /// modulo operations to perform.
317 ///
318 /// # Example
319 ///
320 /// ```
321 /// # use slice_ring_buf::SliceRbRef;
322 /// let data = [1u32, 2, 3, 4];
323 /// let rb = SliceRbRef::new(&data[..]);
324 ///
325 /// let mut i = -3;
326 /// assert_eq!(*rb.constrain_and_get(&mut i), 2);
327 /// assert_eq!(i, 1);
328 /// ```
329 #[inline]
330 pub fn constrain_and_get(&self, i: &mut isize) -> &T {
331 // SAFETY:
332 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
333 unsafe { inner::constrain_and_get(i, self.data) }
334 }
335}
336
337impl<'a, T: Clone + Copy> SliceRbRef<'a, T> {
338 /// Copies the data from the ring buffer starting from the index `start`
339 /// into the given slice. If the length of `slice` is larger than the
340 /// capacity of the ring buffer, then the data will be reapeated until
341 /// the given slice is filled.
342 ///
343 /// * `slice` - This slice to copy the data into.
344 /// * `start` - The index of the ring buffer to start copying from.
345 ///
346 /// # Performance
347 ///
348 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
349 ///
350 /// # Example
351 ///
352 /// ```
353 /// # use slice_ring_buf::SliceRbRef;
354 /// let data = [1u32, 2, 3, 4];
355 /// let rb = SliceRbRef::new(&data[..]);
356 ///
357 /// let mut read_buf = [0u32; 3];
358 /// rb.read_into(&mut read_buf[..], -3);
359 /// assert_eq!(read_buf, [2, 3, 4]);
360 ///
361 /// let mut read_buf = [0u32; 9];
362 /// rb.read_into(&mut read_buf[..], 2);
363 /// assert_eq!(read_buf, [3, 4, 1, 2, 3, 4, 1, 2, 3]);
364 /// ```
365 pub fn read_into(&self, slice: &mut [T], start: isize) {
366 // SAFETY:
367 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
368 unsafe { inner::read_into(slice, start, self.data) }
369 }
370}
371
372/// A ring buffer implementation optimized for working with slices. Note this pretty
373/// much does the same thing as [`VecDeque`], but with the added ability to index
374/// using negative values, as well as working with buffers allocated on the stack.
375/// This struct can be used without the standard library (`#![no_std]`).
376///
377/// This works the same as [`SliceRB`] except it uses a mutable reference as its
378/// data source instead of an internal Vec.
379///
380/// This struct has no consumer/producer logic, and is meant to be used for DSP or as
381/// a base for other data structures.
382///
383/// This data type is optimized for manipulating data in chunks with slices.
384/// Indexing one element at a time is slow.
385///
386/// The length of this ring buffer cannot be `0`.
387///
388/// ## Example
389/// ```rust
390/// # use slice_ring_buf::SliceRbRefMut;
391/// let mut stack_data = [0u32, 1, 2, 3];
392/// let mut rb_ref = SliceRbRefMut::new(&mut stack_data);
393///
394/// rb_ref[-4] = 5;
395///
396/// let (s1, s2) = rb_ref.as_slices_len(2, 3);
397/// assert_eq!(s1, &[2, 3]);
398/// assert_eq!(s2, &[5]);
399/// ```
400///
401/// [`VecDeque`]: https://doc.rust-lang.org/std/collections/struct.VecDeque.html
402/// [`SliceRB`]: struct.SliceRB.html
403pub struct SliceRbRefMut<'a, T> {
404 data: &'a mut [T],
405}
406
407impl<'a, T> SliceRbRefMut<'a, T> {
408 /// Creates a new [`SliceRbRefMut`] with the given data.
409 ///
410 /// # Example
411 ///
412 /// ```
413 /// # use slice_ring_buf::SliceRbRefMut;
414 /// let mut data = [1u32, 2, 3, 4];
415 /// let rb = SliceRbRefMut::new(&mut data[..]);
416 ///
417 /// assert_eq!(rb.len(), 4);
418 ///
419 /// assert_eq!(rb[0], 1);
420 /// assert_eq!(rb[1], 2);
421 /// assert_eq!(rb[2], 3);
422 /// assert_eq!(rb[3], 4);
423 /// ```
424 ///
425 /// # Panics
426 ///
427 /// * This will panic if the length of `slice` is `0` or is greater
428 /// than `isize::MAX`.
429 #[inline]
430 pub const fn new(slice: &'a mut [T]) -> Self {
431 assert!(!slice.is_empty());
432 assert!(slice.len() <= isize::MAX as usize);
433
434 Self { data: slice }
435 }
436
437 /// Creates a new [`SliceRbRefMut`] with the given data without checking
438 /// that the length of the data is greater than `0` and less than or
439 /// equal to `isize::MAX`.
440 ///
441 /// # Example
442 ///
443 /// ```
444 /// # use slice_ring_buf::SliceRbRefMut;
445 /// let mut data = [1u32, 2, 3, 4];
446 /// let rb = unsafe { SliceRbRefMut::new_unchecked(&mut data[..]) };
447 ///
448 /// assert_eq!(rb.len(), 4);
449 ///
450 /// assert_eq!(rb[0], 1);
451 /// assert_eq!(rb[1], 2);
452 /// assert_eq!(rb[2], 3);
453 /// assert_eq!(rb[3], 4);
454 /// ```
455 ///
456 /// # Safety
457 ///
458 /// The length of `slice` must be greater than `0` and less than or
459 /// equal to `isize::MAX`.
460 #[inline]
461 pub const unsafe fn new_unchecked(slice: &'a mut [T]) -> Self {
462 debug_assert!(!slice.is_empty());
463 debug_assert!(slice.len() <= isize::MAX as usize);
464
465 Self { data: slice }
466 }
467
468 /// Returns the length of the ring buffer.
469 ///
470 /// # Example
471 ///
472 /// ```
473 /// # use slice_ring_buf::SliceRbRefMut;
474 /// let mut data = [0u32; 4];
475 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
476 ///
477 /// assert_eq!(rb.len(), 4);
478 /// ```
479 pub fn len(&self) -> usize {
480 self.data.len()
481 }
482
483 /// Returns the actual index of the ring buffer from the given
484 /// `i` index.
485 ///
486 /// * First, a bounds check will be performed. If it is within bounds,
487 /// then it is simply returned.
488 /// * If it is not in bounds, then performance will
489 /// be limited by the modulo (remainder) operation on an `isize` value.
490 ///
491 /// # Performance
492 ///
493 /// Prefer to manipulate data in bulk with methods that return slices. If you
494 /// need to index multiple elements one at a time, prefer to use
495 /// `SliceRbRef::at(&mut i)` over `SliceRbRef[i]` to reduce the number of
496 /// modulo operations to perform.
497 ///
498 /// # Example
499 ///
500 /// ```
501 /// # use slice_ring_buf::SliceRbRefMut;
502 /// let mut data = [0u32; 4];
503 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
504 ///
505 /// assert_eq!(rb.constrain(2), 2);
506 /// assert_eq!(rb.constrain(4), 0);
507 /// assert_eq!(rb.constrain(-3), 1);
508 /// assert_eq!(rb.constrain(7), 3);
509 /// ```
510 pub fn constrain(&self, i: isize) -> isize {
511 inner::constrain(i, self.data.len() as isize)
512 }
513
514 /// Returns two slices that contain all the data in the ring buffer
515 /// starting at the index `start`.
516 ///
517 /// # Returns
518 ///
519 /// * The first slice is the starting chunk of data. This will never be empty.
520 /// * The second slice is the second contiguous chunk of data. This may
521 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
522 /// its internal memory layout.
523 ///
524 /// # Performance
525 ///
526 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
527 ///
528 /// # Example
529 ///
530 /// ```
531 /// # use slice_ring_buf::SliceRbRefMut;
532 /// let mut data = [1u32, 2, 3, 4];
533 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
534 ///
535 /// let (s1, s2) = rb.as_slices(-4);
536 /// assert_eq!(s1, &[1, 2, 3, 4]);
537 /// assert_eq!(s2, &[]);
538 ///
539 /// let (s1, s2) = rb.as_slices(3);
540 /// assert_eq!(s1, &[4]);
541 /// assert_eq!(s2, &[1, 2, 3]);
542 /// ```
543 pub fn as_slices(&self, start: isize) -> (&[T], &[T]) {
544 // SAFETY:
545 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
546 unsafe { inner::as_slices(start, self.data) }
547 }
548
549 /// Returns two slices of data in the ring buffer
550 /// starting at the index `start` and with length `len`.
551 ///
552 /// * `start` - The starting index
553 /// * `len` - The length of data to read. If `len` is greater than the
554 /// capacity of the ring buffer, then that capacity will be used instead.
555 ///
556 /// # Returns
557 ///
558 /// * The first slice is the starting chunk of data.
559 /// * The second slice is the second contiguous chunk of data. This may
560 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
561 /// its internal memory layout.
562 ///
563 /// # Performance
564 ///
565 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
566 ///
567 /// # Example
568 ///
569 /// ```
570 /// # use slice_ring_buf::SliceRbRefMut;
571 /// let mut data = [1u32, 2, 3, 4];
572 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
573 ///
574 /// let (s1, s2) = rb.as_slices_len(-4, 3);
575 /// assert_eq!(s1, &[1, 2, 3]);
576 /// assert_eq!(s2, &[]);
577 ///
578 /// let (s1, s2) = rb.as_slices_len(3, 5);
579 /// assert_eq!(s1, &[4]);
580 /// assert_eq!(s2, &[1, 2, 3]);
581 /// ```
582 pub fn as_slices_len(&self, start: isize, len: usize) -> (&[T], &[T]) {
583 // SAFETY:
584 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
585 unsafe { inner::as_slices_len(start, len, self.data) }
586 }
587
588 /// Returns two slices of data in the ring buffer
589 /// starting at the index `start` and with length `len`. If `len` is greater
590 /// than the length of the ring buffer, then the buffer's length will be used
591 /// instead, while still preserving the position of the last element.
592 ///
593 /// * `start` - The starting index
594 /// * `len` - The length of data to read. If `len` is greater than the
595 /// length of the ring buffer, then the buffer's length will be used instead, while
596 /// still preserving the position of the last element.
597 ///
598 /// # Returns
599 ///
600 /// * The first slice is the starting chunk of data.
601 /// * The second slice is the second contiguous chunk of data. This may
602 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
603 /// its internal memory layout.
604 ///
605 /// # Performance
606 ///
607 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
608 ///
609 /// # Example
610 ///
611 /// ```
612 /// # use slice_ring_buf::SliceRbRefMut;
613 /// let mut data = [1u32, 2, 3, 4];
614 /// let rb = SliceRbRefMut::new(&mut data[..]);
615 ///
616 /// let (s1, s2) = rb.as_slices_latest(-4, 3);
617 /// assert_eq!(s1, &[1, 2, 3]);
618 /// assert_eq!(s2, &[]);
619 ///
620 /// let (s1, s2) = rb.as_slices_latest(0, 5);
621 /// assert_eq!(s1, &[2, 3, 4]);
622 /// assert_eq!(s2, &[1]);
623 /// ```
624 pub fn as_slices_latest(&self, start: isize, len: usize) -> (&[T], &[T]) {
625 // SAFETY:
626 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
627 unsafe { inner::as_slices_latest(start, len, self.data) }
628 }
629
630 /// Returns two mutable slices that contain all the data in the ring buffer
631 /// starting at the index `start`.
632 ///
633 /// # Returns
634 ///
635 /// * The first slice is the starting chunk of data. This will never be empty.
636 /// * The second slice is the second contiguous chunk of data. This may
637 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
638 /// its internal memory layout.
639 ///
640 /// # Performance
641 ///
642 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
643 ///
644 /// # Example
645 ///
646 /// ```
647 /// # use slice_ring_buf::SliceRbRefMut;
648 /// let mut data = [1u32, 2, 3, 4];
649 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
650 ///
651 /// let (s1, s2) = rb.as_mut_slices(-4);
652 /// assert_eq!(s1, &mut [1, 2, 3, 4]);
653 /// assert_eq!(s2, &mut []);
654 ///
655 /// let (s1, s2) = rb.as_mut_slices(3);
656 /// assert_eq!(s1, &mut [4]);
657 /// assert_eq!(s2, &mut [1, 2, 3]);
658 /// ```
659 pub fn as_mut_slices(&mut self, start: isize) -> (&mut [T], &mut [T]) {
660 // SAFETY:
661 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
662 unsafe { inner::as_mut_slices(start, self.data) }
663 }
664
665 /// Returns two mutable slices of data in the ring buffer
666 /// starting at the index `start` and with length `len`.
667 ///
668 /// * `start` - The starting index
669 /// * `len` - The length of data to read. If `len` is greater than the
670 /// capacity of the ring buffer, then that capacity will be used instead.
671 ///
672 /// # Returns
673 ///
674 /// * The first slice is the starting chunk of data.
675 /// * The second slice is the second contiguous chunk of data. This may
676 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
677 /// its internal memory layout.
678 ///
679 /// # Performance
680 ///
681 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
682 ///
683 /// # Example
684 ///
685 /// ```
686 /// # use slice_ring_buf::SliceRbRefMut;
687 /// let mut data = [1u32, 2, 3, 4];
688 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
689 ///
690 /// let (s1, s2) = rb.as_mut_slices_len(-4, 3);
691 /// assert_eq!(s1, &mut [1, 2, 3]);
692 /// assert_eq!(s2, &mut []);
693 ///
694 /// let (s1, s2) = rb.as_mut_slices_len(3, 5);
695 /// assert_eq!(s1, &mut [4]);
696 /// assert_eq!(s2, &mut [1, 2, 3]);
697 /// ```
698 pub fn as_mut_slices_len(&mut self, start: isize, len: usize) -> (&mut [T], &mut [T]) {
699 // SAFETY:
700 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
701 unsafe { inner::as_mut_slices_len(start, len, self.data) }
702 }
703
704 /// Returns two mutable slices of data in the ring buffer
705 /// starting at the index `start` and with length `len`. If `len` is greater
706 /// than the length of the ring buffer, then the buffer's length will be used
707 /// instead, while still preserving the position of the last element.
708 ///
709 /// * `start` - The starting index
710 /// * `len` - The length of data to read. If `len` is greater than the
711 /// length of the ring buffer, then the buffer's length will be used instead, while
712 /// still preserving the position of the last element.
713 ///
714 /// # Returns
715 ///
716 /// * The first slice is the starting chunk of data.
717 /// * The second slice is the second contiguous chunk of data. This may
718 /// or may not be empty depending if the buffer needed to wrap around to the beginning of
719 /// its internal memory layout.
720 ///
721 /// # Performance
722 ///
723 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
724 ///
725 /// # Example
726 ///
727 /// ```
728 /// # use slice_ring_buf::SliceRbRefMut;
729 /// let mut data = [1u32, 2, 3, 4];
730 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
731 ///
732 /// let (s1, s2) = rb.as_mut_slices_latest(-4, 3);
733 /// assert_eq!(s1, &mut [1, 2, 3]);
734 /// assert_eq!(s2, &mut []);
735 ///
736 /// let (s1, s2) = rb.as_mut_slices_latest(0, 5);
737 /// assert_eq!(s1, &mut [2, 3, 4]);
738 /// assert_eq!(s2, &mut [1]);
739 /// ```
740 pub fn as_mut_slices_latest(&mut self, start: isize, len: usize) -> (&mut [T], &mut [T]) {
741 // SAFETY:
742 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
743 unsafe { inner::as_mut_slices_latest(start, len, self.data) }
744 }
745
746 /// Returns all the data in the buffer. The starting index will
747 /// always be `0`.
748 ///
749 /// # Example
750 ///
751 /// ```
752 /// # use slice_ring_buf::SliceRbRefMut;
753 /// let mut data = [1u32, 2, 3, 4];
754 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
755 ///
756 /// let raw_data = rb.raw_data();
757 /// assert_eq!(raw_data, &[1u32, 2, 3, 4]);
758 /// ```
759 pub fn raw_data(&self) -> &[T] {
760 self.data
761 }
762
763 /// Returns all the data in the buffer as mutable. The starting
764 /// index will always be `0`.
765 ///
766 /// # Example
767 ///
768 /// ```
769 /// # use slice_ring_buf::SliceRbRefMut;
770 /// let mut data = [1u32, 2, 3, 4];
771 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
772 ///
773 /// let raw_data = rb.raw_data_mut();
774 /// assert_eq!(raw_data, &mut [1u32, 2, 3, 4]);
775 /// ```
776 pub fn raw_data_mut(&mut self) -> &mut [T] {
777 self.data
778 }
779
780 /// Returns an immutable reference the element at the index of type `isize`.
781 ///
782 /// # Performance
783 ///
784 /// Prefer to manipulate data in bulk with methods that return slices. If you
785 /// need to index multiple elements one at a time, prefer to use
786 /// this over `SliceRbRef[i]` to reduce the number of
787 /// modulo operations to perform.
788 ///
789 /// # Example
790 ///
791 /// ```
792 /// # use slice_ring_buf::SliceRbRefMut;
793 /// let mut data = [1u32, 2, 3, 4];
794 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
795 ///
796 /// assert_eq!(*rb.get(-3), 2);
797 /// ```
798 #[inline]
799 pub fn get(&self, i: isize) -> &T {
800 // SAFETY:
801 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
802 unsafe { inner::get(i, self.data) }
803 }
804
805 /// Returns a mutable reference the element at the index of type `isize`.
806 ///
807 /// # Performance
808 ///
809 /// Prefer to manipulate data in bulk with methods that return slices. If you
810 /// need to index multiple elements one at a time, prefer to use
811 /// this over `SliceRbRef[i]` to reduce the number of
812 /// modulo operations to perform.
813 ///
814 /// # Example
815 ///
816 /// ```
817 /// # use slice_ring_buf::SliceRbRefMut;
818 /// let mut data = [1u32, 2, 3, 4];
819 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
820 ///
821 /// *rb.get_mut(-3) = 5;
822 ///
823 /// assert_eq!(*rb.get(-3), 5);
824 /// ```
825 #[inline]
826 pub fn get_mut(&mut self, i: isize) -> &mut T {
827 // SAFETY:
828 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
829 unsafe { inner::get_mut(i, self.data) }
830 }
831
832 /// Returns an immutable reference to the element at the index of type `isize`
833 /// while also constraining the index `i`. This is more efficient than calling
834 /// both methods individually.
835 ///
836 /// # Performance
837 ///
838 /// Prefer to manipulate data in bulk with methods that return slices. If you
839 /// need to index multiple elements one at a time, prefer to use
840 /// this over `SliceRbRef[i]` to reduce the number of
841 /// modulo operations to perform.
842 ///
843 /// # Example
844 ///
845 /// ```
846 /// # use slice_ring_buf::SliceRbRefMut;
847 /// let mut data = [1u32, 2, 3, 4];
848 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
849 ///
850 /// let mut i = -3;
851 /// assert_eq!(*rb.constrain_and_get(&mut i), 2);
852 /// assert_eq!(i, 1);
853 /// ```
854 #[inline]
855 pub fn constrain_and_get(&self, i: &mut isize) -> &T {
856 // SAFETY:
857 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
858 unsafe { inner::constrain_and_get(i, self.data) }
859 }
860
861 /// Returns a mutable reference to the element at the index of type `isize` as
862 /// mutable while also constraining the index `i`. This is more efficient than
863 /// calling both methods individually.
864 ///
865 /// # Performance
866 ///
867 /// Prefer to manipulate data in bulk with methods that return slices. If you
868 /// need to index multiple elements one at a time, prefer to use
869 /// this over `SliceRbRef[i]` to reduce the number of
870 /// modulo operations to perform.
871 ///
872 /// # Example
873 ///
874 /// ```
875 /// # use slice_ring_buf::SliceRbRefMut;
876 /// let mut data = [0u32; 4];
877 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
878 ///
879 /// let mut i = -3;
880 /// *rb.constrain_and_get_mut(&mut i) = 2;
881 ///
882 /// assert_eq!(rb[1], 2);
883 /// assert_eq!(i, 1);
884 /// ```
885 #[inline]
886 pub fn constrain_and_get_mut(&mut self, i: &mut isize) -> &mut T {
887 // SAFETY:
888 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
889 unsafe { inner::constrain_and_get_mut(i, self.data) }
890 }
891}
892
893impl<'a, T: Clone + Copy> SliceRbRefMut<'a, T> {
894 /// Copies the data from the ring buffer starting from the index `start`
895 /// into the given slice. If the length of `slice` is larger than the
896 /// capacity of the ring buffer, then the data will be reapeated until
897 /// the given slice is filled.
898 ///
899 /// * `slice` - This slice to copy the data into.
900 /// * `start` - The index of the ring buffer to start copying from.
901 ///
902 /// # Performance
903 ///
904 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
905 ///
906 /// # Example
907 ///
908 /// ```
909 /// # use slice_ring_buf::SliceRbRefMut;
910 /// let mut data = [1u32, 2, 3, 4];
911 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
912 ///
913 /// let mut read_buf = [0u32; 3];
914 /// rb.read_into(&mut read_buf[..], -3);
915 /// assert_eq!(read_buf, [2, 3, 4]);
916 ///
917 /// let mut read_buf = [0u32; 9];
918 /// rb.read_into(&mut read_buf[..], 2);
919 /// assert_eq!(read_buf, [3, 4, 1, 2, 3, 4, 1, 2, 3]);
920 /// ```
921 pub fn read_into(&self, slice: &mut [T], start: isize) {
922 // SAFETY:
923 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
924 unsafe { inner::read_into(slice, start, self.data) }
925 }
926
927 /// Copies data from the given slice into the ring buffer starting from
928 /// the index `start`.
929 ///
930 /// Earlier data will not be copied if it will be
931 /// overwritten by newer data, avoiding unecessary memcpy's. The correct
932 /// placement of the newer data will still be preserved.
933 ///
934 /// * `slice` - This slice to copy data from.
935 /// * `start` - The index of the ring buffer to start copying from.
936 ///
937 /// # Performance
938 ///
939 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
940 ///
941 /// # Example
942 ///
943 /// ```
944 /// # use slice_ring_buf::SliceRbRefMut;
945 /// let mut data = [0u32; 4];
946 /// let mut rb = SliceRbRefMut::new(&mut data[..]);
947 ///
948 /// let input = [1u32, 2, 3];
949 /// rb.write_latest(&input[..], -3);
950 /// assert_eq!(rb[0], 0);
951 /// assert_eq!(rb[1], 1);
952 /// assert_eq!(rb[2], 2);
953 /// assert_eq!(rb[3], 3);
954 ///
955 /// let input = [1u32, 2, 3, 4, 5, 6, 7, 8, 9];
956 /// rb.write_latest(&input[..], 2);
957 /// assert_eq!(rb[0], 7);
958 /// assert_eq!(rb[1], 8);
959 /// assert_eq!(rb[2], 9);
960 /// assert_eq!(rb[3], 6);
961 /// ```
962 pub fn write_latest(&mut self, slice: &[T], start: isize) {
963 // SAFETY:
964 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
965 unsafe { inner::write_latest(slice, start, self.data) }
966 }
967
968 /// Copies data from two given slices into the ring buffer starting from
969 /// the index `start`. The `first` slice will be copied first then `second`
970 /// will be copied next.
971 ///
972 /// Earlier data will not be copied if it will be
973 /// overwritten by newer data, avoiding unecessary memcpy's. The correct
974 /// placement of the newer data will still be preserved.
975 ///
976 /// * `first` - This first slice to copy data from.
977 /// * `second` - This second slice to copy data from.
978 /// * `start` - The index of the ring buffer to start copying from.
979 ///
980 /// # Performance
981 ///
982 /// Prefer to use this to manipulate data in bulk over indexing one element at a time.
983 ///
984 /// # Example
985 ///
986 /// ```
987 /// # use core::num::NonZeroUsize;
988 /// # use slice_ring_buf::{SliceRB, SliceRbRefMut};
989 /// let mut input_rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
990 /// input_rb[0] = 1;
991 /// input_rb[1] = 2;
992 /// input_rb[2] = 3;
993 /// input_rb[3] = 4;
994 ///
995 /// let mut output_data = [0u32; 4];
996 /// let mut output_rb = SliceRbRefMut::new(&mut output_data[..]);
997 /// // s1 == &[1, 2], s2 == &[]
998 /// let (s1, s2) = input_rb.as_slices_len(0, 2);
999 /// output_rb.write_latest_2(s1, s2, -3);
1000 /// assert_eq!(output_rb[0], 0);
1001 /// assert_eq!(output_rb[1], 1);
1002 /// assert_eq!(output_rb[2], 2);
1003 /// assert_eq!(output_rb[3], 0);
1004 ///
1005 /// let mut output_data = [0u32; 2];
1006 /// let mut output_rb = SliceRbRefMut::new(&mut output_data[..]);
1007 /// // s1 == &[4], s2 == &[1, 2, 3]
1008 /// let (s1, s2) = input_rb.as_slices_len(3, 4);
1009 /// // rb[1] = 4 -> rb[0] = 1 -> rb[1] = 2 -> rb[0] = 3
1010 /// output_rb.write_latest_2(s1, s2, 1);
1011 /// assert_eq!(output_rb[0], 3);
1012 /// assert_eq!(output_rb[1], 2);
1013 /// ```
1014 pub fn write_latest_2(&mut self, first: &[T], second: &[T], start: isize) {
1015 // SAFETY:
1016 // The constructors ensure that `self.data.len() > 0 && self.data.len() <= isize::MAX`.
1017 unsafe { inner::write_latest_2(first, second, start, self.data) }
1018 }
1019}
1020
1021impl<'a, T> Into<SliceRbRef<'a, T>> for SliceRbRefMut<'a, T> {
1022 fn into(self) -> SliceRbRef<'a, T> {
1023 SliceRbRef { data: self.data }
1024 }
1025}
1026
1027impl<'a, T> core::ops::Index<isize> for SliceRbRef<'a, T> {
1028 type Output = T;
1029 fn index(&self, i: isize) -> &T {
1030 self.get(i)
1031 }
1032}
1033
1034impl<'a, T> core::ops::Index<isize> for SliceRbRefMut<'a, T> {
1035 type Output = T;
1036 fn index(&self, i: isize) -> &T {
1037 self.get(i)
1038 }
1039}
1040
1041impl<'a, T> core::ops::IndexMut<isize> for SliceRbRefMut<'a, T> {
1042 fn index_mut(&mut self, i: isize) -> &mut T {
1043 self.get_mut(i)
1044 }
1045}
1046
1047impl<'a, T: Debug> Debug for SliceRbRef<'a, T> {
1048 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1049 let mut f = f.debug_struct("SliceRbRef");
1050 f.field("data", &self.data);
1051 f.finish()
1052 }
1053}
1054
1055impl<'a, T: Debug> Debug for SliceRbRefMut<'a, T> {
1056 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1057 let mut f = f.debug_struct("SliceRbRefMut");
1058 f.field("data", &self.data);
1059 f.finish()
1060 }
1061}
1062
1063#[cfg(test)]
1064mod tests {
1065 use super::*;
1066
1067 #[test]
1068 fn slice_ring_buf_ref_initialize() {
1069 let mut data = [0.0; 4];
1070 let ring_buf = SliceRbRef::new(&mut data);
1071
1072 assert_eq!(&ring_buf.data[..], &[0.0, 0.0, 0.0, 0.0]);
1073 }
1074
1075 #[test]
1076 fn slice_ring_buf_ref_constrain() {
1077 let mut data = [0.0; 4];
1078 let ring_buf = SliceRbRef::new(&mut data);
1079
1080 assert_eq!(&ring_buf.data[..], &[0.0, 0.0, 0.0, 0.0]);
1081
1082 assert_eq!(ring_buf.constrain(-8), 0);
1083 assert_eq!(ring_buf.constrain(-7), 1);
1084 assert_eq!(ring_buf.constrain(-6), 2);
1085 assert_eq!(ring_buf.constrain(-5), 3);
1086 assert_eq!(ring_buf.constrain(-4), 0);
1087 assert_eq!(ring_buf.constrain(-3), 1);
1088 assert_eq!(ring_buf.constrain(-2), 2);
1089 assert_eq!(ring_buf.constrain(-1), 3);
1090 assert_eq!(ring_buf.constrain(0), 0);
1091 assert_eq!(ring_buf.constrain(1), 1);
1092 assert_eq!(ring_buf.constrain(2), 2);
1093 assert_eq!(ring_buf.constrain(3), 3);
1094 assert_eq!(ring_buf.constrain(4), 0);
1095 assert_eq!(ring_buf.constrain(5), 1);
1096 assert_eq!(ring_buf.constrain(6), 2);
1097 assert_eq!(ring_buf.constrain(7), 3);
1098 assert_eq!(ring_buf.constrain(8), 0);
1099 }
1100
1101 #[test]
1102 fn slice_ring_buf_ref_index() {
1103 let mut data = [0.0f32, 1.0, 2.0, 3.0];
1104 let ring_buf = SliceRbRef::new(&mut data);
1105
1106 let ring_buf = &ring_buf;
1107
1108 assert_eq!(ring_buf[-8], 0.0);
1109 assert_eq!(ring_buf[-7], 1.0);
1110 assert_eq!(ring_buf[-6], 2.0);
1111 assert_eq!(ring_buf[-5], 3.0);
1112 assert_eq!(ring_buf[-4], 0.0);
1113 assert_eq!(ring_buf[-3], 1.0);
1114 assert_eq!(ring_buf[-2], 2.0);
1115 assert_eq!(ring_buf[-1], 3.0);
1116 assert_eq!(ring_buf[0], 0.0);
1117 assert_eq!(ring_buf[1], 1.0);
1118 assert_eq!(ring_buf[2], 2.0);
1119 assert_eq!(ring_buf[3], 3.0);
1120 assert_eq!(ring_buf[4], 0.0);
1121 assert_eq!(ring_buf[5], 1.0);
1122 assert_eq!(ring_buf[6], 2.0);
1123 assert_eq!(ring_buf[7], 3.0);
1124 assert_eq!(ring_buf[8], 0.0);
1125 }
1126
1127 #[test]
1128 fn slice_ring_buf_ref_index_mut() {
1129 let mut data = [0.0f32, 1.0, 2.0, 3.0];
1130 let mut ring_buf = SliceRbRefMut::new(&mut data);
1131
1132 assert_eq!(&mut ring_buf[-8], &mut 0.0);
1133 assert_eq!(&mut ring_buf[-7], &mut 1.0);
1134 assert_eq!(&mut ring_buf[-6], &mut 2.0);
1135 assert_eq!(&mut ring_buf[-5], &mut 3.0);
1136 assert_eq!(&mut ring_buf[-4], &mut 0.0);
1137 assert_eq!(&mut ring_buf[-3], &mut 1.0);
1138 assert_eq!(&mut ring_buf[-2], &mut 2.0);
1139 assert_eq!(&mut ring_buf[-1], &mut 3.0);
1140 assert_eq!(&mut ring_buf[0], &mut 0.0);
1141 assert_eq!(&mut ring_buf[1], &mut 1.0);
1142 assert_eq!(&mut ring_buf[2], &mut 2.0);
1143 assert_eq!(&mut ring_buf[3], &mut 3.0);
1144 assert_eq!(&mut ring_buf[4], &mut 0.0);
1145 assert_eq!(&mut ring_buf[5], &mut 1.0);
1146 assert_eq!(&mut ring_buf[6], &mut 2.0);
1147 assert_eq!(&mut ring_buf[7], &mut 3.0);
1148 assert_eq!(&mut ring_buf[8], &mut 0.0);
1149 }
1150
1151 #[test]
1152 fn slice_ring_buf_ref_as_slices() {
1153 let mut data = [1.0f32, 2.0, 3.0, 4.0];
1154 let ring_buf = SliceRbRef::new(&mut data);
1155
1156 let (s1, s2) = ring_buf.as_slices(0);
1157 assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
1158 assert_eq!(s2, &[]);
1159
1160 let (s1, s2) = ring_buf.as_slices(1);
1161 assert_eq!(s1, &[2.0, 3.0, 4.0]);
1162 assert_eq!(s2, &[1.0]);
1163
1164 let (s1, s2) = ring_buf.as_slices(2);
1165 assert_eq!(s1, &[3.0, 4.0]);
1166 assert_eq!(s2, &[1.0, 2.0]);
1167
1168 let (s1, s2) = ring_buf.as_slices(3);
1169 assert_eq!(s1, &[4.0]);
1170 assert_eq!(s2, &[1.0, 2.0, 3.0]);
1171
1172 let (s1, s2) = ring_buf.as_slices(4);
1173 assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
1174 assert_eq!(s2, &[]);
1175 }
1176
1177 #[test]
1178 fn slice_ring_buf_ref_as_mut_slices() {
1179 let mut data = [1.0f32, 2.0, 3.0, 4.0];
1180 let mut ring_buf = SliceRbRefMut::new(&mut data);
1181
1182 let (s1, s2) = ring_buf.as_mut_slices(0);
1183 assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
1184 assert_eq!(s2, &[]);
1185
1186 let (s1, s2) = ring_buf.as_mut_slices(1);
1187 assert_eq!(s1, &[2.0, 3.0, 4.0]);
1188 assert_eq!(s2, &[1.0]);
1189
1190 let (s1, s2) = ring_buf.as_mut_slices(2);
1191 assert_eq!(s1, &[3.0, 4.0]);
1192 assert_eq!(s2, &[1.0, 2.0]);
1193
1194 let (s1, s2) = ring_buf.as_mut_slices(3);
1195 assert_eq!(s1, &[4.0]);
1196 assert_eq!(s2, &[1.0, 2.0, 3.0]);
1197
1198 let (s1, s2) = ring_buf.as_mut_slices(4);
1199 assert_eq!(s1, &[1.0, 2.0, 3.0, 4.0]);
1200 assert_eq!(s2, &[]);
1201 }
1202
1203 #[repr(C, align(1))]
1204 struct Aligned1([f32; 8]);
1205
1206 #[repr(C, align(2))]
1207 struct Aligned2([f32; 8]);
1208
1209 #[repr(C, align(4))]
1210 struct Aligned4([f32; 8]);
1211
1212 #[repr(C, align(8))]
1213 struct Aligned8([f32; 8]);
1214
1215 #[repr(C, align(16))]
1216 struct Aligned16([f32; 8]);
1217
1218 #[repr(C, align(32))]
1219 struct Aligned32([f32; 8]);
1220
1221 #[repr(C, align(64))]
1222 struct Aligned64([f32; 8]);
1223
1224 #[repr(C, align(32))]
1225 struct Aligned324([f32; 4]);
1226
1227 #[test]
1228 fn slice_ring_buf_ref_write_latest_2() {
1229 let mut data = Aligned324([0.0f32; 4]);
1230 let mut ring_buf = SliceRbRefMut::new(&mut data.0);
1231
1232 ring_buf.write_latest_2(&[], &[0.0, 1.0, 2.0, 3.0, 4.0], 1);
1233 assert_eq!(ring_buf.data, &[3.0, 4.0, 1.0, 2.0]);
1234 ring_buf.write_latest_2(&[-1.0], &[0.0, 1.0, 2.0, 3.0, 4.0], 1);
1235 assert_eq!(ring_buf.data, &[2.0, 3.0, 4.0, 1.0]);
1236 ring_buf.write_latest_2(&[-2.0, -1.0], &[0.0, 1.0, 2.0, 3.0, 4.0], 1);
1237 assert_eq!(ring_buf.data, &[1.0, 2.0, 3.0, 4.0]);
1238 ring_buf.write_latest_2(&[-2.0, -1.0], &[0.0, 1.0], 3);
1239 assert_eq!(ring_buf.data, &[-1.0, 0.0, 1.0, -2.0]);
1240 ring_buf.write_latest_2(&[0.0, 1.0], &[2.0], 3);
1241 assert_eq!(ring_buf.data, &[1.0, 2.0, 1.0, 0.0]);
1242 ring_buf.write_latest_2(&[1.0, 2.0, 3.0, 4.0], &[], 0);
1243 assert_eq!(ring_buf.data, &[1.0, 2.0, 3.0, 4.0]);
1244 ring_buf.write_latest_2(&[1.0, 2.0], &[], 2);
1245 assert_eq!(ring_buf.data, &[1.0, 2.0, 1.0, 2.0]);
1246 ring_buf.write_latest_2(&[], &[], 2);
1247 assert_eq!(ring_buf.data, &[1.0, 2.0, 1.0, 2.0]);
1248 ring_buf.write_latest_2(&[1.0, 2.0, 3.0, 4.0, 5.0], &[], 1);
1249 assert_eq!(ring_buf.data, &[4.0, 5.0, 2.0, 3.0]);
1250 ring_buf.write_latest_2(&[1.0, 2.0, 3.0, 4.0, 5.0], &[6.0], 2);
1251 assert_eq!(ring_buf.data, &[3.0, 4.0, 5.0, 6.0]);
1252 ring_buf.write_latest_2(&[1.0, 2.0, 3.0, 4.0, 5.0], &[6.0, 7.0], 2);
1253 assert_eq!(ring_buf.data, &[7.0, 4.0, 5.0, 6.0]);
1254 ring_buf.write_latest_2(&[1.0, 2.0, 3.0, 4.0, 5.0], &[6.0, 7.0, 8.0, 9.0, 10.0], 3);
1255 assert_eq!(ring_buf.data, &[10.0, 7.0, 8.0, 9.0]);
1256 }
1257
1258 #[test]
1259 fn slice_ring_buf_ref_write_latest() {
1260 let mut data = Aligned324([0.0f32; 4]);
1261 let mut ring_buf = SliceRbRefMut::new(&mut data.0);
1262
1263 let input = [0.0f32, 1.0, 2.0, 3.0];
1264
1265 ring_buf.write_latest(&input, 0);
1266 assert_eq!(ring_buf.data, &[0.0, 1.0, 2.0, 3.0]);
1267 ring_buf.write_latest(&input, 1);
1268 assert_eq!(ring_buf.data, &[3.0, 0.0, 1.0, 2.0]);
1269 ring_buf.write_latest(&input, 2);
1270 assert_eq!(ring_buf.data, &[2.0, 3.0, 0.0, 1.0]);
1271 ring_buf.write_latest(&input, 3);
1272 assert_eq!(ring_buf.data, &[1.0, 2.0, 3.0, 0.0]);
1273 ring_buf.write_latest(&input, 4);
1274 assert_eq!(ring_buf.data, &[0.0, 1.0, 2.0, 3.0]);
1275
1276 let input = [0.0f32, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
1277
1278 ring_buf.write_latest(&input, 0);
1279 assert_eq!(ring_buf.data, &[4.0, 5.0, 6.0, 7.0]);
1280 ring_buf.write_latest(&input, 1);
1281 assert_eq!(ring_buf.data, &[7.0, 4.0, 5.0, 6.0]);
1282 ring_buf.write_latest(&input, 2);
1283 assert_eq!(ring_buf.data, &[6.0, 7.0, 4.0, 5.0]);
1284 ring_buf.write_latest(&input, 3);
1285 assert_eq!(ring_buf.data, &[5.0, 6.0, 7.0, 4.0]);
1286 ring_buf.write_latest(&input, 4);
1287 assert_eq!(ring_buf.data, &[4.0, 5.0, 6.0, 7.0]);
1288
1289 let input = [0.0f32, 1.0];
1290
1291 ring_buf.write_latest(&input, 0);
1292 assert_eq!(ring_buf.data, &[0.0, 1.0, 6.0, 7.0]);
1293 ring_buf.write_latest(&input, 1);
1294 assert_eq!(ring_buf.data, &[0.0, 0.0, 1.0, 7.0]);
1295 ring_buf.write_latest(&input, 2);
1296 assert_eq!(ring_buf.data, &[0.0, 0.0, 0.0, 1.0]);
1297 ring_buf.write_latest(&input, 3);
1298 assert_eq!(ring_buf.data, &[1.0, 0.0, 0.0, 0.0]);
1299 ring_buf.write_latest(&input, 4);
1300 assert_eq!(ring_buf.data, &[0.0, 1.0, 0.0, 0.0]);
1301
1302 let aligned_input = Aligned1([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
1303 ring_buf.write_latest(&aligned_input.0, 0);
1304 assert_eq!(ring_buf.data, &[12.0, 13.0, 14.0, 15.0]);
1305
1306 let aligned_input = Aligned2([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
1307 ring_buf.write_latest(&aligned_input.0, 0);
1308 assert_eq!(ring_buf.data, &[12.0, 13.0, 14.0, 15.0]);
1309
1310 let aligned_input = Aligned4([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
1311 ring_buf.write_latest(&aligned_input.0, 0);
1312 assert_eq!(ring_buf.data, &[12.0, 13.0, 14.0, 15.0]);
1313
1314 let aligned_input = Aligned8([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
1315 ring_buf.write_latest(&aligned_input.0, 0);
1316 assert_eq!(ring_buf.data, &[12.0, 13.0, 14.0, 15.0]);
1317
1318 let aligned_input = Aligned16([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
1319 ring_buf.write_latest(&aligned_input.0, 0);
1320 assert_eq!(ring_buf.data, &[12.0, 13.0, 14.0, 15.0]);
1321
1322 let aligned_input = Aligned32([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
1323 ring_buf.write_latest(&aligned_input.0, 0);
1324 assert_eq!(ring_buf.data, &[12.0, 13.0, 14.0, 15.0]);
1325
1326 let aligned_input = Aligned64([8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0]);
1327 ring_buf.write_latest(&aligned_input.0, 0);
1328 assert_eq!(ring_buf.data, &[12.0, 13.0, 14.0, 15.0]);
1329 }
1330
1331 #[test]
1332 fn slice_ring_buf_ref_as_slices_len() {
1333 let mut data = [0.0f32, 1.0, 2.0, 3.0];
1334 let ring_buf = SliceRbRef::new(&mut data);
1335
1336 let (s1, s2) = ring_buf.as_slices_len(0, 0);
1337 assert_eq!(s1, &[]);
1338 assert_eq!(s2, &[]);
1339 let (s1, s2) = ring_buf.as_slices_len(0, 1);
1340 assert_eq!(s1, &[0.0]);
1341 assert_eq!(s2, &[]);
1342 let (s1, s2) = ring_buf.as_slices_len(0, 2);
1343 assert_eq!(s1, &[0.0, 1.0]);
1344 assert_eq!(s2, &[]);
1345 let (s1, s2) = ring_buf.as_slices_len(0, 3);
1346 assert_eq!(s1, &[0.0, 1.0, 2.0]);
1347 assert_eq!(s2, &[]);
1348 let (s1, s2) = ring_buf.as_slices_len(0, 4);
1349 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1350 assert_eq!(s2, &[]);
1351 let (s1, s2) = ring_buf.as_slices_len(0, 5);
1352 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1353 assert_eq!(s2, &[]);
1354
1355 let (s1, s2) = ring_buf.as_slices_len(1, 0);
1356 assert_eq!(s1, &[]);
1357 assert_eq!(s2, &[]);
1358 let (s1, s2) = ring_buf.as_slices_len(1, 1);
1359 assert_eq!(s1, &[1.0]);
1360 assert_eq!(s2, &[]);
1361 let (s1, s2) = ring_buf.as_slices_len(1, 2);
1362 assert_eq!(s1, &[1.0, 2.0]);
1363 assert_eq!(s2, &[]);
1364 let (s1, s2) = ring_buf.as_slices_len(1, 3);
1365 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1366 assert_eq!(s2, &[]);
1367 let (s1, s2) = ring_buf.as_slices_len(1, 4);
1368 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1369 assert_eq!(s2, &[0.0]);
1370 let (s1, s2) = ring_buf.as_slices_len(1, 5);
1371 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1372 assert_eq!(s2, &[0.0]);
1373
1374 let (s1, s2) = ring_buf.as_slices_len(2, 0);
1375 assert_eq!(s1, &[]);
1376 assert_eq!(s2, &[]);
1377 let (s1, s2) = ring_buf.as_slices_len(2, 1);
1378 assert_eq!(s1, &[2.0]);
1379 assert_eq!(s2, &[]);
1380 let (s1, s2) = ring_buf.as_slices_len(2, 2);
1381 assert_eq!(s1, &[2.0, 3.0]);
1382 assert_eq!(s2, &[]);
1383 let (s1, s2) = ring_buf.as_slices_len(2, 3);
1384 assert_eq!(s1, &[2.0, 3.0]);
1385 assert_eq!(s2, &[0.0]);
1386 let (s1, s2) = ring_buf.as_slices_len(2, 4);
1387 assert_eq!(s1, &[2.0, 3.0]);
1388 assert_eq!(s2, &[0.0, 1.0]);
1389 let (s1, s2) = ring_buf.as_slices_len(2, 5);
1390 assert_eq!(s1, &[2.0, 3.0]);
1391 assert_eq!(s2, &[0.0, 1.0]);
1392
1393 let (s1, s2) = ring_buf.as_slices_len(3, 0);
1394 assert_eq!(s1, &[]);
1395 assert_eq!(s2, &[]);
1396 let (s1, s2) = ring_buf.as_slices_len(3, 1);
1397 assert_eq!(s1, &[3.0]);
1398 assert_eq!(s2, &[]);
1399 let (s1, s2) = ring_buf.as_slices_len(3, 2);
1400 assert_eq!(s1, &[3.0]);
1401 assert_eq!(s2, &[0.0]);
1402 let (s1, s2) = ring_buf.as_slices_len(3, 3);
1403 assert_eq!(s1, &[3.0]);
1404 assert_eq!(s2, &[0.0, 1.0]);
1405 let (s1, s2) = ring_buf.as_slices_len(3, 4);
1406 assert_eq!(s1, &[3.0]);
1407 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1408 let (s1, s2) = ring_buf.as_slices_len(3, 5);
1409 assert_eq!(s1, &[3.0]);
1410 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1411
1412 let (s1, s2) = ring_buf.as_slices_len(4, 0);
1413 assert_eq!(s1, &[]);
1414 assert_eq!(s2, &[]);
1415 let (s1, s2) = ring_buf.as_slices_len(4, 1);
1416 assert_eq!(s1, &[0.0]);
1417 assert_eq!(s2, &[]);
1418 let (s1, s2) = ring_buf.as_slices_len(4, 2);
1419 assert_eq!(s1, &[0.0, 1.0]);
1420 assert_eq!(s2, &[]);
1421 let (s1, s2) = ring_buf.as_slices_len(4, 3);
1422 assert_eq!(s1, &[0.0, 1.0, 2.0]);
1423 assert_eq!(s2, &[]);
1424 let (s1, s2) = ring_buf.as_slices_len(4, 4);
1425 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1426 assert_eq!(s2, &[]);
1427 let (s1, s2) = ring_buf.as_slices_len(4, 5);
1428 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1429 assert_eq!(s2, &[]);
1430 }
1431
1432 #[test]
1433 fn slice_ring_buf_ref_as_slices_latest() {
1434 let mut data = [0.0f32, 1.0, 2.0, 3.0];
1435 let ring_buf = SliceRbRefMut::new(&mut data);
1436
1437 let (s1, s2) = ring_buf.as_slices_latest(0, 0);
1438 assert_eq!(s1, &[]);
1439 assert_eq!(s2, &[]);
1440 let (s1, s2) = ring_buf.as_slices_latest(0, 1);
1441 assert_eq!(s1, &[0.0]);
1442 assert_eq!(s2, &[]);
1443 let (s1, s2) = ring_buf.as_slices_latest(0, 2);
1444 assert_eq!(s1, &[0.0, 1.0]);
1445 assert_eq!(s2, &[]);
1446 let (s1, s2) = ring_buf.as_slices_latest(0, 3);
1447 assert_eq!(s1, &[0.0, 1.0, 2.0]);
1448 assert_eq!(s2, &[]);
1449 let (s1, s2) = ring_buf.as_slices_latest(0, 4);
1450 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1451 assert_eq!(s2, &[]);
1452 let (s1, s2) = ring_buf.as_slices_latest(0, 5);
1453 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1454 assert_eq!(s2, &[0.0]);
1455 let (s1, s2) = ring_buf.as_slices_latest(0, 6);
1456 assert_eq!(s1, &[2.0, 3.0]);
1457 assert_eq!(s2, &[0.0, 1.0]);
1458 let (s1, s2) = ring_buf.as_slices_latest(0, 7);
1459 assert_eq!(s1, &[3.0]);
1460 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1461 let (s1, s2) = ring_buf.as_slices_latest(0, 10);
1462 assert_eq!(s1, &[2.0, 3.0]);
1463 assert_eq!(s2, &[0.0, 1.0]);
1464
1465 let (s1, s2) = ring_buf.as_slices_latest(1, 0);
1466 assert_eq!(s1, &[]);
1467 assert_eq!(s2, &[]);
1468 let (s1, s2) = ring_buf.as_slices_latest(1, 1);
1469 assert_eq!(s1, &[1.0]);
1470 assert_eq!(s2, &[]);
1471 let (s1, s2) = ring_buf.as_slices_latest(1, 2);
1472 assert_eq!(s1, &[1.0, 2.0]);
1473 assert_eq!(s2, &[]);
1474 let (s1, s2) = ring_buf.as_slices_latest(1, 3);
1475 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1476 assert_eq!(s2, &[]);
1477 let (s1, s2) = ring_buf.as_slices_latest(1, 4);
1478 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1479 assert_eq!(s2, &[0.0]);
1480 let (s1, s2) = ring_buf.as_slices_latest(1, 5);
1481 assert_eq!(s1, &[2.0, 3.0]);
1482 assert_eq!(s2, &[0.0, 1.0]);
1483 let (s1, s2) = ring_buf.as_slices_latest(1, 6);
1484 assert_eq!(s1, &[3.0]);
1485 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1486 let (s1, s2) = ring_buf.as_slices_latest(1, 7);
1487 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1488 assert_eq!(s2, &[]);
1489 let (s1, s2) = ring_buf.as_slices_latest(1, 10);
1490 assert_eq!(s1, &[3.0]);
1491 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1492
1493 let (s1, s2) = ring_buf.as_slices_latest(2, 0);
1494 assert_eq!(s1, &[]);
1495 assert_eq!(s2, &[]);
1496 let (s1, s2) = ring_buf.as_slices_latest(2, 1);
1497 assert_eq!(s1, &[2.0]);
1498 assert_eq!(s2, &[]);
1499 let (s1, s2) = ring_buf.as_slices_latest(2, 2);
1500 assert_eq!(s1, &[2.0, 3.0]);
1501 assert_eq!(s2, &[]);
1502 let (s1, s2) = ring_buf.as_slices_latest(2, 3);
1503 assert_eq!(s1, &[2.0, 3.0]);
1504 assert_eq!(s2, &[0.0]);
1505 let (s1, s2) = ring_buf.as_slices_latest(2, 4);
1506 assert_eq!(s1, &[2.0, 3.0]);
1507 assert_eq!(s2, &[0.0, 1.0]);
1508 let (s1, s2) = ring_buf.as_slices_latest(2, 5);
1509 assert_eq!(s1, &[3.0]);
1510 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1511 let (s1, s2) = ring_buf.as_slices_latest(2, 6);
1512 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1513 assert_eq!(s2, &[]);
1514 let (s1, s2) = ring_buf.as_slices_latest(2, 7);
1515 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1516 assert_eq!(s2, &[0.0]);
1517 let (s1, s2) = ring_buf.as_slices_latest(2, 10);
1518 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1519 assert_eq!(s2, &[]);
1520
1521 let (s1, s2) = ring_buf.as_slices_latest(3, 0);
1522 assert_eq!(s1, &[]);
1523 assert_eq!(s2, &[]);
1524 let (s1, s2) = ring_buf.as_slices_latest(3, 1);
1525 assert_eq!(s1, &[3.0]);
1526 assert_eq!(s2, &[]);
1527 let (s1, s2) = ring_buf.as_slices_latest(3, 2);
1528 assert_eq!(s1, &[3.0]);
1529 assert_eq!(s2, &[0.0]);
1530 let (s1, s2) = ring_buf.as_slices_latest(3, 3);
1531 assert_eq!(s1, &[3.0]);
1532 assert_eq!(s2, &[0.0, 1.0]);
1533 let (s1, s2) = ring_buf.as_slices_latest(3, 4);
1534 assert_eq!(s1, &[3.0]);
1535 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1536 let (s1, s2) = ring_buf.as_slices_latest(3, 5);
1537 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1538 assert_eq!(s2, &[]);
1539 let (s1, s2) = ring_buf.as_slices_latest(3, 6);
1540 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1541 assert_eq!(s2, &[0.0]);
1542 let (s1, s2) = ring_buf.as_slices_latest(3, 7);
1543 assert_eq!(s1, &[2.0, 3.0]);
1544 assert_eq!(s2, &[0.0, 1.0]);
1545 let (s1, s2) = ring_buf.as_slices_latest(3, 10);
1546 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1547 assert_eq!(s2, &[0.0]);
1548
1549 let (s1, s2) = ring_buf.as_slices_latest(4, 0);
1550 assert_eq!(s1, &[]);
1551 assert_eq!(s2, &[]);
1552 let (s1, s2) = ring_buf.as_slices_latest(4, 1);
1553 assert_eq!(s1, &[0.0]);
1554 assert_eq!(s2, &[]);
1555 let (s1, s2) = ring_buf.as_slices_latest(4, 2);
1556 assert_eq!(s1, &[0.0, 1.0]);
1557 assert_eq!(s2, &[]);
1558 let (s1, s2) = ring_buf.as_slices_latest(4, 3);
1559 assert_eq!(s1, &[0.0, 1.0, 2.0]);
1560 assert_eq!(s2, &[]);
1561 let (s1, s2) = ring_buf.as_slices_latest(4, 4);
1562 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1563 assert_eq!(s2, &[]);
1564 let (s1, s2) = ring_buf.as_slices_latest(4, 5);
1565 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1566 assert_eq!(s2, &[0.0]);
1567 let (s1, s2) = ring_buf.as_slices_latest(4, 6);
1568 assert_eq!(s1, &[2.0, 3.0]);
1569 assert_eq!(s2, &[0.0, 1.0]);
1570 let (s1, s2) = ring_buf.as_slices_latest(4, 7);
1571 assert_eq!(s1, &[3.0]);
1572 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1573 let (s1, s2) = ring_buf.as_slices_latest(4, 10);
1574 assert_eq!(s1, &[2.0, 3.0]);
1575 assert_eq!(s2, &[0.0, 1.0]);
1576 }
1577
1578 #[test]
1579 fn slice_ring_buf_ref_as_mut_slices_len() {
1580 let mut data = [0.0f32, 1.0, 2.0, 3.0];
1581 let mut ring_buf = SliceRbRefMut::new(&mut data);
1582
1583 let (s1, s2) = ring_buf.as_mut_slices_len(0, 0);
1584 assert_eq!(s1, &[]);
1585 assert_eq!(s2, &[]);
1586 let (s1, s2) = ring_buf.as_mut_slices_len(0, 1);
1587 assert_eq!(s1, &[0.0]);
1588 assert_eq!(s2, &[]);
1589 let (s1, s2) = ring_buf.as_mut_slices_len(0, 2);
1590 assert_eq!(s1, &[0.0, 1.0]);
1591 assert_eq!(s2, &[]);
1592 let (s1, s2) = ring_buf.as_mut_slices_len(0, 3);
1593 assert_eq!(s1, &[0.0, 1.0, 2.0]);
1594 assert_eq!(s2, &[]);
1595 let (s1, s2) = ring_buf.as_mut_slices_len(0, 4);
1596 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1597 assert_eq!(s2, &[]);
1598 let (s1, s2) = ring_buf.as_mut_slices_len(0, 5);
1599 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1600 assert_eq!(s2, &[]);
1601
1602 let (s1, s2) = ring_buf.as_mut_slices_len(1, 0);
1603 assert_eq!(s1, &[]);
1604 assert_eq!(s2, &[]);
1605 let (s1, s2) = ring_buf.as_mut_slices_len(1, 1);
1606 assert_eq!(s1, &[1.0]);
1607 assert_eq!(s2, &[]);
1608 let (s1, s2) = ring_buf.as_mut_slices_len(1, 2);
1609 assert_eq!(s1, &[1.0, 2.0]);
1610 assert_eq!(s2, &[]);
1611 let (s1, s2) = ring_buf.as_mut_slices_len(1, 3);
1612 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1613 assert_eq!(s2, &[]);
1614 let (s1, s2) = ring_buf.as_mut_slices_len(1, 4);
1615 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1616 assert_eq!(s2, &[0.0]);
1617 let (s1, s2) = ring_buf.as_mut_slices_len(1, 5);
1618 assert_eq!(s1, &[1.0, 2.0, 3.0]);
1619 assert_eq!(s2, &[0.0]);
1620
1621 let (s1, s2) = ring_buf.as_mut_slices_len(2, 0);
1622 assert_eq!(s1, &[]);
1623 assert_eq!(s2, &[]);
1624 let (s1, s2) = ring_buf.as_mut_slices_len(2, 1);
1625 assert_eq!(s1, &[2.0]);
1626 assert_eq!(s2, &[]);
1627 let (s1, s2) = ring_buf.as_mut_slices_len(2, 2);
1628 assert_eq!(s1, &[2.0, 3.0]);
1629 assert_eq!(s2, &[]);
1630 let (s1, s2) = ring_buf.as_mut_slices_len(2, 3);
1631 assert_eq!(s1, &[2.0, 3.0]);
1632 assert_eq!(s2, &[0.0]);
1633 let (s1, s2) = ring_buf.as_mut_slices_len(2, 4);
1634 assert_eq!(s1, &[2.0, 3.0]);
1635 assert_eq!(s2, &[0.0, 1.0]);
1636 let (s1, s2) = ring_buf.as_mut_slices_len(2, 5);
1637 assert_eq!(s1, &[2.0, 3.0]);
1638 assert_eq!(s2, &[0.0, 1.0]);
1639
1640 let (s1, s2) = ring_buf.as_mut_slices_len(3, 0);
1641 assert_eq!(s1, &[]);
1642 assert_eq!(s2, &[]);
1643 let (s1, s2) = ring_buf.as_mut_slices_len(3, 1);
1644 assert_eq!(s1, &[3.0]);
1645 assert_eq!(s2, &[]);
1646 let (s1, s2) = ring_buf.as_mut_slices_len(3, 2);
1647 assert_eq!(s1, &[3.0]);
1648 assert_eq!(s2, &[0.0]);
1649 let (s1, s2) = ring_buf.as_mut_slices_len(3, 3);
1650 assert_eq!(s1, &[3.0]);
1651 assert_eq!(s2, &[0.0, 1.0]);
1652 let (s1, s2) = ring_buf.as_mut_slices_len(3, 4);
1653 assert_eq!(s1, &[3.0]);
1654 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1655 let (s1, s2) = ring_buf.as_mut_slices_len(3, 5);
1656 assert_eq!(s1, &[3.0]);
1657 assert_eq!(s2, &[0.0, 1.0, 2.0]);
1658
1659 let (s1, s2) = ring_buf.as_mut_slices_len(4, 0);
1660 assert_eq!(s1, &[]);
1661 assert_eq!(s2, &[]);
1662 let (s1, s2) = ring_buf.as_mut_slices_len(4, 1);
1663 assert_eq!(s1, &[0.0]);
1664 assert_eq!(s2, &[]);
1665 let (s1, s2) = ring_buf.as_mut_slices_len(4, 2);
1666 assert_eq!(s1, &[0.0, 1.0]);
1667 assert_eq!(s2, &[]);
1668 let (s1, s2) = ring_buf.as_mut_slices_len(4, 3);
1669 assert_eq!(s1, &[0.0, 1.0, 2.0]);
1670 assert_eq!(s2, &[]);
1671 let (s1, s2) = ring_buf.as_mut_slices_len(4, 4);
1672 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1673 assert_eq!(s2, &[]);
1674 let (s1, s2) = ring_buf.as_mut_slices_len(4, 5);
1675 assert_eq!(s1, &[0.0, 1.0, 2.0, 3.0]);
1676 assert_eq!(s2, &[]);
1677 }
1678
1679 #[test]
1680 fn slice_ring_buf_ref_as_mut_slices_latest() {
1681 let mut data = [0.0f32, 1.0, 2.0, 3.0];
1682 let mut ring_buf = SliceRbRefMut::new(&mut data);
1683
1684 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 0);
1685 assert_eq!(s1, &mut []);
1686 assert_eq!(s2, &mut []);
1687 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 1);
1688 assert_eq!(s1, &mut [0.0]);
1689 assert_eq!(s2, &mut []);
1690 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 2);
1691 assert_eq!(s1, &mut [0.0, 1.0]);
1692 assert_eq!(s2, &mut []);
1693 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 3);
1694 assert_eq!(s1, &mut [0.0, 1.0, 2.0]);
1695 assert_eq!(s2, &mut []);
1696 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 4);
1697 assert_eq!(s1, &mut [0.0, 1.0, 2.0, 3.0]);
1698 assert_eq!(s2, &mut []);
1699 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 5);
1700 assert_eq!(s1, &mut [1.0, 2.0, 3.0]);
1701 assert_eq!(s2, &mut [0.0]);
1702 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 6);
1703 assert_eq!(s1, &mut [2.0, 3.0]);
1704 assert_eq!(s2, &mut [0.0, 1.0]);
1705 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 7);
1706 assert_eq!(s1, &mut [3.0]);
1707 assert_eq!(s2, &mut [0.0, 1.0, 2.0]);
1708 let (s1, s2) = ring_buf.as_mut_slices_latest(0, 10);
1709 assert_eq!(s1, &mut [2.0, 3.0]);
1710 assert_eq!(s2, &mut [0.0, 1.0]);
1711
1712 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 0);
1713 assert_eq!(s1, &mut []);
1714 assert_eq!(s2, &mut []);
1715 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 1);
1716 assert_eq!(s1, &mut [1.0]);
1717 assert_eq!(s2, &mut []);
1718 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 2);
1719 assert_eq!(s1, &mut [1.0, 2.0]);
1720 assert_eq!(s2, &mut []);
1721 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 3);
1722 assert_eq!(s1, &mut [1.0, 2.0, 3.0]);
1723 assert_eq!(s2, &mut []);
1724 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 4);
1725 assert_eq!(s1, &mut [1.0, 2.0, 3.0]);
1726 assert_eq!(s2, &mut [0.0]);
1727 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 5);
1728 assert_eq!(s1, &mut [2.0, 3.0]);
1729 assert_eq!(s2, &mut [0.0, 1.0]);
1730 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 6);
1731 assert_eq!(s1, &mut [3.0]);
1732 assert_eq!(s2, &mut [0.0, 1.0, 2.0]);
1733 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 7);
1734 assert_eq!(s1, &mut [0.0, 1.0, 2.0, 3.0]);
1735 assert_eq!(s2, &mut []);
1736 let (s1, s2) = ring_buf.as_mut_slices_latest(1, 10);
1737 assert_eq!(s1, &mut [3.0]);
1738 assert_eq!(s2, &mut [0.0, 1.0, 2.0]);
1739
1740 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 0);
1741 assert_eq!(s1, &mut []);
1742 assert_eq!(s2, &mut []);
1743 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 1);
1744 assert_eq!(s1, &mut [2.0]);
1745 assert_eq!(s2, &mut []);
1746 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 2);
1747 assert_eq!(s1, &mut [2.0, 3.0]);
1748 assert_eq!(s2, &mut []);
1749 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 3);
1750 assert_eq!(s1, &mut [2.0, 3.0]);
1751 assert_eq!(s2, &mut [0.0]);
1752 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 4);
1753 assert_eq!(s1, &mut [2.0, 3.0]);
1754 assert_eq!(s2, &mut [0.0, 1.0]);
1755 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 5);
1756 assert_eq!(s1, &mut [3.0]);
1757 assert_eq!(s2, &mut [0.0, 1.0, 2.0]);
1758 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 6);
1759 assert_eq!(s1, &mut [0.0, 1.0, 2.0, 3.0]);
1760 assert_eq!(s2, &mut []);
1761 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 7);
1762 assert_eq!(s1, &mut [1.0, 2.0, 3.0]);
1763 assert_eq!(s2, &mut [0.0]);
1764 let (s1, s2) = ring_buf.as_mut_slices_latest(2, 10);
1765 assert_eq!(s1, &mut [0.0, 1.0, 2.0, 3.0]);
1766 assert_eq!(s2, &mut []);
1767
1768 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 0);
1769 assert_eq!(s1, &mut []);
1770 assert_eq!(s2, &mut []);
1771 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 1);
1772 assert_eq!(s1, &mut [3.0]);
1773 assert_eq!(s2, &mut []);
1774 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 2);
1775 assert_eq!(s1, &mut [3.0]);
1776 assert_eq!(s2, &mut [0.0]);
1777 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 3);
1778 assert_eq!(s1, &mut [3.0]);
1779 assert_eq!(s2, &mut [0.0, 1.0]);
1780 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 4);
1781 assert_eq!(s1, &mut [3.0]);
1782 assert_eq!(s2, &mut [0.0, 1.0, 2.0]);
1783 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 5);
1784 assert_eq!(s1, &mut [0.0, 1.0, 2.0, 3.0]);
1785 assert_eq!(s2, &mut []);
1786 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 6);
1787 assert_eq!(s1, &mut [1.0, 2.0, 3.0]);
1788 assert_eq!(s2, &mut [0.0]);
1789 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 7);
1790 assert_eq!(s1, &mut [2.0, 3.0]);
1791 assert_eq!(s2, &mut [0.0, 1.0]);
1792 let (s1, s2) = ring_buf.as_mut_slices_latest(3, 10);
1793 assert_eq!(s1, &mut [1.0, 2.0, 3.0]);
1794 assert_eq!(s2, &mut [0.0]);
1795
1796 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 0);
1797 assert_eq!(s1, &mut []);
1798 assert_eq!(s2, &mut []);
1799 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 1);
1800 assert_eq!(s1, &mut [0.0]);
1801 assert_eq!(s2, &mut []);
1802 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 2);
1803 assert_eq!(s1, &mut [0.0, 1.0]);
1804 assert_eq!(s2, &mut []);
1805 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 3);
1806 assert_eq!(s1, &mut [0.0, 1.0, 2.0]);
1807 assert_eq!(s2, &mut []);
1808 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 4);
1809 assert_eq!(s1, &mut [0.0, 1.0, 2.0, 3.0]);
1810 assert_eq!(s2, &mut []);
1811 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 5);
1812 assert_eq!(s1, &mut [1.0, 2.0, 3.0]);
1813 assert_eq!(s2, &mut [0.0]);
1814 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 6);
1815 assert_eq!(s1, &mut [2.0, 3.0]);
1816 assert_eq!(s2, &mut [0.0, 1.0]);
1817 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 7);
1818 assert_eq!(s1, &mut [3.0]);
1819 assert_eq!(s2, &mut [0.0, 1.0, 2.0]);
1820 let (s1, s2) = ring_buf.as_mut_slices_latest(4, 10);
1821 assert_eq!(s1, &mut [2.0, 3.0]);
1822 assert_eq!(s2, &mut [0.0, 1.0]);
1823 }
1824
1825 #[test]
1826 fn slice_ring_buf_ref_read_into() {
1827 let mut data = Aligned324([0.0f32, 1.0, 2.0, 3.0]);
1828 let ring_buf = SliceRbRef::new(&mut data.0);
1829
1830 let mut output = [0.0f32; 4];
1831
1832 ring_buf.read_into(&mut output, 0);
1833 assert_eq!(output, [0.0, 1.0, 2.0, 3.0]);
1834 ring_buf.read_into(&mut output, 1);
1835 assert_eq!(output, [1.0, 2.0, 3.0, 0.0]);
1836 ring_buf.read_into(&mut output, 2);
1837 assert_eq!(output, [2.0, 3.0, 0.0, 1.0]);
1838 ring_buf.read_into(&mut output, 3);
1839 assert_eq!(output, [3.0, 0.0, 1.0, 2.0]);
1840 ring_buf.read_into(&mut output, 4);
1841 assert_eq!(output, [0.0, 1.0, 2.0, 3.0]);
1842
1843 let mut output = [0.0f32; 3];
1844
1845 ring_buf.read_into(&mut output, 0);
1846 assert_eq!(output, [0.0, 1.0, 2.0]);
1847 ring_buf.read_into(&mut output, 1);
1848 assert_eq!(output, [1.0, 2.0, 3.0]);
1849 ring_buf.read_into(&mut output, 2);
1850 assert_eq!(output, [2.0, 3.0, 0.0]);
1851 ring_buf.read_into(&mut output, 3);
1852 assert_eq!(output, [3.0, 0.0, 1.0]);
1853 ring_buf.read_into(&mut output, 4);
1854 assert_eq!(output, [0.0, 1.0, 2.0]);
1855
1856 let mut output = [0.0f32; 5];
1857
1858 ring_buf.read_into(&mut output, 0);
1859 assert_eq!(output, [0.0, 1.0, 2.0, 3.0, 0.0]);
1860 ring_buf.read_into(&mut output, 1);
1861 assert_eq!(output, [1.0, 2.0, 3.0, 0.0, 1.0]);
1862 ring_buf.read_into(&mut output, 2);
1863 assert_eq!(output, [2.0, 3.0, 0.0, 1.0, 2.0]);
1864 ring_buf.read_into(&mut output, 3);
1865 assert_eq!(output, [3.0, 0.0, 1.0, 2.0, 3.0]);
1866 ring_buf.read_into(&mut output, 4);
1867 assert_eq!(output, [0.0, 1.0, 2.0, 3.0, 0.0]);
1868
1869 let mut output = [0.0f32; 10];
1870
1871 ring_buf.read_into(&mut output, 0);
1872 assert_eq!(output, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0]);
1873 ring_buf.read_into(&mut output, 3);
1874 assert_eq!(output, [3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0]);
1875
1876 let mut aligned_output = Aligned1([0.0; 8]);
1877 ring_buf.read_into(&mut aligned_output.0, 0);
1878 assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
1879
1880 let mut aligned_output = Aligned2([0.0; 8]);
1881 ring_buf.read_into(&mut aligned_output.0, 0);
1882 assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
1883
1884 let mut aligned_output = Aligned4([0.0; 8]);
1885 ring_buf.read_into(&mut aligned_output.0, 0);
1886 assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
1887
1888 let mut aligned_output = Aligned8([0.0; 8]);
1889 ring_buf.read_into(&mut aligned_output.0, 0);
1890 assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
1891
1892 let mut aligned_output = Aligned16([0.0; 8]);
1893 ring_buf.read_into(&mut aligned_output.0, 0);
1894 assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
1895
1896 let mut aligned_output = Aligned32([0.0; 8]);
1897 ring_buf.read_into(&mut aligned_output.0, 0);
1898 assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
1899
1900 let mut aligned_output = Aligned64([0.0; 8]);
1901 ring_buf.read_into(&mut aligned_output.0, 0);
1902 assert_eq!(aligned_output.0, [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]);
1903 }
1904}