Skip to content

Commit df2d69c

Browse files
committed
Add more Duration* and Timestamp* adapters with different base units
The current Duration* and Timestamp* only support full seconds. Other time units, like milliseconds, microseconds, or nanoseconds, are sometimes required too.
1 parent 771d85e commit df2d69c

File tree

4 files changed

+295
-63
lines changed

4 files changed

+295
-63
lines changed

src/de/impls.rs

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,14 @@ where
693693

694694
macro_rules! use_signed_duration {
695695
(
696-
$ty:ty =>
697696
$main_trait:ident $internal_trait:ident =>
698-
$converter:ident =>
699-
$({
700-
$format:ty, $strictness:ty =>
701-
$($tbound:ident: $bound:ident)*
702-
})*
697+
{
698+
$ty:ty; $converter:ident =>
699+
$({
700+
$format:ty, $strictness:ty =>
701+
$($tbound:ident: $bound:ident)*
702+
})*
703+
}
703704
) => {
704705
$(
705706
impl<'de, $($tbound,)*> DeserializeAs<'de, $ty> for $main_trait<$format, $strictness>
@@ -716,42 +717,63 @@ macro_rules! use_signed_duration {
716717
}
717718
)*
718719
};
720+
(
721+
$( $main_trait:ident $internal_trait:ident, )+ => $rest:tt
722+
) => {
723+
$( use_signed_duration!($main_trait $internal_trait => $rest); )+
724+
};
719725
}
720726

721727
use_signed_duration!(
722-
Duration =>
723-
DurationSeconds DurationSeconds =>
724-
to_std_duration =>
725-
{u64, Strict =>}
726-
{f64, Strict =>}
727-
{String, Strict =>}
728-
{FORMAT, Flexible => FORMAT: Format}
728+
DurationSeconds DurationSeconds,
729+
DurationMilliSeconds DurationMilliSeconds,
730+
DurationMicroSeconds DurationMicroSeconds,
731+
DurationNanoSeconds DurationNanoSeconds,
732+
=> {
733+
Duration; to_std_duration =>
734+
{u64, Strict =>}
735+
{f64, Strict =>}
736+
{String, Strict =>}
737+
{FORMAT, Flexible => FORMAT: Format}
738+
}
729739
);
730740
use_signed_duration!(
731-
Duration =>
732-
DurationSecondsWithFrac DurationSecondsWithFrac =>
733-
to_std_duration =>
734-
{f64, Strict =>}
735-
{String, Strict =>}
736-
{FORMAT, Flexible => FORMAT: Format}
741+
DurationSecondsWithFrac DurationSecondsWithFrac,
742+
DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac,
743+
DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac,
744+
DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac,
745+
=> {
746+
Duration; to_std_duration =>
747+
{f64, Strict =>}
748+
{String, Strict =>}
749+
{FORMAT, Flexible => FORMAT: Format}
750+
}
737751
);
738752

739753
use_signed_duration!(
740-
SystemTime =>
741-
TimestampSeconds DurationSeconds =>
742-
to_system_time =>
743-
{i64, Strict =>}
744-
{f64, Strict =>}
745-
{String, Strict =>}
746-
{FORMAT, Flexible => FORMAT: Format}
754+
TimestampSeconds DurationSeconds,
755+
TimestampMilliSeconds DurationMilliSeconds,
756+
TimestampMicroSeconds DurationMicroSeconds,
757+
TimestampNanoSeconds DurationNanoSeconds,
758+
=> {
759+
SystemTime; to_system_time =>
760+
{i64, Strict =>}
761+
{f64, Strict =>}
762+
{String, Strict =>}
763+
{FORMAT, Flexible => FORMAT: Format}
764+
}
747765
);
748766
use_signed_duration!(
749-
SystemTime =>
750-
TimestampSecondsWithFrac DurationSecondsWithFrac =>
751-
to_system_time =>
752-
{f64, Strict =>}
753-
{String, Strict =>}
754-
{FORMAT, Flexible => FORMAT: Format}
767+
TimestampSecondsWithFrac DurationSecondsWithFrac,
768+
TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac,
769+
TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac,
770+
TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac,
771+
=> {
772+
SystemTime; to_system_time =>
773+
{f64, Strict =>}
774+
{String, Strict =>}
775+
{FORMAT, Flexible => FORMAT: Format}
776+
}
755777
);
756778

