Browse Source

Add TryIntoJsResult for vectors (#3993)

* Add TryIntoJsResult for vectors

This way JsFunction made with into_js_function can return vectors.

* Fix build error
pull/3995/head
Hans Larsen 2 months ago committed by GitHub
parent
commit
50356e9518
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 15
      core/engine/src/try_into_js_result_impls.rs

15
core/engine/src/try_into_js_result_impls.rs

@ -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
@ -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,

Loading…
Cancel
Save