remove mut bound for seq counter impl
This commit is contained in:
@ -1,17 +1,35 @@
|
||||
use core::cell::Cell;
|
||||
use core::sync::atomic::{AtomicU16, Ordering};
|
||||
#[cfg(feature = "alloc")]
|
||||
use dyn_clone::DynClone;
|
||||
#[cfg(feature = "std")]
|
||||
pub use stdmod::*;
|
||||
|
||||
/// Core trait for objects which can provide a sequence count.
|
||||
///
|
||||
/// The core functions are not mutable on purpose to allow easier usage with
|
||||
/// static structs when using the interior mutability pattern. This can be achieved by using
|
||||
/// [Cell], [RefCell] or atomic types.
|
||||
pub trait SequenceCountProviderCore<Raw> {
|
||||
fn get(&self) -> Raw;
|
||||
fn increment(&mut self);
|
||||
fn get_and_increment(&mut self) -> Raw {
|
||||
|
||||
fn increment(&self);
|
||||
|
||||
// TODO: Maybe remove this?
|
||||
fn increment_mut(&mut self) {
|
||||
self.increment();
|
||||
}
|
||||
|
||||
fn get_and_increment(&self) -> Raw {
|
||||
let val = self.get();
|
||||
self.increment();
|
||||
val
|
||||
}
|
||||
|
||||
// TODO: Maybe remove this?
|
||||
fn get_and_increment_mut(&mut self) -> Raw {
|
||||
self.get_and_increment()
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait which allows cloning a sequence count provider after it was turned into
|
||||
@ -25,25 +43,30 @@ impl<T, Raw> SequenceCountProvider<Raw> for T where T: SequenceCountProviderCore
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct SeqCountProviderSimple {
|
||||
seq_count: u16,
|
||||
seq_count: Cell<u16>,
|
||||
}
|
||||
|
||||
impl SequenceCountProviderCore<u16> for SeqCountProviderSimple {
|
||||
fn get(&self) -> u16 {
|
||||
self.seq_count
|
||||
self.seq_count.get()
|
||||
}
|
||||
|
||||
fn increment(&mut self) {
|
||||
if self.seq_count == u16::MAX {
|
||||
self.seq_count = 0;
|
||||
return;
|
||||
fn increment(&self) {
|
||||
self.get_and_increment();
|
||||
}
|
||||
|
||||
fn get_and_increment(&self) -> u16 {
|
||||
let curr_count = self.seq_count.get();
|
||||
|
||||
if curr_count == u16::MAX {
|
||||
self.seq_count.set(0);
|
||||
} else {
|
||||
self.seq_count.set(curr_count + 1);
|
||||
}
|
||||
self.seq_count += 1;
|
||||
curr_count
|
||||
}
|
||||
}
|
||||
|
||||
use core::sync::atomic::{AtomicU16, Ordering};
|
||||
|
||||
pub struct SeqCountProviderAtomicRef {
|
||||
atomic: AtomicU16,
|
||||
ordering: Ordering,
|
||||
@ -51,7 +74,10 @@ pub struct SeqCountProviderAtomicRef {
|
||||
|
||||
impl SeqCountProviderAtomicRef {
|
||||
pub const fn new(ordering: Ordering) -> Self {
|
||||
Self { atomic: AtomicU16::new(0), ordering }
|
||||
Self {
|
||||
atomic: AtomicU16::new(0),
|
||||
ordering,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,11 +86,11 @@ impl SequenceCountProviderCore<u16> for SeqCountProviderAtomicRef {
|
||||
self.atomic.load(self.ordering)
|
||||
}
|
||||
|
||||
fn increment(&mut self) {
|
||||
fn increment(&self) {
|
||||
self.atomic.fetch_add(1, self.ordering);
|
||||
}
|
||||
|
||||
fn get_and_increment(&mut self) -> u16 {
|
||||
fn get_and_increment(&self) -> u16 {
|
||||
self.atomic.fetch_add(1, self.ordering)
|
||||
}
|
||||
}
|
||||
@ -75,20 +101,20 @@ pub mod stdmod {
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct SeqCountProviderSync {
|
||||
pub struct SeqCountProviderSyncClonable {
|
||||
seq_count: Arc<AtomicU16>,
|
||||
}
|
||||
|
||||
impl SequenceCountProviderCore<u16> for SeqCountProviderSync {
|
||||
impl SequenceCountProviderCore<u16> for SeqCountProviderSyncClonable {
|
||||
fn get(&self) -> u16 {
|
||||
self.seq_count.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
fn increment(&mut self) {
|
||||
fn increment(&self) {
|
||||
self.seq_count.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
fn get_and_increment(&mut self) -> u16 {
|
||||
fn get_and_increment(&self) -> u16 {
|
||||
self.seq_count.fetch_add(1, Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user