Path: blob/main/crates/bevy_platform/src/sync/rwlock.rs
6849 views
//! Provides `RwLock`, `RwLockReadGuard`, `RwLockWriteGuard`12pub use implementation::{RwLock, RwLockReadGuard, RwLockWriteGuard};34#[cfg(feature = "std")]5use std::sync as implementation;67#[cfg(not(feature = "std"))]8mod implementation {9use crate::sync::{LockResult, TryLockError, TryLockResult};10use core::fmt;1112pub use spin::rwlock::{RwLockReadGuard, RwLockWriteGuard};1314/// Fallback implementation of `RwLock` from the standard library.15pub struct RwLock<T: ?Sized> {16inner: spin::RwLock<T>,17}1819impl<T> RwLock<T> {20/// Creates a new instance of an `RwLock<T>` which is unlocked.21///22/// See the standard library for further details.23pub const fn new(t: T) -> RwLock<T> {24Self {25inner: spin::RwLock::new(t),26}27}28}2930impl<T: ?Sized> RwLock<T> {31/// Locks this `RwLock` with shared read access, blocking the current thread32/// until it can be acquired.33///34/// See the standard library for further details.35pub fn read(&self) -> LockResult<RwLockReadGuard<'_, T>> {36Ok(self.inner.read())37}3839/// Attempts to acquire this `RwLock` with shared read access.40///41/// See the standard library for further details.42pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>> {43self.inner.try_read().ok_or(TryLockError::WouldBlock)44}4546/// Locks this `RwLock` with exclusive write access, blocking the current47/// thread until it can be acquired.48///49/// See the standard library for further details.50pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>> {51Ok(self.inner.write())52}5354/// Attempts to lock this `RwLock` with exclusive write access.55///56/// See the standard library for further details.57pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>> {58self.inner.try_write().ok_or(TryLockError::WouldBlock)59}6061/// Determines whether the lock is poisoned.62///63/// See the standard library for further details.64pub fn is_poisoned(&self) -> bool {65false66}6768/// Clear the poisoned state from a lock.69///70/// See the standard library for further details.71pub fn clear_poison(&self) {72// no-op73}7475/// Consumes this `RwLock`, returning the underlying data.76///77/// See the standard library for further details.78pub fn into_inner(self) -> LockResult<T>79where80T: Sized,81{82Ok(self.inner.into_inner())83}8485/// Returns a mutable reference to the underlying data.86///87/// See the standard library for further details.88pub fn get_mut(&mut self) -> LockResult<&mut T> {89Ok(self.inner.get_mut())90}91}9293impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {94fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {95let mut d = f.debug_struct("RwLock");96match self.try_read() {97Ok(guard) => {98d.field("data", &&*guard);99}100Err(TryLockError::Poisoned(err)) => {101d.field("data", &&**err.get_ref());102}103Err(TryLockError::WouldBlock) => {104d.field("data", &format_args!("<locked>"));105}106}107d.field("poisoned", &false);108d.finish_non_exhaustive()109}110}111112impl<T: Default> Default for RwLock<T> {113fn default() -> RwLock<T> {114RwLock::new(Default::default())115}116}117118impl<T> From<T> for RwLock<T> {119fn from(t: T) -> Self {120RwLock::new(t)121}122}123}124125126