Skip to content

Commit

Permalink
use Result instead of an absolute path to it
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Mar 7, 2018
1 parent c29d772 commit dc98c99
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/io/read.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::cmp;

use Result;

/// Non-blocking reader trait
pub trait Read {
/// An enumeration of possible errors
Expand All @@ -17,21 +19,21 @@ pub trait Read {
/// buf.len()`. The `n` value indicates that the buffer `buf` has been filled in with `n` bytes
/// of data from this source. If `n == 0 && buf.len() > 0` then it can be assumed that this
/// reader has run out of data and will not be able service any future read calls.
fn read(&mut self, buf: &mut [u8]) -> ::Result<usize, Self::Error>;
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
}

impl<'a, R: ?Sized + Read> Read for &'a mut R {
type Error = R::Error;

fn read(&mut self, buf: &mut [u8]) -> ::Result<usize, Self::Error> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
(**self).read(buf)
}
}

impl<'a> Read for &'a [u8] {
type Error = !;

fn read(&mut self, buf: &mut [u8]) -> ::Result<usize, Self::Error> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let len = cmp::min(self.len(), buf.len());
let (head, tail) = self.split_at(len);
buf[..len].copy_from_slice(head);
Expand Down
3 changes: 2 additions & 1 deletion src/io/read_exact.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::mem;

use io::Read;
use Result;

/// Creates a helper that will read from a provided reader until the provided buffer is filled.
///
Expand Down Expand Up @@ -60,7 +61,7 @@ impl<R: Read, B: AsMut<[u8]>> ReadExact<R, B> {
/// error.
///
/// Otherwise will return `nb::Error::WouldBlock`.
pub fn poll(&mut self) -> ::Result<(R, B), Error<R::Error>> {
pub fn poll(&mut self) -> Result<(R, B), Error<R::Error>> {
if let State::Reading { ref mut reader, ref mut buffer, ref mut position } = self.state {
let buffer = buffer.as_mut();
while *position < buffer.len() {
Expand Down
14 changes: 8 additions & 6 deletions src/io/write.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::{cmp, mem};

use Result;

/// Non-blocking writer trait
pub trait Write {
/// An enumeration of possible errors
Expand All @@ -17,40 +19,40 @@ pub trait Write {
/// buf.len()`. The `n` value indicates that `n` bytes from the buffer `buf` have been written
/// to this source. If `n == 0 && buf.len() > 0` then it can be assumed that this writer has
/// run out of space and will not be able to service future writes.
fn write(&mut self, buf: &[u8]) -> ::Result<usize, Self::Error>;
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;

/// Attempt to flush the object, ensuring that any buffered data reach their destination.
///
/// On success, returns `Ok(())`.
///
/// If flushing cannot immediately complete, this method returns `Err(nb::Error::WouldBlock)`.
fn flush(&mut self) -> ::Result<(), Self::Error>;
fn flush(&mut self) -> Result<(), Self::Error>;
}

impl<'a, W: ?Sized + Write> Write for &'a mut W {
type Error = W::Error;

fn write(&mut self, buf: &[u8]) -> ::Result<usize, Self::Error> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
(**self).write(buf)
}

fn flush(&mut self) -> ::Result<(), Self::Error> {
fn flush(&mut self) -> Result<(), Self::Error> {
(**self).flush()
}
}

impl<'a> Write for &'a mut [u8] {
type Error = !;

fn write(&mut self, buf: &[u8]) -> ::Result<usize, Self::Error> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let len = cmp::min(self.len(), buf.len());
let (head, tail) = mem::replace(self, &mut []).split_at_mut(len);
head.copy_from_slice(&buf[..len]);
*self = tail;
Ok(len)
}

fn flush(&mut self) -> ::Result<(), Self::Error> {
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
3 changes: 2 additions & 1 deletion src/io/write_all.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::mem;

use io::Write;
use Result;

/// Creates a helper that will write all data from the provided buffer into the provided writer.
///
Expand Down Expand Up @@ -62,7 +63,7 @@ impl<W: Write, B: AsRef<[u8]>> WriteAll<W, B> {
/// error.
///
/// Otherwise will return `nb::Error::WouldBlock`.
pub fn poll(&mut self) -> ::Result<(W, B), Error<W::Error>> {
pub fn poll(&mut self) -> Result<(W, B), Error<W::Error>> {
if let State::Writing { ref mut writer, ref mut buffer, ref mut position } = self.state {
let buffer = buffer.as_ref();
while *position < buffer.len() {
Expand Down

0 comments on commit dc98c99

Please sign in to comment.