Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/mutex_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
/// Multithreaded blocking Mutex
pub mod sync {
use std::{
fmt,
ops::{Deref, DerefMut},
sync::{Mutex as Inner, MutexGuard as InnerGuard},
};

/// A multithreaded Mutex based on [`std::sync::Mutex`].
pub struct Mutex<T: ?Sized>(Inner<T>);

impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl<T> Mutex<T> {
/// Creates a new mutex in an unlocked state ready for use.
pub const fn new(val: T) -> Self {
Expand Down Expand Up @@ -43,6 +50,12 @@ pub mod sync {
/// unlocked.
pub struct MutexGuard<'a, T: ?Sized>(InnerGuard<'a, T>);

impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl<'a, T: ?Sized> MutexGuard<'a, T> {
/// Get the inner [`std::sync::MutexGuard`].
pub fn into_inner(self) -> InnerGuard<'a, T> {
Expand Down Expand Up @@ -71,12 +84,19 @@ pub mod sync {
pub mod unsync {
use std::{
cell::{RefCell as Inner, RefMut as InnerGuard},
fmt,
ops::{Deref, DerefMut},
};

/// A singlethreaded Mutex based on [`std::cell::RefCell`].
pub struct Mutex<T: ?Sized>(Inner<T>);

impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl<T> Mutex<T> {
/// Creates a new mutex in an unlocked state ready for use.
pub const fn new(val: T) -> Self {
Expand Down Expand Up @@ -107,6 +127,12 @@ pub mod unsync {
/// unlocked.
pub struct MutexGuard<'a, T: ?Sized>(InnerGuard<'a, T>);

impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl<'a, T: ?Sized> MutexGuard<'a, T> {
/// Get the inner [`std::cell::RefMut`].
pub fn into_inner(self) -> InnerGuard<'a, T> {
Expand Down