757779
impl<'de, T, U> DeserializeAs<'de, T> for DefaultOnNull<U>

src/lib.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,60 @@ pub struct DurationSecondsWithFrac<
932932
STRICTNESS: formats::Strictness = formats::Strict,
933933
>(PhantomData<(FORMAT, STRICTNESS)>);
934934

935+
/// Equivalent to [`DurationSeconds`] with milli-seconds as base unit.
936+
///
937+
/// This type is equivalent to [`DurationSeconds`] except that the each unit represents 1 milli-second instead of 1 second for [`DurationSeconds`].
938+
#[derive(Copy, Clone, Debug, Default)]
939+
pub struct DurationMilliSeconds<
940+
FORMAT: formats::Format = u64,
941+
STRICTNESS: formats::Strictness = formats::Strict,
942+
>(PhantomData<(FORMAT, STRICTNESS)>);
943+
944+
/// Equivalent to [`DurationSecondsWithFrac`] with milli-seconds as base unit.
945+
///
946+
/// This type is equivalent to [`DurationSecondsWithFrac`] except that the each unit represents 1 milli-second instead of 1 second for [`DurationSecondsWithFrac`].
947+
#[derive(Copy, Clone, Debug, Default)]
948+
pub struct DurationMilliSecondsWithFrac<
949+
FORMAT: formats::Format = f64,
950+
STRICTNESS: formats::Strictness = formats::Strict,
951+
>(PhantomData<(FORMAT, STRICTNESS)>);
952+
953+
/// Equivalent to [`DurationSeconds`] with micro-seconds as base unit.
954+
///
955+
/// This type is equivalent to [`DurationSeconds`] except that the each unit represents 1 micro-second instead of 1 second for [`DurationSeconds`].
956+
#[derive(Copy, Clone, Debug, Default)]
957+
pub struct DurationMicroSeconds<
958+
FORMAT: formats::Format = u64,
959+
STRICTNESS: formats::Strictness = formats::Strict,
960+
>(PhantomData<(FORMAT, STRICTNESS)>);
961+
962+
/// Equivalent to [`DurationSecondsWithFrac`] with micro-seconds as base unit.
963+
///
964+
/// This type is equivalent to [`DurationSecondsWithFrac`] except that the each unit represents 1 micro-second instead of 1 second for [`DurationSecondsWithFrac`].
965+
#[derive(Copy, Clone, Debug, Default)]
966+
pub struct DurationMicroSecondsWithFrac<
967+
FORMAT: formats::Format = f64,
968+
STRICTNESS: formats::Strictness = formats::Strict,
969+
>(PhantomData<(FORMAT, STRICTNESS)>);
970+
971+
/// Equivalent to [`DurationSeconds`] with nano-seconds as base unit.
972+
///
973+
/// This type is equivalent to [`DurationSeconds`] except that the each unit represents 1 nano-second instead of 1 second for [`DurationSeconds`].
974+
#[derive(Copy, Clone, Debug, Default)]
975+
pub struct DurationNanoSeconds<
976+
FORMAT: formats::Format = u64,
977+
STRICTNESS: formats::Strictness = formats::Strict,
978+
>(PhantomData<(FORMAT, STRICTNESS)>);
979+
980+
/// Equivalent to [`DurationSecondsWithFrac`] with nano-seconds as base unit.
981+
///
982+
/// This type is equivalent to [`DurationSecondsWithFrac`] except that the each unit represents 1 nano-second instead of 1 second for [`DurationSecondsWithFrac`].
983+
#[derive(Copy, Clone, Debug, Default)]
984+
pub struct DurationNanoSecondsWithFrac<
985+
FORMAT: formats::Format = f64,
986+
STRICTNESS: formats::Strictness = formats::Strict,
987+
>(PhantomData<(FORMAT, STRICTNESS)>);
988+
935989
/// De/Serialize timestamps as seconds since the UNIX epoch
936990
///
937991
/// De/serialize timestamps as seconds since the UNIX epoch.
@@ -1203,3 +1257,57 @@ pub struct TimestampSecondsWithFrac<
12031257
FORMAT: formats::Format = f64,
12041258
STRICTNESS: formats::Strictness = formats::Strict,
12051259
>(PhantomData<(FORMAT, STRICTNESS)>);
1260+
1261+
/// Equivalent to [`TimestampSeconds`] with milli-seconds as base unit.
1262+
///
1263+
/// This type is equivalent to [`TimestampSeconds`] except that the each unit represents 1 milli-second instead of 1 second for [`TimestampSeconds`].
1264+
#[derive(Copy, Clone, Debug, Default)]
1265+
pub struct TimestampMilliSeconds<
1266+
FORMAT: formats::Format = i64,
1267+
STRICTNESS: formats::Strictness = formats::Strict,
1268+
>(PhantomData<(FORMAT, STRICTNESS)>);
1269+
1270+
/// Equivalent to [`TimestampSecondsWithFrac`] with milli-seconds as base unit.
1271+
///
1272+
/// This type is equivalent to [`TimestampSecondsWithFrac`] except that the each unit represents 1 milli-second instead of 1 second for [`TimestampSecondsWithFrac`].
1273+
#[derive(Copy, Clone, Debug, Default)]
1274+
pub struct TimestampMilliSecondsWithFrac<
1275+
FORMAT: formats::Format = f64,
1276+
STRICTNESS: formats::Strictness = formats::Strict,
1277+
>(PhantomData<(FORMAT, STRICTNESS)>);
1278+
1279+
/// Equivalent to [`TimestampSeconds`] with micro-seconds as base unit.
1280+
///
1281+
/// This type is equivalent to [`TimestampSeconds`] except that the each unit represents 1 micro-second instead of 1 second for [`TimestampSeconds`].
1282+
#[derive(Copy, Clone, Debug, Default)]
1283+
pub struct TimestampMicroSeconds<
1284+
FORMAT: formats::Format = i64,
1285+
STRICTNESS: formats::Strictness = formats::Strict,
1286+
>(PhantomData<(FORMAT, STRICTNESS)>);
1287+
1288+
/// Equivalent to [`TimestampSecondsWithFrac`] with micro-seconds as base unit.
1289+
///
1290+
/// This type is equivalent to [`TimestampSecondsWithFrac`] except that the each unit represents 1 micro-second instead of 1 second for [`TimestampSecondsWithFrac`].
1291+
#[derive(Copy, Clone, Debug, Default)]
1292+
pub struct TimestampMicroSecondsWithFrac<
1293+
FORMAT: formats::Format = f64,
1294+
STRICTNESS: formats::Strictness = formats::Strict,
1295+
>(PhantomData<(FORMAT, STRICTNESS)>);
1296+
1297+
/// Equivalent to [`TimestampSeconds`] with nano-seconds as base unit.
1298+
///
1299+
/// This type is equivalent to [`TimestampSeconds`] except that the each unit represents 1 nano-second instead of 1 second for [`TimestampSeconds`].
1300+
#[derive(Copy, Clone, Debug, Default)]
1301+
pub struct TimestampNanoSeconds<
1302+
FORMAT: formats::Format = i64,
1303+
STRICTNESS: formats::Strictness = formats::Strict,
1304+
>(PhantomData<(FORMAT, STRICTNESS)>);
1305+
1306+
/// Equivalent to [`TimestampSecondsWithFrac`] with nano-seconds as base unit.
1307+
///
1308+
/// This type is equivalent to [`TimestampSecondsWithFrac`] except that the each unit represents 1 nano-second instead of 1 second for [`TimestampSecondsWithFrac`].
1309+
#[derive(Copy, Clone, Debug, Default)]
1310+
pub struct TimestampNanoSecondsWithFrac<
1311+
FORMAT: formats::Format = f64,
1312+
STRICTNESS: formats::Strictness = formats::Strict,
1313+
>(PhantomData<(FORMAT, STRICTNESS)>);

