use crate::*;
#[cfg(feature = "backend-go-pion")]
pub(crate) mod imp {
mod imp_go_pion;
pub use imp_go_pion::*;
}
pub struct BufWriter {
imp: imp::ImpWriter,
_not_sync: std::marker::PhantomData<std::cell::Cell<()>>,
}
impl BufWriter {
#[inline]
pub fn new() -> Result<Self> {
Ok(Self {
imp: imp::ImpWriter::new()?,
_not_sync: std::marker::PhantomData,
})
}
#[inline]
pub fn finish(self) -> Buf {
Buf {
imp: self.imp.finish(),
_not_sync: std::marker::PhantomData,
}
}
}
impl std::io::Write for BufWriter {
#[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.imp.write(buf)
}
#[inline]
fn write_vectored(
&mut self,
bufs: &[std::io::IoSlice<'_>],
) -> std::io::Result<usize> {
self.imp.write_vectored(bufs)
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
self.imp.write_all(buf)
}
#[inline]
fn flush(&mut self) -> std::io::Result<()> {
self.imp.flush()
}
}
#[allow(clippy::len_without_is_empty)]
pub struct Buf {
pub(crate) imp: imp::Imp,
pub(crate) _not_sync: std::marker::PhantomData<std::cell::Cell<()>>,
}
impl std::fmt::Debug for Buf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.imp.fmt(f)
}
}
impl Buf {
pub(crate) fn from_raw(buf: tx5_go_pion::GoBuf) -> Self {
Self {
imp: imp::Imp::from_raw(buf),
_not_sync: std::marker::PhantomData,
}
}
#[inline]
pub fn from_slice<S: AsRef<[u8]>>(slice: S) -> Result<Self> {
Ok(Self {
imp: imp::Imp::from_slice(slice)?,
_not_sync: std::marker::PhantomData,
})
}
#[inline]
pub fn from_writer() -> Result<BufWriter> {
BufWriter::new()
}
#[inline]
pub fn from_json<S: serde::Serialize>(s: S) -> Result<Self> {
Ok(Self {
imp: imp::Imp::from_json(s)?,
_not_sync: std::marker::PhantomData,
})
}
#[inline]
pub fn len(&mut self) -> Result<usize> {
self.imp.len()
}
#[inline]
pub fn try_clone(&mut self) -> Result<Self> {
Ok(Self {
imp: self.imp.try_clone()?,
_not_sync: std::marker::PhantomData,
})
}
#[inline]
pub fn to_vec(&mut self) -> Result<Vec<u8>> {
self.imp.to_vec()
}
#[inline]
pub fn to_json<D>(&mut self) -> Result<D>
where
D: serde::de::DeserializeOwned + Sized,
{
self.imp.to_json()
}
}
impl std::io::Read for Buf {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.imp.read(buf)
}
}
pub enum BufRef<'lt> {
Owned(Result<Buf>),
Borrowed(Result<&'lt mut Buf>),
}
impl<'lt> BufRef<'lt> {
pub fn as_mut_ref(&'lt mut self) -> Result<&'lt mut Buf> {
match self {
BufRef::Owned(o) => match o {
Ok(o) => Ok(o),
Err(e) => Err(e.err_clone()),
},
BufRef::Borrowed(b) => match b {
Ok(b) => Ok(b),
Err(e) => Err(e.err_clone()),
},
}
}
}
impl From<Buf> for BufRef<'static> {
fn from(b: Buf) -> Self {
Self::Owned(Ok(b))
}
}
impl<'lt> From<&'lt mut Buf> for BufRef<'lt> {
fn from(b: &'lt mut Buf) -> Self {
Self::Borrowed(Ok(b))
}
}
impl<S: serde::Serialize> From<S> for BufRef<'static> {
fn from(s: S) -> Self {
Self::Owned(Buf::from_json(s))
}
}