kib/
bytes.rs

1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4KiB
5
6Copyright (C) 2016-2017, 2019-2020, 2022, 2024  Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2016-2017".
10
11This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
24::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
25*/
26
27//! # Bytes
28
29use {
30    core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize},
31    self::inner::Inner,
32};
33
34mod inner;
35
36/// # Bytes
37///
38/// This struct is just a simple container for some primitive numbers. It is used in [`fmt()`][fn:fmt], [`fmt_as_parts()`][fn:fmt_as_parts]...
39///
40/// [fn:fmt]: fn.fmt.html
41/// [fn:fmt_as_parts]: fn.fmt_as_parts.html
42#[derive(Debug)]
43pub struct Bytes {
44    inner: Inner,
45}
46
47impl Bytes {
48
49    /// # Casts to `f64`
50    pub (crate) const fn as_f64_lossy(&self) -> f64 {
51        match self.inner {
52            Inner::USize(n) => n as f64,
53            Inner::U64(n) => n as f64,
54            Inner::U128(n) => n as f64,
55        }
56    }
57
58    /// # Casts to `u64`
59    pub (crate) fn as_u64(&self) -> Option<u64> {
60        match self.inner {
61            Inner::USize(n) => n.try_into().ok(),
62            Inner::U64(n) => Some(n),
63            Inner::U128(n) => n.try_into().ok(),
64        }
65    }
66
67}
68
69impl From<usize> for Bytes {
70
71    fn from(n: usize) -> Self {
72        Self {
73            inner: Inner::USize(n),
74        }
75    }
76
77}
78
79impl From<u128> for Bytes {
80
81    fn from(n: u128) -> Self {
82        Self {
83            inner: Inner::U128(n),
84        }
85    }
86
87}
88
89macro_rules! impl_from_ux_for_bytes { ($($ty: ty),+$(,)?) => {
90    $(
91        impl From<$ty> for Bytes {
92
93            fn from(n: $ty) -> Self {
94                Self {
95                    inner: Inner::U64(n.into()),
96                }
97            }
98
99        }
100    )+
101}}
102
103impl_from_ux_for_bytes!(u8, u16, u32, u64);
104
105macro_rules! impl_from_non_zero_ux_for_bytes { ($($ty: ty),+$(,)?) => {
106    $(
107        impl From<$ty> for Bytes {
108
109            fn from(n: $ty) -> Self {
110                Self::from(n.get())
111            }
112
113        }
114    )+
115}}
116
117impl_from_non_zero_ux_for_bytes!(NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize);