-
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding TryFromJs implementations for tuples (#3843)
* Adding TryFromJs implementations for tuples * rustfmt and clippies * rustfmt * Remove unit type * Clippies
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Implementation of [`TryFromJs`] for tuples. | ||
//! | ||
//! Tuples are converted from a JavaScript array, using similar semantics to `TypeScript` tuples: | ||
//! - If the tuple is shorter than the array, the extra elements are ignored. | ||
//! - If the tuple is longer than the array, the extra elements are `undefined`. | ||
//! - If the array is empty, all elements are `undefined`. | ||
//! | ||
//! A tuple of size 0 (unit type) does not implement [`TryFromJs`]. | ||
use crate::value::JsValue; | ||
use crate::{Context, JsResult}; | ||
|
||
use super::TryFromJs; | ||
|
||
macro_rules! impl_try_from_js_for_tuples { | ||
($($name:ident),*) => { | ||
impl<$($name: TryFromJs),*> TryFromJs for ($($name,)*) { | ||
fn try_from_js(value: &JsValue, context: &mut Context) -> JsResult<Self> { | ||
let vec: Vec<JsValue> = value.try_js_into(context)?; | ||
let mut iter = vec.into_iter(); | ||
|
||
Ok(( | ||
$( | ||
$name::try_from_js(&iter.next().unwrap_or_else(JsValue::undefined), context)?, | ||
)* | ||
)) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_try_from_js_for_tuples!(A); | ||
impl_try_from_js_for_tuples!(A, B); | ||
impl_try_from_js_for_tuples!(A, B, C); | ||
impl_try_from_js_for_tuples!(A, B, C, D); | ||
impl_try_from_js_for_tuples!(A, B, C, D, E); | ||
impl_try_from_js_for_tuples!(A, B, C, D, E, F); | ||
impl_try_from_js_for_tuples!(A, B, C, D, E, F, G); | ||
impl_try_from_js_for_tuples!(A, B, C, D, E, F, G, H); | ||
impl_try_from_js_for_tuples!(A, B, C, D, E, F, G, H, I); | ||
impl_try_from_js_for_tuples!(A, B, C, D, E, F, G, H, I, J); |