Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["codegen", "examples", "performance_measurement", "performance_measur

[package]
name = "worktable"
version = "0.8.7"
version = "0.8.8"
edition = "2024"
authors = ["Handy-caT"]
license = "MIT"
Expand Down
100 changes: 37 additions & 63 deletions src/primary_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::sync::atomic::{AtomicI64, AtomicU32, AtomicU64, Ordering};
use std::sync::atomic::{
AtomicI8, AtomicI16, AtomicI32, AtomicI64, AtomicU8, AtomicU16, AtomicU32, AtomicU64, Ordering,
};

pub trait TablePrimaryKey {
type Generator;
Expand All @@ -16,68 +18,40 @@ pub trait PrimaryKeyGeneratorState {
fn from_state(state: Self::State) -> Self;
}

impl<T> PrimaryKeyGenerator<T> for AtomicU32
where
T: From<u32>,
{
fn next(&self) -> T {
self.fetch_add(1, Ordering::Relaxed).into()
}
}

impl PrimaryKeyGeneratorState for AtomicU32 {
type State = u32;

fn get_state(&self) -> Self::State {
self.load(Ordering::Relaxed)
}

fn from_state(state: Self::State) -> Self {
AtomicU32::from(state)
}
}

impl<T> PrimaryKeyGenerator<T> for AtomicU64
where
T: From<u64>,
{
fn next(&self) -> T {
self.fetch_add(1, Ordering::Relaxed).into()
}
}

impl PrimaryKeyGeneratorState for AtomicU64 {
type State = u64;

fn get_state(&self) -> Self::State {
self.load(Ordering::Relaxed)
}

fn from_state(state: Self::State) -> Self {
AtomicU64::from(state)
}
}

impl<T> PrimaryKeyGenerator<T> for AtomicI64
where
T: From<i64>,
{
fn next(&self) -> T {
self.fetch_add(1, Ordering::Relaxed).into()
}
}

impl PrimaryKeyGeneratorState for AtomicI64 {
type State = i64;

fn get_state(&self) -> Self::State {
self.load(Ordering::Relaxed)
}

fn from_state(state: Self::State) -> Self {
AtomicI64::from(state)
}
}
macro_rules! atomic_primary_key {
($ty:ident, $atomic_ty:ident) => {
impl<T> PrimaryKeyGenerator<T> for $atomic_ty
where
T: From<$ty>,
{
fn next(&self) -> T {
self.fetch_add(1, Ordering::AcqRel).into()
}
}

impl PrimaryKeyGeneratorState for $atomic_ty {
type State = $ty;

fn get_state(&self) -> Self::State {
self.load(Ordering::Acquire)
}

fn from_state(state: Self::State) -> Self {
$atomic_ty::from(state)
}
}
};
}

atomic_primary_key!(u8, AtomicU8);
atomic_primary_key!(u16, AtomicU16);
atomic_primary_key!(u32, AtomicU32);
atomic_primary_key!(u64, AtomicU64);

atomic_primary_key!(i8, AtomicI8);
atomic_primary_key!(i16, AtomicI16);
atomic_primary_key!(i32, AtomicI32);
atomic_primary_key!(i64, AtomicI64);

impl PrimaryKeyGeneratorState for () {
type State = ();
Expand Down
Loading