#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
use std::{future::Future, time::Duration};
#[cfg(any(test, feature = "test"))]
mod real;
#[derive(Debug)]
#[non_exhaustive]
pub struct Config {
pub seed: [u8; 32],
pub check_named_locks_work_for: Option<Duration>,
pub check_addressed_locks_work_for: Option<Duration>,
}
impl Config {
pub fn with_random_seed() -> Config {
use rand::Rng;
Config {
seed: rand::thread_rng().gen(),
check_addressed_locks_work_for: None,
check_named_locks_work_for: None,
}
}
pub fn from_seed(seed: [u8; 32]) -> Config {
Config {
seed,
check_addressed_locks_work_for: None,
check_named_locks_work_for: None,
}
}
}
#[cfg(all(test, not(feature = "test")))]
const _: () = panic!("Trying to test `reord` without its `test` feature");
#[inline]
#[allow(unused_variables)]
pub async fn init_test(config: Config) {
#[cfg(any(test, feature = "test"))]
real::init_test(config).await
}
#[inline]
pub async fn new_task<T>(f: impl Future<Output = T>) -> T {
#[cfg(any(test, feature = "test"))]
let res = real::new_task(f).await;
#[cfg(not(any(test, feature = "test")))]
let res = f.await;
res
}
#[inline]
#[allow(unused_variables)]
pub async fn start(tasks: usize) -> tokio::task::JoinHandle<()> {
#[cfg(not(any(test, feature = "test")))]
panic!("Trying to start a `reord` test, but the `test` feature is not set");
#[cfg(any(test, feature = "test"))]
real::start(tasks).await
}
#[inline]
pub async fn point() {
#[cfg(any(test, feature = "test"))]
real::point().await
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum LockInfo {
Named(String),
Addressed(usize),
}
#[derive(Debug)]
pub struct Lock {
#[cfg(any(test, feature = "test"))]
_data: real::Lock,
#[cfg(not(any(test, feature = "test")))]
_unused: (),
}
impl Lock {
#[inline]
#[allow(unused_variables)]
pub async fn take_named(name: String) -> Lock {
#[cfg(any(test, feature = "test"))]
let res = Lock {
_data: real::Lock::take_named(name).await,
};
#[cfg(not(any(test, feature = "test")))]
let res = Lock { _unused: () };
res
}
#[inline]
#[allow(unused_variables)]
pub async fn take_addressed(address: usize) -> Lock {
#[cfg(any(test, feature = "test"))]
let res = Lock {
_data: real::Lock::take_addressed(address).await,
};
#[cfg(not(any(test, feature = "test")))]
let res = Lock { _unused: () };
res
}
#[inline]
#[allow(unused_variables)]
pub async fn take_atomic(l: Vec<LockInfo>) -> Lock {
#[cfg(any(test, feature = "test"))]
let res = Lock {
_data: real::Lock::take_atomic(l).await,
};
#[cfg(not(any(test, feature = "test")))]
let res = Lock { _unused: () };
res
}
}
impl Drop for Lock {
#[inline]
fn drop(&mut self) {}
}
#[cfg(test)]
mod tests;