aboutsummaryrefslogtreecommitdiff
path: root/vendor/qoi/src/utils.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/qoi/src/utils.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/qoi/src/utils.rs')
-rw-r--r--vendor/qoi/src/utils.rs107
1 files changed, 107 insertions, 0 deletions
diff --git a/vendor/qoi/src/utils.rs b/vendor/qoi/src/utils.rs
new file mode 100644
index 0000000..d0c37a6
--- /dev/null
+++ b/vendor/qoi/src/utils.rs
@@ -0,0 +1,107 @@
+#[cfg(feature = "std")]
+use std::io::Write;
+
+use crate::error::Result;
+
+#[inline(always)]
+#[cold]
+pub const fn cold() {}
+
+#[inline(always)]
+#[allow(unused)]
+pub const fn likely(b: bool) -> bool {
+ if !b {
+ cold();
+ }
+ b
+}
+
+#[inline(always)]
+pub const fn unlikely(b: bool) -> bool {
+ if b {
+ cold();
+ }
+ b
+}
+
+pub trait Writer: Sized {
+ fn write_one(self, v: u8) -> Result<Self>;
+ fn write_many(self, v: &[u8]) -> Result<Self>;
+ fn capacity(&self) -> usize;
+}
+
+pub struct BytesMut<'a>(&'a mut [u8]);
+
+impl<'a> BytesMut<'a> {
+ pub fn new(buf: &'a mut [u8]) -> Self {
+ Self(buf)
+ }
+
+ #[inline]
+ pub fn write_one(self, v: u8) -> Self {
+ if let Some((first, tail)) = self.0.split_first_mut() {
+ *first = v;
+ Self(tail)
+ } else {
+ unreachable!()
+ }
+ }
+
+ #[inline]
+ pub fn write_many(self, v: &[u8]) -> Self {
+ if v.len() <= self.0.len() {
+ let (head, tail) = self.0.split_at_mut(v.len());
+ head.copy_from_slice(v);
+ Self(tail)
+ } else {
+ unreachable!()
+ }
+ }
+}
+
+impl<'a> Writer for BytesMut<'a> {
+ #[inline]
+ fn write_one(self, v: u8) -> Result<Self> {
+ Ok(BytesMut::write_one(self, v))
+ }
+
+ #[inline]
+ fn write_many(self, v: &[u8]) -> Result<Self> {
+ Ok(BytesMut::write_many(self, v))
+ }
+
+ #[inline]
+ fn capacity(&self) -> usize {
+ self.0.len()
+ }
+}
+
+#[cfg(feature = "std")]
+pub struct GenericWriter<W> {
+ writer: W,
+ n_written: usize,
+}
+
+#[cfg(feature = "std")]
+impl<W: Write> GenericWriter<W> {
+ pub const fn new(writer: W) -> Self {
+ Self { writer, n_written: 0 }
+ }
+}
+
+#[cfg(feature = "std")]
+impl<W: Write> Writer for GenericWriter<W> {
+ fn write_one(mut self, v: u8) -> Result<Self> {
+ self.n_written += 1;
+ self.writer.write_all(&[v]).map(|_| self).map_err(Into::into)
+ }
+
+ fn write_many(mut self, v: &[u8]) -> Result<Self> {
+ self.n_written += v.len();
+ self.writer.write_all(v).map(|_| self).map_err(Into::into)
+ }
+
+ fn capacity(&self) -> usize {
+ usize::MAX - self.n_written
+ }
+}