|
1 |
| -use crate::coder::{Buffer, Decoder, Encoder, Result, View}; |
| 1 | +use crate::convert::ConvertFrom; |
2 | 2 | use crate::datetime::{Hour, Minute, Nanoseconds, Second};
|
3 |
| -use crate::{Decode, Encode}; |
4 |
| -use alloc::vec::Vec; |
5 |
| -use core::num::NonZeroUsize; |
6 | 3 | use time::Time;
|
7 | 4 |
|
8 |
| -#[derive(Default)] |
9 |
| -pub struct TimeEncoder { |
10 |
| - hour: <u8 as Encode>::Encoder, |
11 |
| - minute: <u8 as Encode>::Encoder, |
12 |
| - second: <u8 as Encode>::Encoder, |
13 |
| - nanosecond: <u32 as Encode>::Encoder, |
14 |
| -} |
15 |
| -impl Encoder<Time> for TimeEncoder { |
16 |
| - #[inline(always)] |
17 |
| - fn encode(&mut self, t: &Time) { |
18 |
| - let (hour, minute, second, nanosecond) = t.as_hms_nano(); |
19 |
| - self.hour.encode(&hour); |
20 |
| - self.minute.encode(&minute); |
21 |
| - self.second.encode(&second); |
22 |
| - self.nanosecond.encode(&nanosecond); |
23 |
| - } |
24 |
| -} |
25 |
| -impl Buffer for TimeEncoder { |
26 |
| - fn collect_into(&mut self, out: &mut Vec<u8>) { |
27 |
| - self.hour.collect_into(out); |
28 |
| - self.minute.collect_into(out); |
29 |
| - self.second.collect_into(out); |
30 |
| - self.nanosecond.collect_into(out); |
31 |
| - } |
32 |
| - |
33 |
| - fn reserve(&mut self, additional: NonZeroUsize) { |
34 |
| - self.hour.reserve(additional); |
35 |
| - self.minute.reserve(additional); |
36 |
| - self.second.reserve(additional); |
37 |
| - self.nanosecond.reserve(additional); |
| 5 | +impl ConvertFrom<&Time> for (Hour, Minute, Second, Nanoseconds) { |
| 6 | + fn convert_from(value: &Time) -> Self { |
| 7 | + let (hour, minute, second, nanosecond) = value.as_hms_nano(); |
| 8 | + ( |
| 9 | + Hour(hour), |
| 10 | + Minute(minute), |
| 11 | + Second(second), |
| 12 | + Nanoseconds(nanosecond), |
| 13 | + ) |
38 | 14 | }
|
39 | 15 | }
|
40 |
| -impl Encode for Time { |
41 |
| - type Encoder = TimeEncoder; |
42 |
| -} |
43 | 16 |
|
44 |
| -#[derive(Default)] |
45 |
| -pub struct TimeDecoder<'a> { |
46 |
| - hour: <Hour as Decode<'a>>::Decoder, |
47 |
| - minute: <Minute as Decode<'a>>::Decoder, |
48 |
| - second: <Second as Decode<'a>>::Decoder, |
49 |
| - nanosecond: <Nanoseconds as Decode<'a>>::Decoder, |
50 |
| -} |
51 |
| -impl<'a> View<'a> for TimeDecoder<'a> { |
52 |
| - fn populate(&mut self, input: &mut &'a [u8], length: usize) -> Result<()> { |
53 |
| - self.hour.populate(input, length)?; |
54 |
| - self.minute.populate(input, length)?; |
55 |
| - self.second.populate(input, length)?; |
56 |
| - self.nanosecond.populate(input, length)?; |
57 |
| - Ok(()) |
58 |
| - } |
59 |
| -} |
60 |
| -impl<'a> Decoder<'a, Time> for TimeDecoder<'a> { |
61 |
| - #[inline(always)] |
62 |
| - fn decode(&mut self) -> Time { |
63 |
| - let Hour(hour) = self.hour.decode(); |
64 |
| - let Minute(minute) = self.minute.decode(); |
65 |
| - let Second(second) = self.second.decode(); |
66 |
| - let Nanoseconds(nanosecond) = self.nanosecond.decode(); |
| 17 | +impl ConvertFrom<(Hour, Minute, Second, Nanoseconds)> for Time { |
| 18 | + fn convert_from(value: (Hour, Minute, Second, Nanoseconds)) -> Self { |
| 19 | + let (Hour(hour), Minute(minute), Second(second), Nanoseconds(nanosecond)) = value; |
67 | 20 | // Safety: should not fail because all input values are validated with CheckedBitPattern.
|
68 | 21 | unsafe { Time::from_hms_nano(hour, minute, second, nanosecond).unwrap_unchecked() }
|
69 | 22 | }
|
70 | 23 | }
|
71 |
| -impl<'a> Decode<'a> for Time { |
72 |
| - type Decoder = TimeDecoder<'a>; |
73 |
| -} |
74 | 24 |
|
75 | 25 | #[cfg(test)]
|
76 | 26 | mod tests {
|
77 | 27 | #[test]
|
78 | 28 | fn test() {
|
79 |
| - assert!(crate::decode::<Time>(&crate::encode(&(23, 59, 59, 999_999_999))).is_ok()); |
| 29 | + assert!(crate::decode::<Time>(&crate::encode( |
| 30 | + &Time::from_hms_nano(23, 59, 59, 999_999_999).unwrap() |
| 31 | + )) |
| 32 | + .is_ok()); |
80 | 33 | assert!(crate::decode::<Time>(&crate::encode(&(24, 59, 59, 999_999_999))).is_err());
|
81 | 34 | assert!(crate::decode::<Time>(&crate::encode(&(23, 60, 59, 999_999_999))).is_err());
|
82 | 35 | assert!(crate::decode::<Time>(&crate::encode(&(23, 59, 60, 999_999_999))).is_err());
|
|
0 commit comments