fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Home Page:https://fzyzcjy.github.io/flutter_rust_bridge/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix warning use of deprecated associated function `chrono::NaiveDateTime::from_timestamp_micros`

VladTheJunior opened this issue · comments

When generating code for struct which contains chrono::NaiveDateTime compiler get warnings about conversion to NaiveDateTime. It should use DateTime::from_timestamp_micros instead

impl SseDecode for chrono::NaiveDateTime {
    // Codec=Sse (Serialization based), see doc to use other codecs
    fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
        let mut inner = <i64>::sse_decode(deserializer);
        return chrono::NaiveDateTime::from_timestamp_micros(inner) // <-- this line
            .expect("invalid or out-of-range datetime");
    }
}

Looks reasonable to avoid this warning. But I wonder, if someone is really using the type NaiveDateTime (e.g. the code you show), I guess it is not possible to call DateTime's constructor, since that one may return a datetime instead of naivedatetime?

Yes, but DateTime has these methods:

pub const fn naive_utc(&self) -> NaiveDateTime
pub fn naive_local(&self) -> NaiveDateTime

https://docs.rs/chrono/latest/chrono/struct.DateTime.html#method.naive_utc
https://docs.rs/chrono/latest/chrono/struct.DateTime.html#method.naive_local

Btw deprecated method is also used here

impl SseDecode for chrono::DateTime<chrono::Local> {
    // Codec=Sse (Serialization based), see doc to use other codecs
    fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
        let mut inner = <i64>::sse_decode(deserializer);
        return chrono::DateTime::<chrono::Local>::from(
            chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
                chrono::NaiveDateTime::from_timestamp_micros(inner)
                    .expect("invalid or out-of-range datetime"),
                chrono::Utc,
            ),
        );
    }
}

I see. Feel free to PR, alternatively I will fix it in the next batch!

I will wait the next version, ty

Oops I somehow missed it... Will fix it now

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.