| Crates.io | int_like |
| lib.rs | int_like |
| version | 0.1.1 |
| created_at | 2025-08-14 07:27:06.544301+00 |
| updated_at | 2025-08-14 07:32:43.844859+00 |
| description | A Rust macro for defining integer-backed opaque types safely |
| homepage | https://github.com/fslongjin/int_like |
| repository | https://github.com/fslongjin/int_like |
| max_upload_size | |
| id | 1794540 |
| size | 14,197 |
A Rust macro for defining integer-backed opaque types safely.
Originally inspired by code from Redox OS, this crate provides a convenient way to create new types that are backed by primitive integers (typically usize), providing type safety without compromising performance.
Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)Add this to your Cargo.toml:
[dependencies]
int_like = "0.1.0"
Define a simple type backed by usize:
use int_like::int_like;
/// Define an opaque type `Pid` backed by a `usize`.
int_like!(Pid, usize);
const ZERO: Pid = Pid::from(0);
let pid = Pid::from(42);
assert_eq!(pid.into(), 42);
Define both a regular type and its atomic counterpart:
use int_like::int_like;
use std::sync::atomic::Ordering;
/// Define opaque types `Pid` and `AtomicPid`, backed respectively by a `usize`
/// and an `AtomicUsize`.
int_like!(Pid, AtomicPid, usize, AtomicUsize);
const ZERO: Pid = Pid::from(0);
let ATOMIC_PID: AtomicPid = AtomicPid::default();
// Atomic operations
ATOMIC_PID.store(Pid::from(100), Ordering::SeqCst);
let value = ATOMIC_PID.load(Ordering::SeqCst);
assert_eq!(value, Pid::from(100));
For simple types:
from(x: T) -> Self - Create from the backing typenew(x: T) -> Self - Alternative constructorinto() -> T - Convert back to the backing typedata() -> T - Get the backing valueFor atomic types:
new(x: T) -> Self - Create from the non-atomic typedefault() -> Self - Create with zero valueload(ordering) -> T - Atomic loadstore(val, ordering) - Atomic storeswap(val, ordering) -> T - Atomic swapcompare_exchange(current, new, success, failure) -> Result<T, T> - CAS operationcompare_exchange_weak(current, new, success, failure) -> Result<T, T> - Weak CASfetch_add(val, ordering) -> T - Atomic addThis project is licensed under the MIT License - see the LICENSE file for details.