From ccf28965113c4d385cb466ffffb889f1b90b53c1 Mon Sep 17 00:00:00 2001 From: Christian Meusel Date: Fri, 19 Sep 2025 11:18:24 +0200 Subject: [PATCH] Remove implementing Deref 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. --- CHANGELOG.md | 1 + src/history_buf.rs | 18 ++++-------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddb1d69bad..61648238b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Implement `defmt::Format` for `CapacityError`. - Implement `TryFrom` for `Deque` from array. - Switch from `serde` to `serde_core` for enabling faster compilations. +- Removed `impl Deref` for `HistoryBuf` to make accessing the raw backing array explicit (use `as_slice`). ## [v0.9.1] - 2025-08-19 diff --git a/src/history_buf.rs b/src/history_buf.rs index b06e4648b9..1249b5e41c 100644 --- a/src/history_buf.rs +++ b/src/history_buf.rs @@ -34,7 +34,6 @@ use core::fmt; use core::marker::PhantomData; use core::mem::MaybeUninit; -use core::ops::Deref; use core::ptr; use core::slice; @@ -572,18 +571,10 @@ impl + ?Sized> Drop for HistoryBufInner { } } -impl + ?Sized> Deref for HistoryBufInner { - type Target = [T]; - - fn deref(&self) -> &[T] { - self.as_slice() - } -} - impl + ?Sized> AsRef<[T]> for HistoryBufInner { #[inline] fn as_ref(&self) -> &[T] { - self + self.as_slice() } } @@ -592,7 +583,7 @@ where T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - <[T] as fmt::Debug>::fmt(self, f) + <[T] as fmt::Debug>::fmt(self.as_slice(), f) } } @@ -660,7 +651,6 @@ mod tests { let x: HistoryBuf = HistoryBuf::new_with(1); assert_eq!(x.len(), 4); assert_eq!(x.as_slice(), [1; 4]); - assert_eq!(*x, [1; 4]); assert!(x.is_full()); let x: HistoryBuf = HistoryBuf::new(); @@ -910,8 +900,8 @@ mod tests { x, y, "{:?} {:?}", - x.iter().collect::>(), - y.iter().collect::>() + x.as_slice().iter().collect::>(), + y.as_slice().iter().collect::>() ); } }