From 50356e951840e058d69e9ca68fffeef3459d2229 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Tue, 10 Sep 2024 21:10:23 -0700 Subject: [PATCH] Add TryIntoJsResult for vectors (#3993) * Add TryIntoJsResult for vectors This way JsFunction made with into_js_function can return vectors. * Fix build error --- core/engine/src/try_into_js_result_impls.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/engine/src/try_into_js_result_impls.rs b/core/engine/src/try_into_js_result_impls.rs index e4fe117ec7..26a5309282 100644 --- a/core/engine/src/try_into_js_result_impls.rs +++ b/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 TryIntoJsResult for T @@ -11,6 +12,20 @@ where } } +impl TryIntoJsResult for Vec +where + T: TryIntoJsResult, +{ + fn try_into_js_result(self, cx: &mut Context) -> JsResult { + 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 TryIntoJsResult for Option where T: TryIntoJsResult,