pub mod std {
use std::sync::Arc;
use std::sync::{RwLock, RwLockReadGuard};
pub struct ReadOnlyLock<T> {
inner: Arc<RwLock<T>>,
}
impl<T> ReadOnlyLock<T> {
pub fn new(underlying: Arc<RwLock<T>>) -> Self {
Self {
inner: underlying.clone(),
}
}
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.inner.read().unwrap()
}
}
impl<T> Clone for ReadOnlyLock<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
}
#[cfg(feature = "tokio")]
pub mod tokio {
use std::sync::Arc;
use tokio::sync::{RwLock, RwLockReadGuard};
pub struct ReadOnlyLock<T> {
inner: Arc<RwLock<T>>,
}
impl<T> ReadOnlyLock<T> {
pub fn new(underlying: Arc<RwLock<T>>) -> Self {
Self {
inner: underlying.clone(),
}
}
pub async fn read(&self) -> RwLockReadGuard<'_, T> {
self.inner.read().await
}
}
impl<T> Clone for ReadOnlyLock<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
}