use std::{
ops::Deref,
sync::Arc,
time::Duration,
};
#[cfg(feature = "async")]
use super::{
AsyncConditionWaiter,
AsyncMonitorFuture,
AsyncNotificationWaiter,
AsyncTimeoutConditionWaiter,
AsyncTimeoutNotificationWaiter,
};
use super::{
ConditionWaiter,
MockMonitor,
NotificationWaiter,
Notifier,
TimeoutConditionWaiter,
TimeoutNotificationWaiter,
WaitTimeoutResult,
WaitTimeoutStatus,
};
pub struct ArcMockMonitor<T> {
inner: Arc<MockMonitor<T>>,
}
impl<T> ArcMockMonitor<T> {
pub fn new(state: T) -> Self {
Self {
inner: Arc::new(MockMonitor::new(state)),
}
}
pub fn elapsed(&self) -> Duration {
self.inner.elapsed()
}
pub fn set_elapsed(&self, elapsed: Duration) {
self.inner.set_elapsed(elapsed);
}
pub fn advance(&self, duration: Duration) {
self.inner.advance(duration);
}
pub fn reset_elapsed(&self) {
self.inner.reset_elapsed();
}
pub fn read<R, F>(&self, f: F) -> R
where
F: FnOnce(&T) -> R,
{
self.inner.read(f)
}
pub fn write<R, F>(&self, f: F) -> R
where
F: FnOnce(&mut T) -> R,
{
self.inner.write(f)
}
pub fn write_notify_one<R, F>(&self, f: F) -> R
where
F: FnOnce(&mut T) -> R,
{
self.inner.write_notify_one(f)
}
pub fn write_notify_all<R, F>(&self, f: F) -> R
where
F: FnOnce(&mut T) -> R,
{
self.inner.write_notify_all(f)
}
pub fn notify_one(&self) {
self.inner.notify_one();
}
pub fn notify_all(&self) {
self.inner.notify_all();
}
}
impl<T> Notifier for ArcMockMonitor<T> {
fn notify_one(&self) {
Self::notify_one(self);
}
fn notify_all(&self) {
Self::notify_all(self);
}
}
impl<T> NotificationWaiter for ArcMockMonitor<T> {
fn wait(&self) {
self.inner.wait();
}
}
impl<T> TimeoutNotificationWaiter for ArcMockMonitor<T> {
fn wait_for(&self, timeout: Duration) -> WaitTimeoutStatus {
self.inner.wait_for(timeout)
}
}
impl<T> ConditionWaiter for ArcMockMonitor<T> {
type State = T;
fn wait_until<R, P, F>(&self, predicate: P, action: F) -> R
where
P: FnMut(&Self::State) -> bool,
F: FnOnce(&mut Self::State) -> R,
{
self.inner.wait_until(predicate, action)
}
fn wait_while<R, P, F>(&self, predicate: P, action: F) -> R
where
P: FnMut(&Self::State) -> bool,
F: FnOnce(&mut Self::State) -> R,
{
self.inner.wait_while(predicate, action)
}
}
impl<T> TimeoutConditionWaiter for ArcMockMonitor<T> {
fn wait_until_for<R, P, F>(
&self,
timeout: Duration,
predicate: P,
action: F,
) -> WaitTimeoutResult<R>
where
P: FnMut(&Self::State) -> bool,
F: FnOnce(&mut Self::State) -> R,
{
self.inner.wait_until_for(timeout, predicate, action)
}
fn wait_while_for<R, P, F>(
&self,
timeout: Duration,
predicate: P,
action: F,
) -> WaitTimeoutResult<R>
where
P: FnMut(&Self::State) -> bool,
F: FnOnce(&mut Self::State) -> R,
{
self.inner.wait_while_for(timeout, predicate, action)
}
}
#[cfg(feature = "async")]
impl<T: Send> AsyncNotificationWaiter for ArcMockMonitor<T> {
fn async_wait<'a>(&'a self) -> AsyncMonitorFuture<'a, ()> {
self.inner.async_wait()
}
}
#[cfg(feature = "async")]
impl<T: Send> AsyncTimeoutNotificationWaiter for ArcMockMonitor<T> {
fn async_wait_for<'a>(
&'a self,
timeout: Duration,
) -> AsyncMonitorFuture<'a, WaitTimeoutStatus> {
self.inner.async_wait_for(timeout)
}
}
#[cfg(feature = "async")]
impl<T: Send> AsyncConditionWaiter for ArcMockMonitor<T> {
type State = T;
fn async_wait_until<'a, R, P, F>(&'a self, predicate: P, action: F) -> AsyncMonitorFuture<'a, R>
where
R: Send + 'a,
P: FnMut(&Self::State) -> bool + Send + 'a,
F: FnOnce(&mut Self::State) -> R + Send + 'a,
{
self.inner.async_wait_until(predicate, action)
}
fn async_wait_while<'a, R, P, F>(&'a self, predicate: P, action: F) -> AsyncMonitorFuture<'a, R>
where
R: Send + 'a,
P: FnMut(&Self::State) -> bool + Send + 'a,
F: FnOnce(&mut Self::State) -> R + Send + 'a,
{
self.inner.async_wait_while(predicate, action)
}
}
#[cfg(feature = "async")]
impl<T: Send> AsyncTimeoutConditionWaiter for ArcMockMonitor<T> {
fn async_wait_until_for<'a, R, P, F>(
&'a self,
timeout: Duration,
predicate: P,
action: F,
) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
where
R: Send + 'a,
P: FnMut(&Self::State) -> bool + Send + 'a,
F: FnOnce(&mut Self::State) -> R + Send + 'a,
{
self.inner.async_wait_until_for(timeout, predicate, action)
}
fn async_wait_while_for<'a, R, P, F>(
&'a self,
timeout: Duration,
predicate: P,
action: F,
) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
where
R: Send + 'a,
P: FnMut(&Self::State) -> bool + Send + 'a,
F: FnOnce(&mut Self::State) -> R + Send + 'a,
{
self.inner.async_wait_while_for(timeout, predicate, action)
}
}
impl<T> AsRef<MockMonitor<T>> for ArcMockMonitor<T> {
fn as_ref(&self) -> &MockMonitor<T> {
self.inner.as_ref()
}
}
impl<T> Deref for ArcMockMonitor<T> {
type Target = MockMonitor<T>;
fn deref(&self) -> &Self::Target {
self.inner.as_ref()
}
}
impl<T> Clone for ArcMockMonitor<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<T> From<T> for ArcMockMonitor<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}
impl<T: Default> Default for ArcMockMonitor<T> {
fn default() -> Self {
Self::new(T::default())
}
}