chrono icon indicating copy to clipboard operation
chrono copied to clipboard

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

Open demhadais opened this issue 2 months ago • 2 comments

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!

demhadais avatar Oct 03 '25 21:10 demhadais

Feel free to submit a PR.

djc avatar Oct 04 '25 07:10 djc

Wow awesome, appreciate the prompt response!

demhadais avatar Oct 06 '25 14:10 demhadais