use std::future::Future;
use super::Transact;
use crate::DataError;
pub struct NoopTransact;
impl Transact for NoopTransact {
async fn transact<F, Fut, T, E>(&self, f: F) -> Result<T, E>
where
F: FnOnce() -> Fut,
Fut: Future<Output = Result<T, E>>,
E: From<DataError>,
{
f().await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn noop_passes_through_ok() {
let tx = NoopTransact;
let result: Result<u32, DataError> = tx.transact(|| async { Ok(99) }).await;
assert_eq!(result.unwrap(), 99);
}
#[tokio::test]
async fn noop_propagates_error() {
let tx = NoopTransact;
let result: Result<u32, DataError> =
tx.transact(|| async { Err(DataError::InvalidPage("test".into())) }).await;
assert!(matches!(result, Err(DataError::InvalidPage(_))));
}
#[tokio::test]
async fn noop_supports_async_work_inside() {
let tx = NoopTransact;
let result: Result<Vec<u32>, DataError> = tx
.transact(|| async {
let a = async { 1u32 }.await;
let b = async { 2u32 }.await;
Ok(vec![a, b])
})
.await;
assert_eq!(result.unwrap(), vec![1, 2]);
}
}