An open API service indexing awesome lists of open source software.

https://github.com/flier/rust-atomic-traits

The traits for generic atomic operations in Rust.
https://github.com/flier/rust-atomic-traits

Last synced: over 1 year ago
JSON representation

The traits for generic atomic operations in Rust.

Awesome Lists containing this project

README

          

# atomic-traits [![crate](https://img.shields.io/crates/v/atomic-traits.svg)](https://crates.io/crates/atomic-traits) [![documentation](https://docs.rs/atomic-traits/badge.svg)](https://docs.rs/atomic-traits/) [![Continuous integration](https://github.com/flier/rust-atomic-traits/actions/workflows/ci.yaml/badge.svg)](https://github.com/flier/rust-atomic-traits/actions/workflows/ci.yaml) [![MSRV](https://img.shields.io/badge/MSRV-1.34.0-green)](https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html)

The traits for generic atomic operations in Rust.

## Compatibility

The crate is tested for stable and nightly compiler.

Current MSRV is `1.34.0`.

## Usage

Add this to your `Cargo.toml`:

```toml
[dependencies]
atomic-traits = "0.4"
```

and this to your crate root:

```rust
extern crate atomic_traits;
```

## Example

```rust
extern crate num_traits;
extern crate atomic_traits;

use std::sync::atomic::{AtomicUsize, Ordering};

use num_traits::One;
use atomic_traits::{Atomic, NumOps, fetch};

#[derive(Debug, Default)]
pub struct RefCnt(T);

impl RefCnt
where
T: Atomic + NumOps + Default,
::Type: One
{
pub fn inc(&self) -> ::Type {
self.0.fetch_add(::Type::one(), Ordering::Acquire)
}

pub fn dec(&self) -> ::Type {
self.0.fetch_sub(::Type::one(), Ordering::Release)
}

pub fn val(&self) -> ::Type {
self.0.load(Ordering::SeqCst)
}
}

let refcnt = RefCnt::::default();

assert_eq!(refcnt.inc(), 0);
assert_eq!(refcnt.dec(), 1);
assert_eq!(refcnt.val(), 0);
```