foobar
foobar8mo ago

WASM / Extract and manipulate Array of Date

I try to extract and manipulate an array of date in a function The rust fn doesn't compile. How to manipulate Date and convert it ? Does someone have a repository/link with example of Rust + Deno library ? Cargo.toml
[dependencies]
wasm-bindgen = "=0.2.87"
time = { version = "0.3", features = ["parsing", "macros"] }
js-sys = "0.3.64"
[dependencies]
wasm-bindgen = "=0.2.87"
time = { version = "0.3", features = ["parsing", "macros"] }
js-sys = "0.3.64"
mod.ts
import { instantiate } from "./lib/rs_lib.generated.js";

const { DateArrayToString } = await instantiate();

console.log(DateArrayToString( [new Date(2015, 11, 1 ), new Date(2016, 7, 1 ), new Date(2016, 7, 19 )]))
import { instantiate } from "./lib/rs_lib.generated.js";

const { DateArrayToString } = await instantiate();

console.log(DateArrayToString( [new Date(2015, 11, 1 ), new Date(2016, 7, 1 ), new Date(2016, 7, 19 )]))
lib.rs
use wasm_bindgen::prelude::*;

use js_sys::Array;
use js_sys::Date;

#[wasm_bindgen]
pub fn DateArrayToString(arr:Array) -> JsString {
let result : JsString;
for i in 0..arr.length() {
// problem is here
let d : Date = arr.get(i).as_ref();
// Concat
result = result.concat(d.to_string());
}
return result;
}
use wasm_bindgen::prelude::*;

use js_sys::Array;
use js_sys::Date;

#[wasm_bindgen]
pub fn DateArrayToString(arr:Array) -> JsString {
let result : JsString;
for i in 0..arr.length() {
// problem is here
let d : Date = arr.get(i).as_ref();
// Concat
result = result.concat(d.to_string());
}
return result;
}
2 Replies
Deno AI Helper
Deno AI Helper8mo ago
The documentation does not provide a clear example of how to manipulate an array of dates in Rust and convert it for use in Deno. However, it does mention that for more complex types like classes or strings, you would want to use tools that generate type bindings between JavaScript and the language used to compile to WebAssembly. In your case, you are using Rust and you might want to look into using the wasm-bindgen tool which is designed to generate these type bindings. The wasm-bindgen tool can help you to work with complex types and JavaScript APIs in your Rust code. The documentation also mentions the js_sys crate which provides bindings to JavaScript's standard, built-in objects. This might be useful for working with JavaScript Date objects in your Rust code. However, the exact code to manipulate an array of dates in Rust and convert it for use in Deno is not provided in the documentation. You might want to look for more specific examples or tutorials online that deal with this particular use case. Remember to always test your code thoroughly to ensure it works as expected. 🦕
foobar
foobar8mo ago
Solution on stackoverflow
use js_sys::{Array, Date, JsString};
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = DateArrayToString)]
pub fn date_array_to_string(arr: Array) -> JsString {
let mut result = JsString::from("");
for date_value in arr.values() {
let d: Date = date_value.expect("Some error occured.").into();
result = result.concat(&d.to_string());
}

result
}
use js_sys::{Array, Date, JsString};
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = DateArrayToString)]
pub fn date_array_to_string(arr: Array) -> JsString {
let mut result = JsString::from("");
for date_value in arr.values() {
let d: Date = date_value.expect("Some error occured.").into();
result = result.concat(&d.to_string());
}

result
}