Skip to main content

circular_buffer/
hash.rs

1// Copyright © 2023-2026 Andrea Corbellini and contributors
2// SPDX-License-Identifier: BSD-3-Clause
3
4//! Implementations of [`Hash`].
5
6use crate::CircularBuffer;
7use crate::FixedCircularBuffer;
8use core::hash::Hash;
9use core::hash::Hasher;
10use core::ops::Deref;
11
12#[cfg(feature = "alloc")]
13use crate::HeapCircularBuffer;
14
15impl<T> Hash for CircularBuffer<T>
16where
17    T: Hash,
18{
19    fn hash<H: Hasher>(&self, state: &mut H) {
20        // TODO: Use `Hasher::write_length_prefix()` once it's stabilized
21        self.inner.size.hash(state);
22        self.iter().for_each(|item| item.hash(state));
23    }
24}
25
26impl<T, const N: usize> Hash for FixedCircularBuffer<T, N>
27where
28    T: Hash,
29{
30    #[inline]
31    fn hash<H: Hasher>(&self, state: &mut H) {
32        self.deref().hash(state);
33    }
34}
35
36#[cfg(feature = "alloc")]
37impl<T> Hash for HeapCircularBuffer<T>
38where
39    T: Hash,
40{
41    #[inline]
42    fn hash<H: Hasher>(&self, state: &mut H) {
43        self.deref().hash(state);
44    }
45}