Skip to content

Commit b2c7e26

Browse files
committed
Replace HistoryBuf::as_slice() to as_unordered_slice()
Some method names already contain statements on ordering like `oldest_ordered()`. In this context a method returning an unordered slice should state this clearly.
1 parent ccf2896 commit b2c7e26

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
- Implement `TryFrom` for `Deque` from array.
1515
- Switch from `serde` to `serde_core` for enabling faster compilations.
1616
- Removed `impl Deref` for `HistoryBuf` to make accessing the raw backing array explicit (use `as_slice`).
17+
- Replace `HistoryBuf::as_slice` with `HistoryBuf::as_unordered_slice` to clarify ordering.
1718

1819
## [v0.9.1] - 2025-08-19
1920

src/history_buf.rs

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
//! // The most recent written element is a four.
2222
//! assert_eq!(buf.recent(), Some(&4));
2323
//!
24-
//! // To access all elements in an unspecified order, use `as_slice()`.
25-
//! for el in buf.as_slice() {
24+
//! // To access all elements in an unspecified order, use `as_unordered_slice()`.
25+
//! for el in buf.as_unordered_slice() {
2626
//! println!("{:?}", el);
2727
//! }
2828
//!
2929
//! // Now we can prepare an average of all values, which comes out to 4.
30-
//! let avg = buf.as_slice().iter().sum::<usize>() / buf.len();
30+
//! let avg = buf.as_unordered_slice().iter().sum::<usize>() / buf.len();
3131
//! assert_eq!(avg, 4);
3232
//! ```
3333
@@ -178,13 +178,13 @@ pub struct HistoryBufInner<T, S: HistoryBufStorage<T> + ?Sized> {
178178
/// // The most recent written element is a four.
179179
/// assert_eq!(buf.recent(), Some(&4));
180180
///
181-
/// // To access all elements in an unspecified order, use `as_slice()`.
182-
/// for el in buf.as_slice() {
181+
/// // To access all elements in an unspecified order, use `as_unordered_slice()`.
182+
/// for el in buf.as_unordered_slice() {
183183
/// println!("{:?}", el);
184184
/// }
185185
///
186186
/// // Now we can prepare an average of all values, which comes out to 4.
187-
/// let avg = buf.as_slice().iter().sum::<usize>() / buf.len();
187+
/// let avg = buf.as_unordered_slice().iter().sum::<usize>() / buf.len();
188188
/// assert_eq!(avg, 4);
189189
/// ```
190190
pub type HistoryBuf<T, const N: usize> = HistoryBufInner<T, OwnedHistoryBufStorage<T, N>>;
@@ -211,13 +211,13 @@ pub type HistoryBuf<T, const N: usize> = HistoryBufInner<T, OwnedHistoryBufStora
211211
/// // The most recent written element is a four.
212212
/// assert_eq!(buf.recent(), Some(&4));
213213
///
214-
/// // To access all elements in an unspecified order, use `as_slice()`.
215-
/// for el in buf.as_slice() {
214+
/// // To access all elements in an unspecified order, use `as_unordered_slice()`.
215+
/// for el in buf.as_unordered_slice() {
216216
/// println!("{:?}", el);
217217
/// }
218218
///
219219
/// // Now we can prepare an average of all values, which comes out to 4.
220-
/// let avg = buf.as_slice().iter().sum::<usize>() / buf.len();
220+
/// let avg = buf.as_unordered_slice().iter().sum::<usize>() / buf.len();
221221
/// assert_eq!(avg, 4);
222222
/// ```
223223
pub type HistoryBufView<T> = HistoryBufInner<T, ViewHistoryBufStorage<T>>;
@@ -269,7 +269,7 @@ where
269269
/// // Allocate a 16-element buffer on the stack
270270
/// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new_with(4);
271271
/// // All elements are four
272-
/// assert_eq!(x.as_slice(), [4; 16]);
272+
/// assert_eq!(x.as_unordered_slice(), [4; 16]);
273273
/// ```
274274
#[inline]
275275
pub fn new_with(t: T) -> Self {
@@ -478,7 +478,16 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
478478

479479
/// Returns the array slice backing the buffer, without keeping track
480480
/// of the write position. Therefore, the element order is unspecified.
481+
#[deprecated(
482+
note = "as_slice's name did not explicitly state unspecified ordering of elements. Use as_unordered_slice instead."
483+
)]
481484
pub fn as_slice(&self) -> &[T] {
485+
self.as_unordered_slice()
486+
}
487+
488+
/// Returns the array slice backing the buffer, without keeping track
489+
/// of the write position. Therefore, the element order is unspecified.
490+
pub fn as_unordered_slice(&self) -> &[T] {
482491
unsafe { slice::from_raw_parts(self.data.borrow().as_ptr().cast(), self.len()) }
483492
}
484493

