Skip to content

Why aren't all the Wasm conversion traits implemented for DateTime<Utc>? #1749

@demhadais

Description

@demhadais

I hope the title doesn't come off entitled or demanding, I just want to be concise. I'm sure this has been covered elsewhere, but how come DateTime<Utc> doesn't implement all the traits that would make it usable with the wasm-bindgen macro? For example, this won't compile:

use chrono::{DateTime, Utc};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct MyStruct {
    pub time: DateTime<Utc>,
}

Surely I'm missing something, but we can give the type full Wasm support with the following:

use chrono::{DateTime, Utc};
use wasm_bindgen::{
    convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi},
    describe::{WasmDescribe, WasmDescribeVector},
    prelude::wasm_bindgen,
};

impl FromWasmAbi for DateTime<Utc> {
    type Abi = <js_sys::Date as FromWasmAbi>::Abi;
    unsafe fn from_abi(js: Self::Abi) -> Self {
        unsafe { js_sys::Date::from_abi(js).into() }
    }
}

impl IntoWasmAbi for DateTime<Utc> {
    type Abi = <js_sys::Date as IntoWasmAbi>::Abi;
    fn into_abi(self) -> Self::Abi {
        let as_js: js_sys::Date = self.into();
        as_js.into_abi()
    }
}

impl WasmDescribe for DateTime<Utc> {
    fn describe() {
        js_sys::Date::describe()
    }
}

impl WasmDescribeVector for DateTime<Utc> {
    fn describe_vector() {
        js_sys::Date::describe_vector()
    }
}

impl OptionFromWasmAbi for DateTime<Utc> {
    fn is_none(abi: &Self::Abi) -> bool {
        js_sys::Date::is_none(abi)
    }
}

impl OptionIntoWasmAbi for DateTime<Utc> {
    fn none() -> Self::Abi {
        js_sys::Date::none()
    }
}

#[wasm_bindgen]
pub struct MyStruct {
    pub time: DateTime<Utc>,
}

It might be that nobody has needed this feature before me (though I doubt that), but if that's the case then I'm happy to submit a pull request.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions