-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize DateTime deserialization to w/o dyn alloc
- Loading branch information
Showing
8 changed files
with
88 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "tzdb" | ||
version = "0.1.2" | ||
version = "0.1.3" | ||
edition = "2021" | ||
authors = ["René Kijewski <[email protected]>"] | ||
repository = "https://github.com/Kijewski/tzdb" | ||
|
@@ -12,7 +12,7 @@ readme = "README.md" | |
rust-version = "1.57" | ||
|
||
[dependencies] | ||
tz-rs = "0.6.3" | ||
tz-rs = "0.6.4" | ||
|
||
# optional dependencies | ||
byte-slice-cast = { version = "1.2.1", optional = true } | ||
|
@@ -44,7 +44,7 @@ serde-as = ["serde", "serde_with"] | |
docsrs = ["document-features", "testing"] | ||
|
||
# Internal feature, used when testing. | ||
testing = ["serde", "serde_with", "serde/derive", "serde_with/macros"] | ||
testing = ["serde-as", "serde/derive", "serde_with/macros"] | ||
|
||
[package.metadata.docs.rs] | ||
features = ["docsrs"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,66 @@ | ||
use serde::de::Error; | ||
use std::fmt; | ||
use std::marker::PhantomData; | ||
|
||
use serde::de::{Error, SeqAccess, Visitor}; | ||
use serde::ser::SerializeTuple; | ||
use serde::Serializer; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use tz::timezone::LocalTimeType; | ||
use tz::{DateTime, TimeZone}; | ||
use tz::{DateTime, TimeZoneRef, UtcDateTime}; | ||
|
||
pub(super) type TzTuple<'de> = (i32, bool, &'de str); | ||
|
||
pub(super) fn deserialize_tz<E: Error>(value: TzTuple<'_>) -> Result<TimeZone, E> { | ||
pub(super) fn project_utc<E: Error>(utc: UtcDateTime, value: TzTuple<'_>) -> Result<DateTime, E> { | ||
let (ut_offset, is_dst, tz) = value; | ||
let tz = match tz { | ||
"" => None, | ||
tz => Some(tz.as_bytes()), | ||
}; | ||
let tz = LocalTimeType::new(ut_offset, is_dst, tz).map_err(E::custom)?; | ||
TimeZone::new(vec![], vec![tz], vec![], None).map_err(E::custom) | ||
let tz = &[tz]; | ||
let tz = TimeZoneRef::new(&[], tz, &[], &None).map_err(E::custom)?; | ||
utc.project(tz).map_err(E::custom) | ||
} | ||
|
||
pub(super) fn deserialize_date_time<'de, D: Deserializer<'de>, T: Deserialize<'de>>( | ||
deserializer: D, | ||
) -> Result<(T, TzTuple<'de>), D::Error> { | ||
struct UnixTpl<T>(PhantomData<T>); | ||
|
||
impl<'de, T: Deserialize<'de>> Visitor<'de> for UnixTpl<T> { | ||
type Value = (T, TzTuple<'de>); | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
formatter.write_str("UnixTime tuple") | ||
} | ||
|
||
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> { | ||
let ut = seq | ||
.next_element()? | ||
.ok_or_else(|| A::Error::custom("expected UnixTime"))?; | ||
let tz = seq | ||
.next_element()? | ||
.ok_or_else(|| A::Error::custom("expected LocalTimeType"))?; | ||
Ok((ut, tz)) | ||
} | ||
} | ||
|
||
deserializer.deserialize_tuple(2, UnixTpl(PhantomData)) | ||
} | ||
|
||
pub(super) fn serialize_tz<S: Serializer>( | ||
pub(super) fn serialize_date_time<S: Serializer, T: Serialize>( | ||
serializer: S, | ||
source: &DateTime, | ||
mut seq: <S as Serializer>::SerializeTuple, | ||
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> { | ||
date_time: T, | ||
) -> Result<S::Ok, S::Error> { | ||
let local_time_type = source.local_time_type(); | ||
seq.serialize_element(&( | ||
let local_time_type = ( | ||
local_time_type.ut_offset(), | ||
local_time_type.is_dst(), | ||
local_time_type.time_zone_designation(), | ||
))?; | ||
); | ||
|
||
let mut seq = serializer.serialize_tuple(2)?; | ||
seq.serialize_element(&date_time)?; | ||
seq.serialize_element(&local_time_type)?; | ||
seq.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters