/rust/registry/src/index.crates.io-6f17d22bba15001f/time-0.3.37/src/parsing/shim.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Extension traits for things either not implemented or not yet stable in the MSRV. |
2 | | |
3 | | /// Equivalent of `foo.parse()` for slices. |
4 | | pub(crate) trait IntegerParseBytes<T> { |
5 | | #[allow(clippy::missing_docs_in_private_items)] |
6 | | fn parse_bytes(&self) -> Option<T>; |
7 | | } |
8 | | |
9 | | impl<T: Integer> IntegerParseBytes<T> for [u8] { |
10 | 0 | fn parse_bytes(&self) -> Option<T> { |
11 | 0 | T::parse_bytes(self) |
12 | 0 | } Unexecuted instantiation: <[u8] as time::parsing::shim::IntegerParseBytes<core::num::nonzero::NonZero<u8>>>::parse_bytes Unexecuted instantiation: <[u8] as time::parsing::shim::IntegerParseBytes<core::num::nonzero::NonZero<u16>>>::parse_bytes Unexecuted instantiation: <[u8] as time::parsing::shim::IntegerParseBytes<u8>>::parse_bytes Unexecuted instantiation: <[u8] as time::parsing::shim::IntegerParseBytes<u32>>::parse_bytes Unexecuted instantiation: <[u8] as time::parsing::shim::IntegerParseBytes<u128>>::parse_bytes Unexecuted instantiation: <[u8] as time::parsing::shim::IntegerParseBytes<u16>>::parse_bytes |
13 | | } |
14 | | |
15 | | /// Marker trait for all integer types, including `NonZero*` |
16 | | pub(crate) trait Integer: Sized { |
17 | | #[allow(clippy::missing_docs_in_private_items)] |
18 | | fn parse_bytes(src: &[u8]) -> Option<Self>; |
19 | | } |
20 | | |
21 | | /// Parse the given types from bytes. |
22 | | macro_rules! impl_parse_bytes { |
23 | | ($($t:ty)*) => ($( |
24 | | impl Integer for $t { |
25 | | #[allow(trivial_numeric_casts)] |
26 | 0 | fn parse_bytes(src: &[u8]) -> Option<Self> { |
27 | 0 | src.iter().try_fold::<Self, _, _>(0, |result, c| { |
28 | 0 | result.checked_mul(10)?.checked_add((c - b'0') as Self) |
29 | 0 | }) Unexecuted instantiation: <u8 as time::parsing::shim::Integer>::parse_bytes::{closure#0} Unexecuted instantiation: <u16 as time::parsing::shim::Integer>::parse_bytes::{closure#0} Unexecuted instantiation: <u32 as time::parsing::shim::Integer>::parse_bytes::{closure#0} Unexecuted instantiation: <u128 as time::parsing::shim::Integer>::parse_bytes::{closure#0} |
30 | 0 | } Unexecuted instantiation: <u8 as time::parsing::shim::Integer>::parse_bytes Unexecuted instantiation: <u16 as time::parsing::shim::Integer>::parse_bytes Unexecuted instantiation: <u32 as time::parsing::shim::Integer>::parse_bytes Unexecuted instantiation: <u128 as time::parsing::shim::Integer>::parse_bytes |
31 | | } |
32 | | )*) |
33 | | } |
34 | | impl_parse_bytes! { u8 u16 u32 u128 } |
35 | | |
36 | | /// Parse the given types from bytes. |
37 | | macro_rules! impl_parse_bytes_nonzero { |
38 | | ($($t:ty)*) => {$( |
39 | | impl Integer for $t { |
40 | 0 | fn parse_bytes(src: &[u8]) -> Option<Self> { |
41 | 0 | Self::new(src.parse_bytes()?) |
42 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as time::parsing::shim::Integer>::parse_bytes Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as time::parsing::shim::Integer>::parse_bytes |
43 | | } |
44 | | )*} |
45 | | } |
46 | | |
47 | | impl_parse_bytes_nonzero! { |
48 | | core::num::NonZeroU8 |
49 | | core::num::NonZeroU16 |
50 | | } |