src/ser/impls.rs

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,14 @@ where
328328

329329
macro_rules! use_signed_duration {
330330
(
331-
$ty:ty =>
332331
$main_trait:ident $internal_trait:ident =>
333-
$converter:ident =>
334-
$({
335-
$format:ty, $strictness:ty =>
336-
$($tbound:ident: $bound:ident)*
337-
})*
332+
{
333+
$ty:ty; $converter:ident =>
334+
$({
335+
$format:ty, $strictness:ty =>
336+
$($tbound:ident: $bound:ident)*
337+
})*
338+
}
338339
) => {
339340
$(
340341
impl<$($tbound,)*> SerializeAs<$ty> for $main_trait<$format, $strictness>
@@ -353,38 +354,59 @@ macro_rules! use_signed_duration {
353354
}
354355
)*
355356
};
357+
(
358+
$( $main_trait:ident $internal_trait:ident, )+ => $rest:tt
359+
) => {
360+
$( use_signed_duration!($main_trait $internal_trait => $rest); )+
361+
};
356362
}
357363

358364
use_signed_duration!(
359-
Duration =>
360-
DurationSeconds DurationSeconds =>
361-
to_std_duration =>
362-
{u64, STRICTNESS => STRICTNESS: Strictness}
363-
{f64, STRICTNESS => STRICTNESS: Strictness}
364-
{String, STRICTNESS => STRICTNESS: Strictness}
365+
DurationSeconds DurationSeconds,
366+
DurationMilliSeconds DurationMilliSeconds,
367+
DurationMicroSeconds DurationMicroSeconds,
368+
DurationNanoSeconds DurationNanoSeconds,
369+
=> {
370+
Duration; to_std_duration =>
371+
{u64, STRICTNESS => STRICTNESS: Strictness}
372+
{f64, STRICTNESS => STRICTNESS: Strictness}
373+
{String, STRICTNESS => STRICTNESS: Strictness}
374+
}
365375
);
366376
use_signed_duration!(
367-
Duration =>
368-
DurationSecondsWithFrac DurationSecondsWithFrac =>
369-
to_std_duration =>
370-
{f64, STRICTNESS => STRICTNESS: Strictness}
371-
{String, STRICTNESS => STRICTNESS: Strictness}
377+
DurationSecondsWithFrac DurationSecondsWithFrac,
378+
DurationMilliSecondsWithFrac DurationMilliSecondsWithFrac,
379+
DurationMicroSecondsWithFrac DurationMicroSecondsWithFrac,
380+
DurationNanoSecondsWithFrac DurationNanoSecondsWithFrac,
381+
=> {
382+
Duration; to_std_duration =>
383+
{f64, STRICTNESS => STRICTNESS: Strictness}
384+
{String, STRICTNESS => STRICTNESS: Strictness}
385+
}
372386
);
373387