@@ -495,7 +504,7 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
495504
/// assert_eq!(buffer.as_slices(), (&[1, 2, 3][..], &[4, 5, 6][..]));
496505
/// ```
497506
pub fn as_slices(&self) -> (&[T], &[T]) {
498-
let buffer = self.as_slice();
507+
let buffer = self.as_unordered_slice();
499508

500509
if self.filled {
501510
(&buffer[self.write_at..], &buffer[..self.write_at])
@@ -556,7 +565,12 @@ where
556565
{
557566
fn clone(&self) -> Self {
558567
let mut ret = Self::new();
559-
for (new, old) in ret.data.borrow_mut().iter_mut().zip(self.as_slice()) {
568+
for (new, old) in ret
569+
.data
570+
.borrow_mut()
571+
.iter_mut()
572+
.zip(self.as_unordered_slice())
573+
{
560574
new.write(old.clone());
561575
}
562576
ret.filled = self.filled;
@@ -574,7 +588,7 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> Drop for HistoryBufInner<T, S> {
574588
impl<T, S: HistoryBufStorage<T> + ?Sized> AsRef<[T]> for HistoryBufInner<T, S> {
575589
#[inline]
576590
fn as_ref(&self) -> &[T] {
577-
self.as_slice()
591+
self.as_unordered_slice()
578592
}
579593
}
580594

@@ -583,7 +597,7 @@ where
583597
T: fmt::Debug,
584598
{
585599
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
586-
<[T] as fmt::Debug>::fmt(self.as_slice(), f)
600+
<[T] as fmt::Debug>::fmt(self.as_unordered_slice(), f)
587601
}
588602
}
589603

@@ -650,11 +664,11 @@ mod tests {
650664
fn new() {
651665
let x: HistoryBuf<u8, 4> = HistoryBuf::new_with(1);
652666
assert_eq!(x.len(), 4);
653-
assert_eq!(x.as_slice(), [1; 4]);
667+
assert_eq!(x.as_unordered_slice(), [1; 4]);
654668
assert!(x.is_full());
655669

656670
let x: HistoryBuf<u8, 4> = HistoryBuf::new();
657-
assert_eq!(x.as_slice(), []);
671+
assert_eq!(x.as_unordered_slice(), []);
658672
assert!(!x.is_full());
659673
}
660674

@@ -663,33 +677,33 @@ mod tests {
663677
let mut x: HistoryBuf<u8, 4> = HistoryBuf::new();
664678
x.write(1);
665679
x.write(4);
666-
assert_eq!(x.as_slice(), [1, 4]);
680+
assert_eq!(x.as_unordered_slice(), [1, 4]);
667681

668682
x.write(5);
669683
x.write(6);
670684
x.write(10);
671-
assert_eq!(x.as_slice(), [10, 4, 5, 6]);
685+
assert_eq!(x.as_unordered_slice(), [10, 4, 5, 6]);
672686

673687
x.extend([11, 12].iter());
674-
assert_eq!(x.as_slice(), [10, 11, 12, 6]);
688+
assert_eq!(x.as_unordered_slice(), [10, 11, 12, 6]);
675689
}
676690

677691
#[test]
678692
fn clear() {
679693
let mut x: HistoryBuf<u8, 4> = HistoryBuf::new_with(1);
680694
x.clear();
681-
assert_eq!(x.as_slice(), []);
695+
assert_eq!(x.as_unordered_slice(), []);
682696

683697
let mut x: HistoryBuf<u8, 4> = HistoryBuf::new();
684698
x.clear_with(1);
685-
assert_eq!(x.as_slice(), [1; 4]);
699+
assert_eq!(x.as_unordered_slice(), [1; 4]);
686700
}
687701

688702
#[test]
689703
fn clone() {
690704
let mut x: HistoryBuf<u8, 3> = HistoryBuf::new();
691705
for i in 0..10 {
692-
assert_eq!(x.as_slice(), x.clone().as_slice());
706+
assert_eq!(x.as_unordered_slice(), x.clone().as_unordered_slice());
693707
x.write(i);
694708
}
695709

@@ -710,17 +724,17 @@ mod tests {
710724
assert_eq!(GLOBAL.load(Ordering::Relaxed), 0);
711725
y.write(InstrumentedClone(0));
712726
assert_eq!(GLOBAL.load(Ordering::Relaxed), 0);
713-
assert_eq!(y.clone().as_slice(), [InstrumentedClone(1)]);
727+
assert_eq!(y.clone().as_unordered_slice(), [InstrumentedClone(1)]);
714728
assert_eq!(GLOBAL.load(Ordering::Relaxed), 1);
715729
y.write(InstrumentedClone(0));
716730
assert_eq!(GLOBAL.load(Ordering::Relaxed), 1);
717731
assert_eq!(
718-
y.clone().as_slice(),
732+
y.clone().as_unordered_slice(),
719733
[InstrumentedClone(1), InstrumentedClone(1)]
720734
);
721735
assert_eq!(GLOBAL.load(Ordering::Relaxed), 3);
722736
assert_eq!(
723-
y.clone().clone().clone().as_slice(),
737+
y.clone().clone().clone().as_unordered_slice(),
724738
[InstrumentedClone(3), InstrumentedClone(3)]
725739
);
726740
assert_eq!(GLOBAL.load(Ordering::Relaxed), 9);
@@ -762,15 +776,27 @@ mod tests {
762776
assert_eq!(x.oldest(), Some(&4));
763777
}
764778

779+
#[allow(deprecated)]
765780
#[test]
766781
fn as_slice() {
767782
let mut x: HistoryBuf<u8, 4> = HistoryBuf::new();
768783

769-
assert_eq!(x.as_slice(), []);
784+
assert_eq!(x.as_slice(), x.as_unordered_slice());
785+
786+
x.extend([1, 2, 3, 4, 5].iter());
787+
788+
assert_eq!(x.as_slice(), x.as_unordered_slice());
789+
}
790+
791+
#[test]
792+
fn as_unordered_slice() {
793+
let mut x: HistoryBuf<u8, 4> = HistoryBuf::new();
794+
795+
assert_eq!(x.as_unordered_slice(), []);
770796

771797
x.extend([1, 2, 3, 4, 5].iter());
772798

773-
assert_eq!(x.as_slice(), [5, 2, 3, 4]);
799+
assert_eq!(x.as_unordered_slice(), [5, 2, 3, 4]);
774800
}
775801

776802
/// Test whether `.as_slices()` behaves as expected.
@@ -900,8 +926,8 @@ mod tests {
900926
x,
901927
y,
902928
"{:?} {:?}",
903-
x.as_slice().iter().collect::<Vec<_>>(),
904-
y.as_slice().iter().collect::<Vec<_>>()
929+
x.as_unordered_slice().iter().collect::<Vec<_>>(),
930+
y.as_unordered_slice().iter().collect::<Vec<_>>()
905931
);
906932
}
907933
}

0 commit comments

Comments
 (0)