From 9b719062f464ee6c06190e326901d64779af3ce0 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 5 Jan 2024 07:47:20 +0100 Subject: [PATCH 1/6] chrono: Add support for NaiveDate --- src/chrono.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/containers.rs | 4 ++++ 2 files changed, 49 insertions(+) diff --git a/src/chrono.rs b/src/chrono.rs index 56a9382..d792c94 100644 --- a/src/chrono.rs +++ b/src/chrono.rs @@ -53,6 +53,25 @@ impl IntoDart for chrono::DateTime { } } +impl IntoDart for chrono::NaiveDate { + /// on the other side of FFI, value should be reconstructed like: + /// + /// - hydrate into Dart [DateTime](https://api.dart.dev/stable/2.18.0/dart-core/DateTime/DateTime.fromMicrosecondsSinceEpoch.html) + /// `DateTime.fromMicrosecondsSinceEpoch(raw, isUtc: true);` + /// + /// - hydrate into Rust [NaiveDate](chrono::NaiveDate) + /// ```rust,ignore + /// let s = (raw / 1_000_000) as i64; + /// let ns = (raw.rem_euclid(1_000_000) * 1_000) as u32; + /// chrono::NaiveDate::from_timestamp(s, ns) + /// ``` + /// + /// note that it could overflow under the same conditions as of [chrono::NaiveDate::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDate.html#method.from_timestamp) + fn into_dart(self) -> DartCObject { + self.timestamp_micros().into_dart() + } +} + impl IntoDart for chrono::NaiveDateTime { /// on the other side of FFI, value should be reconstructed like: /// @@ -137,6 +156,32 @@ impl DartTypedDataTypeTrait for chrono::DateTime { } } +impl IntoDart for Vec { + fn into_dart(self) -> DartCObject { + self.iter() + .map(chrono::NaiveDate::timestamp_micros) + .collect::>() + .into_dart() + } +} + +impl IntoDart for [chrono::NaiveDate; N] { + fn into_dart(self) -> DartCObject { + let vec: Vec<_> = self.into(); + vec.into_dart() + } +} + +impl DartTypedDataTypeTrait for chrono::NaiveDate { + fn dart_typed_data_type() -> DartTypedDataType { + DartTypedDataType::Int64 + } + + fn function_pointer_of_free_zero_copy_buffer() -> DartHandleFinalizer { + free_zero_copy_buffer_i64 + } +} + impl IntoDart for Vec { fn into_dart(self) -> DartCObject { self.iter() diff --git a/tests/containers.rs b/tests/containers.rs index 76cdb3d..3859b0b 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -236,6 +236,10 @@ fn return_backtrace() -> backtrace::Backtrace { backtrace::Backtrace::new() } +#[cfg(feature = "chrono")] +fn return_chrono_naive_date_time() -> chrono::NaiveDate { + chrono::NaiveDate::from_ymd(1776, 7, 4) +} #[cfg(feature = "chrono")] fn return_chrono_naive_date_time() -> chrono::NaiveDateTime { chrono::NaiveDate::from_ymd(2016, 7, 8).and_hms_micro(9, 10, 11, 123_456) From 3d126a354a0912bdbb92d6850126305656f07d97 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 5 Jan 2024 10:57:43 +0100 Subject: [PATCH 2/6] Fix actual conversion of NaiveDate to Int64 --- src/chrono.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/chrono.rs b/src/chrono.rs index d792c94..5eb7a0b 100644 --- a/src/chrono.rs +++ b/src/chrono.rs @@ -59,16 +59,16 @@ impl IntoDart for chrono::NaiveDate { /// - hydrate into Dart [DateTime](https://api.dart.dev/stable/2.18.0/dart-core/DateTime/DateTime.fromMicrosecondsSinceEpoch.html) /// `DateTime.fromMicrosecondsSinceEpoch(raw, isUtc: true);` /// - /// - hydrate into Rust [NaiveDate](chrono::NaiveDate) + /// - hydrate into Rust [NaiveDateTime](chrono::NaiveDate) /// ```rust,ignore /// let s = (raw / 1_000_000) as i64; /// let ns = (raw.rem_euclid(1_000_000) * 1_000) as u32; - /// chrono::NaiveDate::from_timestamp(s, ns) + /// chrono::NaiveDateTime::from_timestamp(s, ns) /// ``` /// - /// note that it could overflow under the same conditions as of [chrono::NaiveDate::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDate.html#method.from_timestamp) + /// note that it could overflow under the same conditions as of [chrono::NaiveDateTime::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp) fn into_dart(self) -> DartCObject { - self.timestamp_micros().into_dart() + self.signed_duration_since(chrono::NaiveDate::MIN).into_dart() } } @@ -159,7 +159,7 @@ impl DartTypedDataTypeTrait for chrono::DateTime { impl IntoDart for Vec { fn into_dart(self) -> DartCObject { self.iter() - .map(chrono::NaiveDate::timestamp_micros) + .map(|lhs|lhs.signed_duration_since(chrono::NaiveDate::MIN)) .collect::>() .into_dart() } From 66b62c6878fee5830fe277ce2eebff14e44308e5 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 5 Jan 2024 10:58:17 +0100 Subject: [PATCH 3/6] Do not use duplicate method names --- tests/containers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/containers.rs b/tests/containers.rs index 3859b0b..f916e8a 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -237,7 +237,7 @@ fn return_backtrace() -> backtrace::Backtrace { } #[cfg(feature = "chrono")] -fn return_chrono_naive_date_time() -> chrono::NaiveDate { +fn return_chrono_naive_date() -> chrono::NaiveDate { chrono::NaiveDate::from_ymd(1776, 7, 4) } #[cfg(feature = "chrono")] From bfc8d70ea574fa742f36e245d94dd539f9f5d2f9 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 5 Jan 2024 11:01:07 +0100 Subject: [PATCH 4/6] Include test method in tests --- tests/containers.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/containers.rs b/tests/containers.rs index f916e8a..0381dc3 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -197,6 +197,7 @@ fn main() { assert!(isolate.post(return_backtrace())); #[cfg(feature = "chrono")] { + assert!(isolate.post(return_chrono_naive_date())); assert!(isolate.post(return_chrono_naive_date_time())); assert!(isolate.post(return_chrono_date_time_utc())); assert!(isolate.post(return_chrono_date_time_local())); From 265bb2b74ab4120c48c4fa8976b9eca6b17cbe1a Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 5 Jan 2024 11:07:47 +0100 Subject: [PATCH 5/6] Fix deprecation warnings --- tests/containers.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/containers.rs b/tests/containers.rs index 0381dc3..083861d 100644 --- a/tests/containers.rs +++ b/tests/containers.rs @@ -239,11 +239,15 @@ fn return_backtrace() -> backtrace::Backtrace { #[cfg(feature = "chrono")] fn return_chrono_naive_date() -> chrono::NaiveDate { - chrono::NaiveDate::from_ymd(1776, 7, 4) + chrono::NaiveDate::from_ymd_opt(1776, 7, 4) + .expect("The input date for testing is required to be valid") } #[cfg(feature = "chrono")] fn return_chrono_naive_date_time() -> chrono::NaiveDateTime { - chrono::NaiveDate::from_ymd(2016, 7, 8).and_hms_micro(9, 10, 11, 123_456) + chrono::NaiveDate::from_ymd_opt(2016, 7, 8) + .map(|nd| nd.and_hms_micro_opt(9, 10, 11, 123_456)) + .flatten() + .expect("The input date and time for testing are required to be valid") } #[cfg(feature = "chrono")] fn return_chrono_date_time_utc() -> chrono::DateTime { From b7158d2f4d7fd4638ac0e2d9a4fdd5bc29d491aa Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 5 Jan 2024 15:25:39 +0100 Subject: [PATCH 6/6] Apply formatting --- src/chrono.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/chrono.rs b/src/chrono.rs index 5eb7a0b..b1c1624 100644 --- a/src/chrono.rs +++ b/src/chrono.rs @@ -68,7 +68,8 @@ impl IntoDart for chrono::NaiveDate { /// /// note that it could overflow under the same conditions as of [chrono::NaiveDateTime::from_timestamp](https://docs.rs/chrono/0.4.20/chrono/naive/struct.NaiveDateTime.html#method.from_timestamp) fn into_dart(self) -> DartCObject { - self.signed_duration_since(chrono::NaiveDate::MIN).into_dart() + self.signed_duration_since(chrono::NaiveDate::MIN) + .into_dart() } } @@ -159,7 +160,7 @@ impl DartTypedDataTypeTrait for chrono::DateTime { impl IntoDart for Vec { fn into_dart(self) -> DartCObject { self.iter() - .map(|lhs|lhs.signed_duration_since(chrono::NaiveDate::MIN)) + .map(|lhs| lhs.signed_duration_since(chrono::NaiveDate::MIN)) .collect::>() .into_dart() }