2018-05-14 18:02:39 +08:00
|
|
|
#![no_std]
|
|
|
|
#![feature(never_type)]
|
|
|
|
#![cfg_attr(feature = "alloc", feature(alloc))]
|
|
|
|
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate alloc;
|
|
|
|
#[cfg(feature = "byteorder")]
|
|
|
|
extern crate byteorder;
|
|
|
|
|
|
|
|
use core::result;
|
|
|
|
use core::fmt;
|
|
|
|
|
|
|
|
#[cfg(feature = "byteorder")]
|
|
|
|
pub mod proto;
|
|
|
|
|
|
|
|
pub type Result<T, E> = result::Result<T, Error<E>>;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum Error<T> {
|
|
|
|
UnexpectedEof,
|
2018-05-14 20:06:33 +08:00
|
|
|
Unrecognized,
|
2018-05-14 18:02:39 +08:00
|
|
|
Other(T)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: fmt::Display> fmt::Display for Error<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
&Error::UnexpectedEof =>
|
|
|
|
write!(f, "unexpected end of stream"),
|
2018-05-14 20:06:33 +08:00
|
|
|
&Error::Unrecognized =>
|
|
|
|
write!(f, "unrecognized input"),
|
2018-05-14 18:02:39 +08:00
|
|
|
&Error::Other(ref err) =>
|
|
|
|
write!(f, "{}", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<T> for Error<T> {
|
|
|
|
fn from(value: T) -> Error<T> {
|
|
|
|
Error::Other(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Read {
|
|
|
|
type ReadError;
|
|
|
|
|
|
|
|
/// Pull some bytes from this source into the specified buffer, returning
|
|
|
|
/// how many bytes were read.
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> result::Result<usize, Self::ReadError>;
|
|
|
|
|
|
|
|
/// Read the exact number of bytes required to fill `buf`.
|
|
|
|
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), Self::ReadError> {
|
|
|
|
while !buf.is_empty() {
|
|
|
|
let read_bytes = self.read(buf)?;
|
|
|
|
if read_bytes == 0 {
|
|
|
|
return Err(Error::UnexpectedEof)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = &mut { buf }[read_bytes..];
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 03:06:54 +08:00
|
|
|
impl<'a, T: Read> Read for &'a mut T {
|
|
|
|
type ReadError = T::ReadError;
|
|
|
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> result::Result<usize, Self::ReadError> {
|
|
|
|
T::read(self, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:02:39 +08:00
|
|
|
pub trait Write {
|
|
|
|
type WriteError;
|
|
|
|
type FlushError;
|
|
|
|
|
|
|
|
/// Write a buffer into this object, returning how many bytes were written.
|
|
|
|
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError>;
|
|
|
|
|
|
|
|
/// Flush this output stream, ensuring that all intermediately buffered contents
|
|
|
|
/// reach their destination.
|
|
|
|
fn flush(&mut self) -> result::Result<(), Self::FlushError>;
|
|
|
|
|
|
|
|
/// Attempts to write an entire buffer into `self`.
|
|
|
|
fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Self::WriteError> {
|
|
|
|
while buf.len() > 0 {
|
|
|
|
let written_bytes = self.write(buf)?;
|
|
|
|
if written_bytes == 0 {
|
|
|
|
return Err(Error::UnexpectedEof)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = &buf[written_bytes..];
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hints the writer how much bytes will be written after call to this function.
|
|
|
|
///
|
|
|
|
/// At least `min` bytes should be written after the call to this function and
|
|
|
|
/// if `max` is `Some(x)` than at most `x` bytes should be written.
|
|
|
|
fn size_hint(&mut self, _min: usize, _max: Option<usize>) {}
|
|
|
|
}
|
|
|
|
|
2018-05-15 03:06:54 +08:00
|
|
|
impl<'a, T: Write> Write for &'a mut T {
|
|
|
|
type WriteError = T::WriteError;
|
|
|
|
type FlushError = T::FlushError;
|
|
|
|
|
|
|
|
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> {
|
|
|
|
T::write(self, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> result::Result<(), Self::FlushError> {
|
|
|
|
T::flush(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&mut self, min: usize, max: Option<usize>) {
|
|
|
|
T::size_hint(self, min, max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 00:52:14 +08:00
|
|
|
impl<'a> Write for &'a mut [u8] {
|
|
|
|
type WriteError = !;
|
|
|
|
type FlushError = !;
|
|
|
|
|
|
|
|
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> {
|
|
|
|
let len = buf.len().min(self.len());
|
|
|
|
self[..len].copy_from_slice(&buf[..len]);
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> result::Result<(), Self::FlushError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 03:06:54 +08:00
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
impl<'a> Write for alloc::Vec<u8> {
|
|
|
|
type WriteError = !;
|
|
|
|
type FlushError = !;
|
|
|
|
|
|
|
|
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> {
|
|
|
|
self.extend_from_slice(buf);
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn flush(&mut self) -> result::Result<(), Self::FlushError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 18:02:39 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum CursorError {
|
|
|
|
EndOfBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Cursor<T> {
|
|
|
|
inner: T,
|
|
|
|
pos: usize
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Cursor<T> {
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
pub fn new(inner: T) -> Cursor<T> {
|
|
|
|
Cursor { inner, pos: 0 }
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.inner
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
pub fn get_ref(&self) -> &T {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.inner
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
pub fn position(&self) -> usize {
|
|
|
|
self.pos
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
pub fn set_position(&mut self, pos: usize) {
|
|
|
|
self.pos = pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<[u8]>> Read for Cursor<T> {
|
|
|
|
type ReadError = !;
|
|
|
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> result::Result<usize, Self::ReadError> {
|
|
|
|
let data = &self.inner.as_ref()[self.pos..];
|
|
|
|
let len = buf.len().min(data.len());
|
|
|
|
buf[..len].copy_from_slice(&data[..len]);
|
|
|
|
self.pos += len;
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:03:57 +08:00
|
|
|
impl<'a> Write for Cursor<&'a mut [u8]> {
|
2018-05-14 18:02:39 +08:00
|
|
|
type WriteError = !;
|
|
|
|
type FlushError = !;
|
|
|
|
|
|
|
|
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> {
|
2018-05-14 21:03:57 +08:00
|
|
|
let data = &mut self.inner[self.pos..];
|
2018-05-14 18:02:39 +08:00
|
|
|
let len = buf.len().min(data.len());
|
|
|
|
data[..len].copy_from_slice(&buf[..len]);
|
|
|
|
self.pos += len;
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
fn flush(&mut self) -> result::Result<(), Self::FlushError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "alloc")]
|
2018-05-14 21:03:57 +08:00
|
|
|
impl Write for Cursor<::alloc::Vec<u8>> {
|
2018-05-14 18:02:39 +08:00
|
|
|
type WriteError = !;
|
|
|
|
type FlushError = !;
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
fn write(&mut self, buf: &[u8]) -> result::Result<usize, Self::WriteError> {
|
2018-05-15 03:06:54 +08:00
|
|
|
self.inner.extend_from_slice(buf);
|
2018-05-14 18:02:39 +08:00
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
|
2018-05-14 19:22:10 +08:00
|
|
|
#[inline]
|
2018-05-14 18:02:39 +08:00
|
|
|
fn flush(&mut self) -> result::Result<(), Self::FlushError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|