diff --git a/crates/neon/src/types_impl/extract/private.rs b/crates/neon/src/types_impl/extract/private.rs index d78f693b3..45048d444 100644 --- a/crates/neon/src/types_impl/extract/private.rs +++ b/crates/neon/src/types_impl/extract/private.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use crate::{ context::FunctionContext, handle::{Handle, Root}, @@ -5,7 +7,7 @@ use crate::{ result::{NeonResult, Throw}, types::{ buffer::Binary, - extract::{ArrayBuffer, Buffer, Date, Error}, + extract::{ArrayBuffer, Buffer, Date, Error, TryIntoJs}, JsTypedArray, Value, }, }; @@ -34,6 +36,8 @@ impl Sealed for () {} impl Sealed for &str {} +impl Sealed for &String {} + impl<'cx, V: Value> Sealed for Handle<'cx, V> {} impl Sealed for Root {} @@ -49,6 +53,13 @@ where { } +impl Sealed for &Vec +where + JsTypedArray: Value, + T: Binary, +{ +} + impl Sealed for &[T] where JsTypedArray: Value, @@ -56,6 +67,10 @@ where { } +impl<'cx, T> Sealed for Arc where for<'a> &'a T: TryIntoJs<'cx> {} + +impl<'cx, T> Sealed for Box where for<'a> &'a T: TryIntoJs<'cx> {} + impl_sealed!( u8, u16, diff --git a/crates/neon/src/types_impl/extract/try_into_js.rs b/crates/neon/src/types_impl/extract/try_into_js.rs index 126f9fde7..28eb3733c 100644 --- a/crates/neon/src/types_impl/extract/try_into_js.rs +++ b/crates/neon/src/types_impl/extract/try_into_js.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use crate::{ context::{Context, Cx}, handle::Handle, @@ -63,6 +65,30 @@ where } } +impl<'cx, T, V> TryIntoJs<'cx> for Box +where + for<'a> &'a T: TryIntoJs<'cx, Value = V>, + V: Value, +{ + type Value = V; + + fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> { + self.as_ref().try_into_js(cx) + } +} + +impl<'cx, T, V> TryIntoJs<'cx> for Arc +where + for<'a> &'a T: TryIntoJs<'cx, Value = V>, + V: Value, +{ + type Value = V; + + fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> { + self.as_ref().try_into_js(cx) + } +} + macro_rules! impl_number { ($ty:ident) => { impl<'cx> TryIntoJs<'cx> for $ty { @@ -91,7 +117,15 @@ impl<'cx> TryIntoJs<'cx> for String { } } -impl<'cx> TryIntoJs<'cx> for &'cx str { +impl<'a, 'cx> TryIntoJs<'cx> for &'a str { + type Value = JsString; + + fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> { + Ok(cx.string(self)) + } +} + +impl<'a, 'cx> TryIntoJs<'cx> for &'a String { type Value = JsString; fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> { @@ -111,7 +145,19 @@ where } } -impl<'cx, T> TryIntoJs<'cx> for &'cx [T] +impl<'a, 'cx, T> TryIntoJs<'cx> for &'a Vec +where + JsTypedArray: Value, + T: Binary, +{ + type Value = JsTypedArray; + + fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> { + JsTypedArray::from_slice(cx, self) + } +} + +impl<'a, 'cx, T> TryIntoJs<'cx> for &'a [T] where JsTypedArray: Value, T: Binary,