2022-01-03 14:14:01 +00:00
|
|
|
#[cfg(feature = "std")]
|
2022-01-02 22:11:01 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
use crate::error::Result;
|
|
|
|
|
2021-11-30 14:30:13 +00:00
|
|
|
#[inline(always)]
|
2021-12-30 10:21:54 +00:00
|
|
|
#[cold]
|
|
|
|
pub const fn cold() {}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2021-12-30 22:44:59 +00:00
|
|
|
#[allow(unused)]
|
2021-11-30 14:30:13 +00:00
|
|
|
pub const fn likely(b: bool) -> bool {
|
2021-12-30 10:21:54 +00:00
|
|
|
if !b {
|
2021-12-31 10:37:56 +00:00
|
|
|
cold();
|
2021-11-30 14:30:13 +00:00
|
|
|
}
|
2021-12-30 10:21:54 +00:00
|
|
|
b
|
2021-11-30 14:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub const fn unlikely(b: bool) -> bool {
|
2021-12-30 10:21:54 +00:00
|
|
|
if b {
|
2021-12-31 10:37:56 +00:00
|
|
|
cold();
|
2021-11-30 14:30:13 +00:00
|
|
|
}
|
2021-12-30 10:21:54 +00:00
|
|
|
b
|
2021-11-30 14:30:13 +00:00
|
|
|
}
|
2022-01-02 17:16:05 +00:00
|
|
|
|
2022-01-02 22:11:01 +00:00
|
|
|
pub trait Writer: Sized {
|
|
|
|
fn write_one(self, v: u8) -> Result<Self>;
|
|
|
|
fn write_many(self, v: &[u8]) -> Result<Self>;
|
|
|
|
fn capacity(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
pub struct BytesMut<'a>(&'a mut [u8]);
|
2022-01-02 22:11:01 +00:00
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
impl<'a> BytesMut<'a> {
|
|
|
|
pub fn new(buf: &'a mut [u8]) -> Self {
|
|
|
|
Self(buf)
|
2022-01-02 22:11:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
#[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!()
|
|
|
|
}
|
2022-01-02 22:11:01 +00:00
|
|
|
}
|
2022-01-02 17:16:05 +00:00
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
#[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!()
|
|
|
|
}
|
2022-01-02 22:11:01 +00:00
|
|
|
}
|
2022-01-02 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Writer for BytesMut<'a> {
|
2022-01-02 22:11:01 +00:00
|
|
|
#[inline]
|
|
|
|
fn write_one(self, v: u8) -> Result<Self> {
|
|
|
|
Ok(BytesMut::write_one(self, v))
|
|
|
|
}
|
2022-01-02 17:16:05 +00:00
|
|
|
|
|
|
|
#[inline]
|
2022-01-02 22:11:01 +00:00
|
|
|
fn write_many(self, v: &[u8]) -> Result<Self> {
|
|
|
|
Ok(BytesMut::write_many(self, v))
|
2022-01-02 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2022-01-02 22:11:01 +00:00
|
|
|
fn capacity(&self) -> usize {
|
|
|
|
self.0.len()
|
2022-01-02 17:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub struct GenericWriter<W> {
|
|
|
|
writer: W,
|
|
|
|
n_written: usize,
|
|
|
|
}
|
2022-01-02 17:16:05 +00:00
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl<W: Write> GenericWriter<W> {
|
|
|
|
pub fn new(writer: W) -> Self {
|
|
|
|
Self { writer, n_written: 0 }
|
2022-01-02 17:16:05 +00:00
|
|
|
}
|
2022-01-03 21:24:11 +00:00
|
|
|
}
|
2022-01-02 17:16:05 +00:00
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
#[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)
|
2022-01-02 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 21:24:11 +00:00
|
|
|
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
|
2022-01-02 17:16:05 +00:00
|
|
|
}
|
|
|
|
}
|