From caac55cd05e7a32e42b48a29ad30e3149afadbd5 Mon Sep 17 00:00:00 2001 From: Jurek Date: Thu, 9 May 2024 12:42:10 +0200 Subject: [PATCH] using std rwlock and spin on no_std environments --- src/lib.rs | 4 +++- src/rwlock.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/rwlock.rs diff --git a/src/lib.rs b/src/lib.rs index 56103b8..192c097 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,6 +102,8 @@ html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png" )] +mod rwlock; + #[cfg(doctest)] mod doctests { doc_comment::doctest!("../README.md"); @@ -122,10 +124,10 @@ use core::future::Future; use core::marker::PhantomPinned; use core::pin::Pin; use core::task::{Context, Poll}; -use spin::RwLock; #[cfg(feature = "std")] use std::error; +use crate::rwlock::RwLock; use event_listener::{Event, EventListener}; use event_listener_strategy::{easy_wrapper, EventListenerFuture}; use futures_core::{ready, stream::Stream}; diff --git a/src/rwlock.rs b/src/rwlock.rs new file mode 100644 index 0000000..e6abdd5 --- /dev/null +++ b/src/rwlock.rs @@ -0,0 +1,47 @@ +#[cfg(feature = "std")] +#[derive(Debug)] +pub(crate) struct RwLock { + rwlock: std::sync::RwLock, +} +#[cfg(not(feature = "std"))] +#[derive(Debug)] +pub(crate) struct RwLock { + rwlock: spin::RwLock, +} + +impl RwLock { + #[cfg(feature = "std")] + #[inline] + pub(crate) const fn new(t: T) -> RwLock { + return RwLock { + rwlock: std::sync::RwLock::new(t), + }; + } + #[cfg(not(feature = "std"))] + #[inline] + pub(crate) const fn new(t: T) -> RwLock { + return RwLock { + rwlock: spin::RwLock::new(t), + }; + } + #[inline] + #[cfg(feature = "std")] + pub(crate) fn read(&self) -> std::sync::RwLockReadGuard<'_, T> { + return self.rwlock.read().unwrap(); + } + #[inline] + #[cfg(not(feature = "std"))] + pub(crate) fn read(&self) -> spin::RwLockReadGuard<'_, T> { + return self.rwlock.read(); + } + #[inline] + #[cfg(feature = "std")] + pub(crate) fn write(&self) -> std::sync::RwLockWriteGuard<'_, T> { + return self.rwlock.write().unwrap(); + } + #[inline] + #[cfg(not(feature = "std"))] + pub(crate) fn write(&self) -> spin::RwLockWriteGuard<'_, T> { + return self.rwlock.write(); + } +}