374388
use_signed_duration!(
375-
SystemTime =>
376-
TimestampSeconds DurationSeconds =>
377-
to_system_time =>
378-
{i64, STRICTNESS => STRICTNESS: Strictness}
379-
{f64, STRICTNESS => STRICTNESS: Strictness}
380-
{String, STRICTNESS => STRICTNESS: Strictness}
389+
TimestampSeconds DurationSeconds,
390+
TimestampMilliSeconds DurationMilliSeconds,
391+
TimestampMicroSeconds DurationMicroSeconds,
392+
TimestampNanoSeconds DurationNanoSeconds,
393+
=> {
394+
SystemTime; to_system_time =>
395+
{i64, STRICTNESS => STRICTNESS: Strictness}
396+
{f64, STRICTNESS => STRICTNESS: Strictness}
397+
{String, STRICTNESS => STRICTNESS: Strictness}
398+
}
381399
);
382400
use_signed_duration!(
383-
SystemTime =>
384-
TimestampSecondsWithFrac DurationSecondsWithFrac =>
385-
to_system_time =>
386-
{f64, STRICTNESS => STRICTNESS: Strictness}
387-
{String, STRICTNESS => STRICTNESS: Strictness}
401+
TimestampSecondsWithFrac DurationSecondsWithFrac,
402+
TimestampMilliSecondsWithFrac DurationMilliSecondsWithFrac,
403+
TimestampMicroSecondsWithFrac DurationMicroSecondsWithFrac,
404+
TimestampNanoSecondsWithFrac DurationNanoSecondsWithFrac,
405+
=> {
406+
SystemTime; to_system_time =>
407+
{f64, STRICTNESS => STRICTNESS: Strictness}
408+
{String, STRICTNESS => STRICTNESS: Strictness}
409+
}
388410
);
389411

390412
impl<T, U> SerializeAs<T> for DefaultOnNull<U>

0 commit comments

Comments
 (0)