libio: removed custom read/write, moved to core_io
This commit is contained in:
parent
9b2b1dadaa
commit
e0516eeda9
@ -1,4 +1,4 @@
|
||||
use ::{Read, Write};
|
||||
use core_io::{Read, Write, Error as IoError};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Cursor<T> {
|
||||
@ -39,9 +39,8 @@ impl<T> Cursor<T> {
|
||||
}
|
||||
|
||||
impl<T: AsRef<[u8]>> Read for Cursor<T> {
|
||||
type ReadError = !;
|
||||
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::ReadError> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
|
||||
let data = &self.inner.as_ref()[self.pos..];
|
||||
let len = buf.len().min(data.len());
|
||||
buf[..len].copy_from_slice(&data[..len]);
|
||||
@ -51,10 +50,8 @@ impl<T: AsRef<[u8]>> Read for Cursor<T> {
|
||||
}
|
||||
|
||||
impl Write for Cursor<&mut [u8]> {
|
||||
type WriteError = !;
|
||||
type FlushError = !;
|
||||
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::WriteError> {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
|
||||
let data = &mut self.inner[self.pos..];
|
||||
let len = buf.len().min(data.len());
|
||||
data[..len].copy_from_slice(&buf[..len]);
|
||||
@ -63,24 +60,22 @@ impl Write for Cursor<&mut [u8]> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<(), Self::FlushError> {
|
||||
fn flush(&mut self) -> Result<(), IoError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl Write for Cursor<::alloc::Vec<u8>> {
|
||||
type WriteError = !;
|
||||
type FlushError = !;
|
||||
|
||||
#[inline]
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::WriteError> {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
|
||||
self.inner.extend_from_slice(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<(), Self::FlushError> {
|
||||
fn flush(&mut self) -> Result<(), IoError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
125
src/libio/lib.rs
125
src/libio/lib.rs
@ -19,127 +19,4 @@ pub use cursor::Cursor;
|
||||
#[cfg(feature = "byteorder")]
|
||||
pub use proto::{ProtoRead, ProtoWrite};
|
||||
#[cfg(all(feature = "byteorder", feature = "alloc"))]
|
||||
pub use proto::ReadStringError;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Error<T> {
|
||||
UnexpectedEnd,
|
||||
Other(T)
|
||||
}
|
||||
|
||||
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<usize, Self::ReadError>;
|
||||
|
||||
/// Read the exact number of bytes required to fill `buf`.
|
||||
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), Error<Self::ReadError>> {
|
||||
while !buf.is_empty() {
|
||||
let read_bytes = self.read(buf)?;
|
||||
if read_bytes == 0 {
|
||||
return Err(Error::UnexpectedEnd)
|
||||
}
|
||||
|
||||
buf = &mut { buf }[read_bytes..];
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Read> Read for &'a mut T {
|
||||
type ReadError = T::ReadError;
|
||||
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::ReadError> {
|
||||
T::read(self, buf)
|
||||
}
|
||||
}
|
||||
|
||||
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<usize, Self::WriteError>;
|
||||
|
||||
/// Flush this output stream, ensuring that all intermediately buffered contents
|
||||
/// reach their destination.
|
||||
fn flush(&mut self) -> Result<(), Self::FlushError>;
|
||||
|
||||
/// Attempts to write an entire buffer into `self`.
|
||||
fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Error<Self::WriteError>> {
|
||||
while buf.len() > 0 {
|
||||
let written_bytes = self.write(buf)?;
|
||||
if written_bytes == 0 {
|
||||
return Err(Error::UnexpectedEnd)
|
||||
}
|
||||
|
||||
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>) {}
|
||||
}
|
||||
|
||||
impl<'a, T: Write> Write for &'a mut T {
|
||||
type WriteError = T::WriteError;
|
||||
type FlushError = T::FlushError;
|
||||
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::WriteError> {
|
||||
T::write(self, buf)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::FlushError> {
|
||||
T::flush(self)
|
||||
}
|
||||
|
||||
fn size_hint(&mut self, min: usize, max: Option<usize>) {
|
||||
T::size_hint(self, min, max)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Write for &'a mut [u8] {
|
||||
type WriteError = !;
|
||||
type FlushError = !;
|
||||
|
||||
fn write(&mut self, buf: &[u8]) -> 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<(), Self::FlushError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
impl<'a> Write for alloc::Vec<u8> {
|
||||
type WriteError = !;
|
||||
type FlushError = !;
|
||||
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::WriteError> {
|
||||
self.extend_from_slice(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> Result<(), Self::FlushError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub use proto::ReadStringError;
|
@ -3,7 +3,7 @@ use byteorder::{ByteOrder, NativeEndian};
|
||||
use alloc::vec;
|
||||
use alloc::string::String;
|
||||
|
||||
use ::{Read, Write, Error as IoError};
|
||||
use core_io::{Read, Write, Error as IoError};
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@ -142,7 +142,7 @@ pub trait ProtoWrite {
|
||||
}
|
||||
|
||||
impl<T> ProtoRead for T where T: Read + ?Sized {
|
||||
type ReadError = IoError<T::ReadError>;
|
||||
type ReadError = IoError;
|
||||
|
||||
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::ReadError> {
|
||||
T::read_exact(self, buf)
|
||||
@ -150,7 +150,7 @@ impl<T> ProtoRead for T where T: Read + ?Sized {
|
||||
}
|
||||
|
||||
impl<T> ProtoWrite for T where T: Write + ?Sized {
|
||||
type WriteError = IoError<T::WriteError>;
|
||||
type WriteError = IoError;
|
||||
|
||||
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::WriteError> {
|
||||
T::write_all(self, buf)
|
||||
|
Loading…
Reference in New Issue
Block a user