Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compatibility with recent Rust versions #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
/.idea/
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@

Traits over atomic primitive integer types.

## Example

```rust
fn incr<T: AtomicInt>(value: &T) -> Result<<T as AtomicInt>::Prim, <T as AtomicInt>::Prim> {
value.fetch_update(
Ordering::SeqCst,
Ordering::SeqCst,
|i| Some(if i == T::MAX { T::MIN } else { i + T::ONE })
)
}

let value = AtomicU8::new(255);
assert_eq!(incr(&value), Ok(255));
assert_eq!(incr(&value), Ok(0));
```

## Notes

* Enable feature `nightly` to get `min`, `max`, `fetch_update` and
`as_mut_ptr` when you have a nightly compiler available.
* Enable feature `nightly` to get `as_mut_ptr` when you have a nightly compiler available.
* Rust 1.45.0 or newer is required.

## Copyright and License

Copyright (c) 2020 James Laver, atomic_prim_traits contributors.
Copyright (c) 2020-2023 James Laver, atomic_prim_traits contributors.

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
93 changes: 54 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(feature = "nightly", feature(atomic_min_max, atomic_mut_ptr, no_more_cas))]
#![cfg_attr(feature = "nightly", feature(atomic_mut_ptr))]
use std::sync::atomic::{self, Ordering};
use std::hash::Hash;
use std::fmt::{Debug, Display};
Expand All @@ -14,17 +14,23 @@ use std::ops::{
pub trait AtomicInt : Default + Send + Sync + RefUnwindSafe + UnwindSafe {
type Prim
: Copy + Debug + Display + Eq + Hash + Ord + Sized
+ Add + AddAssign
+ BitAnd + BitAndAssign
+ BitOr + BitOrAssign
+ BitXor + BitXorAssign
+ Div + DivAssign
+ Mul + MulAssign
+ Not
+ Rem + RemAssign
+ Shl + ShlAssign
+ Shr + ShrAssign
+ Sub + SubAssign;
+ Add<Output = <Self as AtomicInt>::Prim> + AddAssign
+ BitAnd<Output = <Self as AtomicInt>::Prim> + BitAndAssign
+ BitOr<Output = <Self as AtomicInt>::Prim> + BitOrAssign
+ BitXor<Output = <Self as AtomicInt>::Prim> + BitXorAssign
+ Div<Output = <Self as AtomicInt>::Prim> + DivAssign
+ Mul<Output = <Self as AtomicInt>::Prim> + MulAssign
+ Not<Output = <Self as AtomicInt>::Prim>
+ Rem<Output = <Self as AtomicInt>::Prim> + RemAssign
+ Shl<Output = <Self as AtomicInt>::Prim> + ShlAssign
+ Shr<Output = <Self as AtomicInt>::Prim> + ShrAssign
+ Sub<Output = <Self as AtomicInt>::Prim> + SubAssign;

const ZERO: <Self as AtomicInt>::Prim;
const ONE: <Self as AtomicInt>::Prim;

const MIN: <Self as AtomicInt>::Prim;
const MAX: <Self as AtomicInt>::Prim;

fn new(val: <Self as AtomicInt>::Prim) -> Self;

Expand Down Expand Up @@ -64,18 +70,15 @@ pub trait AtomicInt : Default + Send + Sync + RefUnwindSafe + UnwindSafe {
ordering: Ordering
) -> <Self as AtomicInt>::Prim;

#[cfg(feature="nightly")]
fn fetch_min(&self, val: <Self as AtomicInt>::Prim, order: Ordering) -> <Self as AtomicInt>::Prim;

#[cfg(feature="nightly")]
fn fetch_max(&self, val: <Self as AtomicInt>::Prim, order: Ordering) -> <Self as AtomicInt>::Prim;

#[cfg(feature="nightly")]
fn fetch_update<F>(
&self,
f: F,
set_order: Ordering,
fetch_order: Ordering,
set_order: Ordering
f: F
) -> Result<<Self as AtomicInt>::Prim, <Self as AtomicInt>::Prim>
where F: FnMut(<Self as AtomicInt>::Prim) -> Option<<Self as AtomicInt>::Prim>;

Expand All @@ -89,13 +92,6 @@ pub trait AtomicInt : Default + Send + Sync + RefUnwindSafe + UnwindSafe {

fn swap(&self, val: <Self as AtomicInt>::Prim, order: Ordering) -> <Self as AtomicInt>::Prim;

fn compare_and_swap(
&self,
current: <Self as AtomicInt>::Prim,
new: <Self as AtomicInt>::Prim,
ordering: Ordering
) -> <Self as AtomicInt>::Prim;

fn compare_exchange(
&self,
current: <Self as AtomicInt>::Prim,
Expand All @@ -119,6 +115,12 @@ pub trait AtomicInt : Default + Send + Sync + RefUnwindSafe + UnwindSafe {
macro_rules! impl_atomic_int {
($atomic:ty = $prim:ty) => {
impl AtomicInt for $atomic {
const ZERO: $prim = 0;
const ONE: $prim = 1;

const MIN: $prim = <$prim>::MIN;
const MAX: $prim = <$prim>::MAX;

type Prim = $prim;

fn new(val: $prim) -> Self {
Expand Down Expand Up @@ -173,7 +175,6 @@ macro_rules! impl_atomic_int {
self.fetch_xor(new, ordering)
}

#[cfg(feature = "nightly")]
fn fetch_min(
&self,
val: $prim,
Expand All @@ -182,7 +183,6 @@ macro_rules! impl_atomic_int {
self.fetch_min(val, ordering)
}

#[cfg(feature = "nightly")]
fn fetch_max(
&self,
val: $prim,
Expand All @@ -191,17 +191,16 @@ macro_rules! impl_atomic_int {
self.fetch_max(val, ordering)
}

#[cfg(feature = "nightly")]
fn fetch_update<F>(
&self,
f: F,
fetch_order: Ordering,
set_order: Ordering,
fetch_order: Ordering,
f: F,
) -> Result<$prim, $prim>
where
F: FnMut($prim) -> Option<$prim>,
{
self.fetch_update(f, fetch_order, set_order)
self.fetch_update(set_order, fetch_order, f)
}

fn get_mut(&mut self) -> &mut $prim {
Expand All @@ -224,15 +223,6 @@ macro_rules! impl_atomic_int {
self.swap(val, order)
}

fn compare_and_swap(
&self,
current: $prim,
new: $prim,
ordering: Ordering
) -> $prim {
self.compare_and_swap(current, new, ordering)
}

fn compare_exchange(
&self,
current: $prim,
Expand Down Expand Up @@ -272,3 +262,28 @@ impl_atomic_int!(atomic::AtomicI16 = i16);
impl_atomic_int!(atomic::AtomicI32 = i32);
impl_atomic_int!(atomic::AtomicI64 = i64);
impl_atomic_int!(atomic::AtomicIsize = isize);

#[cfg(test)]
mod test {
use std::sync::atomic::AtomicU8;
use super::*;

#[test]
fn test_fetch_update() {
fn incr<T: AtomicInt>(value: &T) -> Result<<T as AtomicInt>::Prim, <T as AtomicInt>::Prim> {
value.fetch_update(
Ordering::SeqCst,
Ordering::SeqCst,
|i| Some(if i == T::MAX { T::MIN } else { i + T::ONE })
)
}

let value = AtomicU8::new(0);
assert_eq!(incr(&value), Ok(0));
assert_eq!(incr(&value), Ok(1));

let value = AtomicU8::new(255);
assert_eq!(incr(&value), Ok(255));
assert_eq!(incr(&value), Ok(0));
}
}