ringbuf/traits/utils.rs
1use super::Observer;
2use core::num::NonZeroUsize;
3
4/// Trait that should be implemented by ring buffer wrappers.
5///
6/// Used for automatically delegating methods.
7pub trait Based {
8 /// Type the wrapper based on.
9 type Base: ?Sized;
10 /// Reference to base.
11 fn base(&self) -> &Self::Base;
12 /// Mutable reference to base.
13 fn base_mut(&mut self) -> &mut Self::Base;
14}
15
16/// Modulus for pointers to item in ring buffer storage.
17///
18/// Equals to `2 * capacity`.
19#[inline]
20pub fn modulus<O: Observer + ?Sized>(this: &O) -> NonZeroUsize {
21 unsafe { NonZeroUsize::new_unchecked(2 * this.capacity().get()) }
22}