Skip to content

Commit

Permalink
feat(neon): Add TryFromJs implementation for Root
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed Sep 17, 2024
1 parent 0cd37f8 commit dbb93bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion crates/neon/src/types_impl/extract/private.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
context::FunctionContext,
handle::Handle,
handle::{Handle, Root},
object::Object,
result::{NeonResult, Throw},
types::{
buffer::Binary,
Expand Down Expand Up @@ -35,6 +36,8 @@ impl Sealed for &str {}

impl<'cx, V: Value> Sealed for Handle<'cx, V> {}

impl<O: Object> Sealed for Root<O> {}

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

impl<T, E> Sealed for Result<T, E> {}
Expand Down
22 changes: 21 additions & 1 deletion crates/neon/src/types_impl/extract/try_from_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::{convert::Infallible, ptr};

use crate::{
context::{internal::ContextInternal, Cx},
handle::Handle,
handle::{Handle, Root},
object::Object,
result::{NeonResult, ResultExt, Throw},
sys,
types::{
Expand Down Expand Up @@ -45,6 +46,25 @@ where
from_js!();
}

impl<'cx, O> TryFromJs<'cx> for Root<O>
where
O: Object,
{
type Error = TypeExpected<O>;

fn try_from_js(
cx: &mut Cx<'cx>,
v: Handle<'cx, JsValue>,
) -> NeonResult<Result<Self, Self::Error>> {
Ok(match v.downcast::<O, _>(cx) {
Ok(v) => Ok(v.root(cx)),
Err(_) => Err(TypeExpected::new()),
})
}

from_js!();
}

impl<'cx, T> TryFromJs<'cx> for Option<T>
where
T: TryFromJs<'cx>,
Expand Down

0 comments on commit dbb93bb

Please sign in to comment.