zeros/
bytes.rs

1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4Zeros
5
6Copyright (C) 2019-2025  Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2019-2025".
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 self::inner::Inner;
30
31mod inner;
32
33/// # No bytes
34pub const NO_BYTES: Option<&[u8]> = None;
35
36/// # Bytes
37///
38/// This struct can be converted from:
39///
40/// - A single slice of bytes.
41/// - A slice of slices of bytes.
42/// - An array of slices of bytes.
43///
44/// You rarely use this struct directly. Some functions in this project look like below example:
45///
46/// ```
47/// use zeros::Bytes;
48///
49/// fn some<'a, const N: usize, B, B0>(bytes: B)
50/// where B: Into<Bytes<'a, N, B0>>, B0: AsRef<[u8]> + 'a {
51/// }
52///
53/// some("data");
54/// some(["test", "data"]);
55/// some([0, 1, 2]);
56/// some(&[[3, 4, 5], [6, 7, 8]][..]);
57/// ```
58///
59/// So it's just a convenient way for you to provide your data to those functions.
60#[derive(Debug)]
61pub struct Bytes<'a, const N: usize, B> where B: AsRef<[u8]> {
62    inner: Inner<'a, N, B>,
63}
64
65impl<const N: usize, B> Bytes<'_, N, B> where B: AsRef<[u8]> {
66
67    /// # Access to bytes-slice
68    pub (crate) fn as_slice(&self) -> &[B] {
69        match &self.inner {
70            Inner::Slice(slice) => &slice,
71            Inner::Array(array) => &array[..],
72        }
73    }
74
75}
76
77impl<'a, B> From<&'a [B]> for Bytes<'a, {usize::MIN}, B> where B: AsRef<[u8]> {
78
79    fn from(bytes: &'a [B]) -> Self {
80        Self {
81            inner: Inner::Slice(bytes),
82        }
83    }
84
85}
86
87impl<B> From<B> for Bytes<'_, 1, B> where B: AsRef<[u8]> {
88
89    fn from(bytes: B) -> Self {
90        Self {
91            inner: Inner::Array([bytes]),
92        }
93    }
94
95}
96
97impl<const N: usize, B> From<[B; N]> for Bytes<'_, N, B> where B: AsRef<[u8]> {
98
99    fn from(bytes: [B; N]) -> Self {
100        Self {
101            inner: Inner::Array(bytes),
102        }
103    }
104
105}