Skip to content

Commit

Permalink
Add TryIntoJsResult for vectors (#3993)
Browse files Browse the repository at this point in the history
* Add TryIntoJsResult for vectors

This way JsFunction made with into_js_function can return vectors.

* Fix build error
  • Loading branch information
hansl authored Sep 11, 2024
1 parent affa652 commit 50356e9
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/engine/src/try_into_js_result_impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Declare implementations of [`TryIntoJsResult`] trait for various types.

use crate::object::JsArray;
use crate::{Context, JsResult, JsValue, TryIntoJsResult};

impl<T> TryIntoJsResult for T
Expand All @@ -11,6 +12,20 @@ where
}
}

impl<T> TryIntoJsResult for Vec<T>
where
T: TryIntoJsResult,
{
fn try_into_js_result(self, cx: &mut Context) -> JsResult<JsValue> {
let array = JsArray::new(cx);
// We have to manually enumerate because we cannot return a Result from a map monad.
for value in self {
array.push(value.try_into_js_result(cx)?, cx)?;
}
Ok(array.into())
}
}

impl<T> TryIntoJsResult for Option<T>
where
T: TryIntoJsResult,
Expand Down

0 comments on commit 50356e9

Please sign in to comment.