From 4124833408395af62ae99fe600ff34ab23eccb3c Mon Sep 17 00:00:00 2001 From: Handy-caT <37216852+Handy-caT@users.noreply.github.com> Date: Mon, 13 Oct 2025 12:24:55 +0300 Subject: [PATCH 1/2] implement for other i and u --- src/primary_key.rs | 100 +++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 63 deletions(-) diff --git a/src/primary_key.rs b/src/primary_key.rs index 54aa44e6..37c7fb3f 100644 --- a/src/primary_key.rs +++ b/src/primary_key.rs @@ -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; @@ -16,68 +18,40 @@ pub trait PrimaryKeyGeneratorState { fn from_state(state: Self::State) -> Self; } -impl PrimaryKeyGenerator for AtomicU32 -where - T: From, -{ - 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 PrimaryKeyGenerator for AtomicU64 -where - T: From, -{ - 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 PrimaryKeyGenerator for AtomicI64 -where - T: From, -{ - 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 PrimaryKeyGenerator 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 = (); From d25494229edc86de4aebfd8daf6ba10aaa5ba570 Mon Sep 17 00:00:00 2001 From: Handy-caT <37216852+Handy-caT@users.noreply.github.com> Date: Mon, 13 Oct 2025 12:25:43 +0300 Subject: [PATCH 2/2] bump --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 95c29eac..eda0c0be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"