Skip to content

Commit

Permalink
feat(neon): Add TryIntoJs implementations for more pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed Sep 19, 2024
1 parent dbb93bb commit eb28eca
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
31 changes: 30 additions & 1 deletion crates/neon/src/types_impl/extract/private.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::sync::Arc;

use crate::{
context::FunctionContext,
handle::{Handle, Root},
object::Object,
result::{NeonResult, Throw},
types::{
buffer::Binary,
extract::{ArrayBuffer, Buffer, Date, Error},
extract::{ArrayBuffer, Buffer, Date, Error, TryIntoJs},
JsTypedArray, Value,
},
};
Expand Down Expand Up @@ -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<O: Object> Sealed for Root<O> {}
Expand All @@ -49,13 +53,38 @@ where
{
}

impl<T> Sealed for Box<[T]>
where
JsTypedArray<T>: Value,
T: Binary,
{
}

impl<T, const N: usize> Sealed for [T; N]
where
JsTypedArray<T>: Value,
T: Binary,
{
}

impl<T> Sealed for &Vec<T>
where
JsTypedArray<T>: Value,
T: Binary,
{
}

impl<T> Sealed for &[T]
where
JsTypedArray<T>: Value,
T: Binary,
{
}

impl<'cx, T> Sealed for Arc<T> where for<'a> &'a T: TryIntoJs<'cx> {}

impl<'cx, T> Sealed for Box<T> where T: TryIntoJs<'cx> {}

impl_sealed!(
u8,
u16,
Expand Down
73 changes: 71 additions & 2 deletions crates/neon/src/types_impl/extract/try_into_js.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use crate::{
context::{Context, Cx},
handle::Handle,
Expand Down Expand Up @@ -63,6 +65,29 @@ where
}
}

impl<'cx, T> TryIntoJs<'cx> for Box<T>
where
T: TryIntoJs<'cx>,
{
type Value = T::Value;

fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
(*self).try_into_js(cx)
}
}

impl<'cx, T, V> TryIntoJs<'cx> for Arc<T>
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 {
Expand Down Expand Up @@ -91,7 +116,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> {
Expand All @@ -111,7 +144,43 @@ where
}
}

impl<'cx, T> TryIntoJs<'cx> for &'cx [T]
impl<'cx, T> TryIntoJs<'cx> for Box<[T]>
where
JsTypedArray<T>: Value,
T: Binary,
{
type Value = JsTypedArray<T>;

fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
JsTypedArray::from_slice(cx, &self)
}
}

impl<'cx, T, const N: usize> TryIntoJs<'cx> for [T; N]
where
JsTypedArray<T>: Value,
T: Binary,
{
type Value = JsTypedArray<T>;

fn try_into_js(self, cx: &mut Cx<'cx>) -> JsResult<'cx, Self::Value> {
JsTypedArray::from_slice(cx, self.as_slice())
}
}

impl<'a, 'cx, T> TryIntoJs<'cx> for &'a Vec<T>
where
JsTypedArray<T>: Value,
T: Binary,
{
type Value = JsTypedArray<T>;

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<T>: Value,
T: Binary,
Expand Down

0 comments on commit eb28eca

Please sign in to comment.