Skip to content

Commit

Permalink
remove the mutex stuff, it seems useless
Browse files Browse the repository at this point in the history
  • Loading branch information
dherman committed Feb 2, 2025
1 parent 4d979cc commit 56a1e27
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 97 deletions.
56 changes: 2 additions & 54 deletions crates/neon/src/types_impl/extract/container.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
cell::{Ref, RefCell, RefMut},
rc::Rc,
sync::{Arc, Mutex, MutexGuard},
sync::Arc,
};

use crate::{
Expand All @@ -14,7 +14,7 @@ use crate::{
},
};

use super::error::{MutexError, RefCellError, RustTypeExpected};
use super::error::{RefCellError, RustTypeExpected};

pub trait Container {
fn container_name() -> &'static str;
Expand All @@ -38,12 +38,6 @@ impl<T> Container for Arc<T> {
}
}

impl<T> Container for Mutex<T> {
fn container_name() -> &'static str {
"std::sync::Mutex"
}
}

impl<'cx, T: 'static> TryFromJs<'cx> for &'cx RefCell<T> {
type Error = RustTypeExpected<RefCell<T>>;

Expand Down Expand Up @@ -154,49 +148,3 @@ where
Ok(JsBox::manually_finalize(cx, self))
}
}

impl<'cx, T: 'static> TryFromJs<'cx> for &'cx Mutex<T> {
type Error = RustTypeExpected<Mutex<T>>;

fn try_from_js(
cx: &mut Cx<'cx>,
v: Handle<'cx, JsValue>,
) -> NeonResult<Result<Self, Self::Error>> {
match v.downcast::<JsBox<Arc<Mutex<T>>>, _>(cx) {
Ok(v) => {
let arc = JsBox::deref(&v);
Ok(Ok(<Arc<Mutex<T>> as std::ops::Deref>::deref(arc)))
}
Err(_) => Ok(Err(RustTypeExpected::new())),
}
}
}

impl<'cx, T: 'static> TryFromJs<'cx> for MutexGuard<'cx, T> {
type Error = MutexError;

fn try_from_js(
cx: &mut Cx<'cx>,
v: Handle<'cx, JsValue>,
) -> NeonResult<Result<Self, Self::Error>> {
match v.downcast::<JsBox<Arc<Mutex<T>>>, _>(cx) {
Ok(v) => {
let arc = JsBox::deref(&v);
let mutex = <Arc<Mutex<T>> as std::ops::Deref>::deref(arc);
Ok(mutex.lock().map_err(|_| MutexError::Poisoned))
}
Err(_) => Ok(Err(MutexError::WrongType)),
}
}
}

impl<'cx, T> TryIntoJs<'cx> for Mutex<T>
where
T: 'static,
{
type Value = JsBox<Arc<Mutex<T>>>;

fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
Ok(JsBox::manually_finalize(cx, Arc::new(self)))
}
}
36 changes: 0 additions & 36 deletions crates/neon/src/types_impl/extract/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,42 +123,6 @@ impl<'cx> TryIntoJs<'cx> for RefCellError {

impl private::Sealed for RefCellError {}

pub enum MutexError {
WrongType,
Poisoned,
}

impl fmt::Display for MutexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
MutexError::WrongType => write!(f, "expected {}", RefCell::<()>::container_name()),
MutexError::Poisoned => write!(f, "std::sync::Mutex is poisoned"),
}
}
}

impl fmt::Debug for MutexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
MutexError::WrongType => f.debug_tuple("MutexError::WrongType").finish(),
MutexError::Poisoned => f.debug_tuple("MutexError::Poisoned").finish(),
}
}
}

impl<'cx> TryIntoJs<'cx> for MutexError {
type Value = JsError;

fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
match self {
MutexError::WrongType => JsError::type_error(cx, self.to_string()),
MutexError::Poisoned => JsError::error(cx, self.to_string()),
}
}
}

impl private::Sealed for MutexError {}

impl<'cx> TryIntoJs<'cx> for Infallible {
type Value = JsValue;

Expand Down
8 changes: 1 addition & 7 deletions crates/neon/src/types_impl/extract/private.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
cell::{Ref, RefCell, RefMut},
rc::Rc,
sync::{Arc, Mutex, MutexGuard},
sync::Arc,
};

use crate::{
Expand Down Expand Up @@ -59,14 +59,8 @@ impl<T> Sealed for Arc<T> {}

impl<T> Sealed for Rc<T> {}

impl<T> Sealed for Mutex<T> {}

impl<T> Sealed for &Mutex<T> {}

impl<'a, T> Sealed for Ref<'a, T> {}

impl<'a, T> Sealed for RefMut<'a, T> {}

impl<'a, T> Sealed for MutexGuard<'a, T> {}

impl_sealed!(u8, u16, u32, i8, i16, i32, f32, f64, bool, String, Date, Throw, Error,);

0 comments on commit 56a1e27

Please sign in to comment.