Skip to content

Commit

Permalink
using std rwlock and spin on no_std environments
Browse files Browse the repository at this point in the history
  • Loading branch information
dscso committed May 9, 2024
1 parent 2842aad commit caac55c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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};
Expand Down
47 changes: 47 additions & 0 deletions src/rwlock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#[cfg(feature = "std")]
#[derive(Debug)]
pub(crate) struct RwLock<T> {
rwlock: std::sync::RwLock<T>,
}
#[cfg(not(feature = "std"))]
#[derive(Debug)]
pub(crate) struct RwLock<T> {
rwlock: spin::RwLock<T>,
}

impl<T> RwLock<T> {
#[cfg(feature = "std")]
#[inline]
pub(crate) const fn new(t: T) -> RwLock<T> {
return RwLock {
rwlock: std::sync::RwLock::new(t),
};
}
#[cfg(not(feature = "std"))]
#[inline]
pub(crate) const fn new(t: T) -> RwLock<T> {
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();
}
}

0 comments on commit caac55c

Please sign in to comment.