/rust/registry/src/index.crates.io-1949cf8c6b5b557f/qoi-0.4.1/src/utils.rs
Line | Count | Source |
1 | | #[cfg(feature = "std")] |
2 | | use std::io::Write; |
3 | | |
4 | | use crate::error::Result; |
5 | | |
6 | | #[inline(always)] |
7 | | #[cold] |
8 | 0 | pub const fn cold() {} |
9 | | |
10 | | #[inline(always)] |
11 | | #[allow(unused)] |
12 | 0 | pub const fn likely(b: bool) -> bool { |
13 | 0 | if !b { |
14 | 0 | cold(); |
15 | 0 | } |
16 | 0 | b |
17 | 0 | } |
18 | | |
19 | | #[inline(always)] |
20 | 0 | pub const fn unlikely(b: bool) -> bool { |
21 | 0 | if b { |
22 | 0 | cold(); |
23 | 0 | } |
24 | 0 | b |
25 | 0 | } |
26 | | |
27 | | pub trait Writer: Sized { |
28 | | fn write_one(self, v: u8) -> Result<Self>; |
29 | | fn write_many(self, v: &[u8]) -> Result<Self>; |
30 | | fn capacity(&self) -> usize; |
31 | | } |
32 | | |
33 | | pub struct BytesMut<'a>(&'a mut [u8]); |
34 | | |
35 | | impl<'a> BytesMut<'a> { |
36 | 0 | pub fn new(buf: &'a mut [u8]) -> Self { |
37 | 0 | Self(buf) |
38 | 0 | } |
39 | | |
40 | | #[inline] |
41 | 0 | pub fn write_one(self, v: u8) -> Self { |
42 | 0 | if let Some((first, tail)) = self.0.split_first_mut() { |
43 | 0 | *first = v; |
44 | 0 | Self(tail) |
45 | | } else { |
46 | 0 | unreachable!() |
47 | | } |
48 | 0 | } Unexecuted instantiation: <qoi::utils::BytesMut>::write_one Unexecuted instantiation: <qoi::utils::BytesMut>::write_one |
49 | | |
50 | | #[inline] |
51 | 0 | pub fn write_many(self, v: &[u8]) -> Self { |
52 | 0 | if v.len() <= self.0.len() { |
53 | 0 | let (head, tail) = self.0.split_at_mut(v.len()); |
54 | 0 | head.copy_from_slice(v); |
55 | 0 | Self(tail) |
56 | | } else { |
57 | 0 | unreachable!() |
58 | | } |
59 | 0 | } Unexecuted instantiation: <qoi::utils::BytesMut>::write_many Unexecuted instantiation: <qoi::utils::BytesMut>::write_many |
60 | | } |
61 | | |
62 | | impl<'a> Writer for BytesMut<'a> { |
63 | | #[inline] |
64 | 0 | fn write_one(self, v: u8) -> Result<Self> { |
65 | 0 | Ok(BytesMut::write_one(self, v)) |
66 | 0 | } Unexecuted instantiation: <qoi::utils::BytesMut as qoi::utils::Writer>::write_one Unexecuted instantiation: <qoi::utils::BytesMut as qoi::utils::Writer>::write_one |
67 | | |
68 | | #[inline] |
69 | 0 | fn write_many(self, v: &[u8]) -> Result<Self> { |
70 | 0 | Ok(BytesMut::write_many(self, v)) |
71 | 0 | } Unexecuted instantiation: <qoi::utils::BytesMut as qoi::utils::Writer>::write_many Unexecuted instantiation: <qoi::utils::BytesMut as qoi::utils::Writer>::write_many |
72 | | |
73 | | #[inline] |
74 | 0 | fn capacity(&self) -> usize { |
75 | 0 | self.0.len() |
76 | 0 | } Unexecuted instantiation: <qoi::utils::BytesMut as qoi::utils::Writer>::capacity Unexecuted instantiation: <qoi::utils::BytesMut as qoi::utils::Writer>::capacity |
77 | | } |
78 | | |
79 | | #[cfg(feature = "std")] |
80 | | pub struct GenericWriter<W> { |
81 | | writer: W, |
82 | | n_written: usize, |
83 | | } |
84 | | |
85 | | #[cfg(feature = "std")] |
86 | | impl<W: Write> GenericWriter<W> { |
87 | 0 | pub const fn new(writer: W) -> Self { |
88 | 0 | Self { writer, n_written: 0 } |
89 | 0 | } |
90 | | } |
91 | | |
92 | | #[cfg(feature = "std")] |
93 | | impl<W: Write> Writer for GenericWriter<W> { |
94 | 0 | fn write_one(mut self, v: u8) -> Result<Self> { |
95 | 0 | self.n_written += 1; |
96 | 0 | self.writer.write_all(&[v]).map(|_| self).map_err(Into::into) |
97 | 0 | } |
98 | | |
99 | 0 | fn write_many(mut self, v: &[u8]) -> Result<Self> { |
100 | 0 | self.n_written += v.len(); |
101 | 0 | self.writer.write_all(v).map(|_| self).map_err(Into::into) |
102 | 0 | } |
103 | | |
104 | 0 | fn capacity(&self) -> usize { |
105 | 0 | usize::MAX - self.n_written |
106 | 0 | } |
107 | | } |