2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-05-08 11:02:02 -04:00
|
|
|
|
|
|
|
pub use inner::*;
|
|
|
|
|
|
|
|
#[cfg(feature = "sync_fs")]
|
|
|
|
mod inner {
|
|
|
|
#![allow(clippy::disallowed_types)]
|
2023-07-25 17:43:00 -04:00
|
|
|
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2023-05-08 11:02:02 -04:00
|
|
|
pub use std::sync::Arc as MaybeArc;
|
|
|
|
|
|
|
|
pub use core::marker::Send as MaybeSend;
|
|
|
|
pub use core::marker::Sync as MaybeSync;
|
2023-07-25 17:43:00 -04:00
|
|
|
|
|
|
|
pub struct MaybeArcMutexGuard<'lock, T>(std::sync::MutexGuard<'lock, T>);
|
|
|
|
|
|
|
|
impl<'lock, T> Deref for MaybeArcMutexGuard<'lock, T> {
|
|
|
|
type Target = std::sync::MutexGuard<'lock, T>;
|
|
|
|
fn deref(&self) -> &std::sync::MutexGuard<'lock, T> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lock, T> DerefMut for MaybeArcMutexGuard<'lock, T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut std::sync::MutexGuard<'lock, T> {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MaybeArcMutex<T>(std::sync::Arc<std::sync::Mutex<T>>);
|
|
|
|
impl<T> MaybeArcMutex<T> {
|
|
|
|
pub fn new(val: T) -> Self {
|
|
|
|
Self(std::sync::Arc::new(std::sync::Mutex::new(val)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lock, T> MaybeArcMutex<T> {
|
|
|
|
pub fn lock(&'lock self) -> MaybeArcMutexGuard<'lock, T> {
|
|
|
|
MaybeArcMutexGuard(self.0.lock().unwrap())
|
|
|
|
}
|
|
|
|
}
|
2023-05-08 11:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "sync_fs"))]
|
|
|
|
mod inner {
|
2023-07-26 11:33:42 -04:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2023-05-08 11:02:02 -04:00
|
|
|
pub use std::rc::Rc as MaybeArc;
|
|
|
|
|
|
|
|
pub trait MaybeSync {}
|
|
|
|
impl<T> MaybeSync for T where T: ?Sized {}
|
|
|
|
pub trait MaybeSend {}
|
|
|
|
impl<T> MaybeSend for T where T: ?Sized {}
|
2023-07-25 17:43:00 -04:00
|
|
|
|
|
|
|
pub struct MaybeArcMutexGuard<'lock, T>(std::cell::RefMut<'lock, T>);
|
|
|
|
|
|
|
|
impl<'lock, T> Deref for MaybeArcMutexGuard<'lock, T> {
|
|
|
|
type Target = std::cell::RefMut<'lock, T>;
|
|
|
|
fn deref(&self) -> &std::cell::RefMut<'lock, T> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lock, T> DerefMut for MaybeArcMutexGuard<'lock, T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut std::cell::RefMut<'lock, T> {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MaybeArcMutex<T>(std::rc::Rc<std::cell::RefCell<T>>);
|
|
|
|
impl<T> MaybeArcMutex<T> {
|
|
|
|
pub fn new(val: T) -> Self {
|
|
|
|
Self(std::rc::Rc::new(std::cell::RefCell::new(val)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lock, T> MaybeArcMutex<T> {
|
|
|
|
pub fn lock(&'lock self) -> MaybeArcMutexGuard<'lock, T> {
|
|
|
|
MaybeArcMutexGuard(self.0.borrow_mut())
|
|
|
|
}
|
|
|
|
}
|
2023-05-08 11:02:02 -04:00
|
|
|
}
|