Skip to content

Commit ccf2896

Browse files
committed
Remove implementing Deref<Target = [T]> for HistoryBuf
There is already `as_slice()` which gives access to the underlying backing array where the elements are not in the order of writing. With automatic dereferencing, functions like `iter()` or `windows()` are automatically available for `HistoryBuf` and are returning items in an order which does not reflect the write order and will likely surprise users which did not carefully read all the documentation on for `HistoryBuf` like me.
1 parent d29f95c commit ccf2896

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Implement `defmt::Format` for `CapacityError`.
1414
- Implement `TryFrom` for `Deque` from array.
1515
- Switch from `serde` to `serde_core` for enabling faster compilations.
16+
- Removed `impl Deref` for `HistoryBuf` to make accessing the raw backing array explicit (use `as_slice`).
1617

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

src/history_buf.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
use core::fmt;
3535
use core::marker::PhantomData;
3636
use core::mem::MaybeUninit;
37-
use core::ops::Deref;
3837
use core::ptr;
3938
use core::slice;
4039

@@ -572,18 +571,10 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> Drop for HistoryBufInner<T, S> {
572571
}
573572
}
574573

575-
impl<T, S: HistoryBufStorage<T> + ?Sized> Deref for HistoryBufInner<T, S> {
576-
type Target = [T];
577-
578-
fn deref(&self) -> &[T] {
579-
self.as_slice()
580-
}
581-
}
582-
583574
impl<T, S: HistoryBufStorage<T> + ?Sized> AsRef<[T]> for HistoryBufInner<T, S> {
584575
#[inline]
585576
fn as_ref(&self) -> &[T] {
586-
self
577+
self.as_slice()
587578
}
588579
}
589580

@@ -592,7 +583,7 @@ where
592583
T: fmt::Debug,
593584
{
594585
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
595-
<[T] as fmt::Debug>::fmt(self, f)
586+
<[T] as fmt::Debug>::fmt(self.as_slice(), f)
596587
}
597588
}
598589

@@ -660,7 +651,6 @@ mod tests {
660651
let x: HistoryBuf<u8, 4> = HistoryBuf::new_with(1);
661652
assert_eq!(x.len(), 4);
662653
assert_eq!(x.as_slice(), [1; 4]);
663-
assert_eq!(*x, [1; 4]);
664654
assert!(x.is_full());
665655

666656
let x: HistoryBuf<u8, 4> = HistoryBuf::new();
@@ -910,8 +900,8 @@ mod tests {
910900
x,
911901
y,
912902
"{:?} {:?}",
913-
x.iter().collect::<Vec<_>>(),
914-
y.iter().collect::<Vec<_>>()
903+
x.as_slice().iter().collect::<Vec<_>>(),
904+
y.as_slice().iter().collect::<Vec<_>>()
915905
);
916906
}
917907
}

0 commit comments

Comments
 (0)