Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor std::io::Cursor as buf::Cursor (closes #75) #146

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 25 additions & 45 deletions src/buf/buf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain, Cursor};
use byteorder::ByteOrder;
use iovec::IoVec;

use std::{cmp, io, ptr};
use std::{cmp, ptr};

/// Read bytes from a buffer.
///
Expand All @@ -15,8 +15,7 @@ use std::{cmp, io, ptr};
/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`.
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a nice ergonomic improvement over:

use bytes::Buf;
use std::io::Cursor;

///
/// let mut buf = Cursor::new(b"hello world");
///
Expand All @@ -39,8 +38,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
///
/// let mut buf = Cursor::new(b"hello world");
///
Expand All @@ -67,8 +65,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
///
/// let mut buf = Cursor::new(b"hello world");
///
Expand Down Expand Up @@ -134,8 +131,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
///
/// let mut buf = Cursor::new(b"hello world");
///
Expand Down Expand Up @@ -166,8 +162,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
///
/// let mut buf = Cursor::new(b"a");
///
Expand All @@ -189,8 +184,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
///
/// let mut buf = Cursor::new(b"hello world");
/// let mut dst = [0; 5];
Expand Down Expand Up @@ -232,8 +226,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
///
/// let mut buf = Cursor::new(b"\x08 hello");
/// assert_eq!(8, buf.get_u8());
Expand All @@ -255,8 +248,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::Buf;
/// use std::io::Cursor;
/// use bytes::{Buf, Cursor};
///
/// let mut buf = Cursor::new(b"\x08 hello");
/// assert_eq!(8, buf.get_i8());
Expand All @@ -278,8 +270,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x08\x09 hello");
/// assert_eq!(0x0809, buf.get_u16::<BigEndian>());
Expand All @@ -301,8 +292,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x08\x09 hello");
/// assert_eq!(0x0809, buf.get_i16::<BigEndian>());
Expand All @@ -324,8 +314,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello");
/// assert_eq!(0x0809A0A1, buf.get_u32::<BigEndian>());
Expand All @@ -347,8 +336,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello");
/// assert_eq!(0x0809A0A1, buf.get_i32::<BigEndian>());
Expand All @@ -370,8 +358,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
/// assert_eq!(0x0102030405060708, buf.get_u64::<BigEndian>());
Expand All @@ -393,8 +380,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello");
/// assert_eq!(0x0102030405060708, buf.get_i64::<BigEndian>());
Expand All @@ -416,8 +402,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x01\x02\x03 hello");
/// assert_eq!(0x010203, buf.get_uint::<BigEndian>(3));
Expand All @@ -439,8 +424,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x01\x02\x03 hello");
/// assert_eq!(0x010203, buf.get_int::<BigEndian>(3));
Expand All @@ -463,8 +447,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x3F\x99\x99\x9A hello");
/// assert_eq!(1.2f32, buf.get_f32::<BigEndian>());
Expand All @@ -487,8 +470,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BigEndian};
/// use std::io::Cursor;
/// use bytes::{Buf, BigEndian, Cursor};
///
/// let mut buf = Cursor::new(b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello");
/// assert_eq!(1.2f64, buf.get_f64::<BigEndian>());
Expand Down Expand Up @@ -535,8 +517,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BufMut};
/// use std::io::Cursor;
/// use bytes::{Buf, BufMut, Cursor};
///
/// let mut buf = Cursor::new("hello world").take(5);
/// let mut dst = vec![];
Expand Down Expand Up @@ -586,8 +567,7 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, BufMut};
/// use std::io::Cursor;
/// use bytes::{Buf, BufMut, Cursor};
///
/// let mut buf = Cursor::new("hello world");
/// let mut dst = vec![];
Expand Down Expand Up @@ -689,12 +669,12 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
}
}

impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
impl<T: AsRef<[u8]>> Buf for Cursor<T> {
fn remaining(&self) -> usize {
let len = self.get_ref().as_ref().len();
let pos = self.position();

if pos >= len as u64 {
if pos >= len as u32 {
return 0;
}

Expand All @@ -718,7 +698,7 @@ impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {

assert!(pos <= self.get_ref().as_ref().len());

self.set_position(pos as u64);
self.set_position(pos as u32);
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{IntoBuf, Writer};
use super::{Cursor, IntoBuf, Writer};
use byteorder::ByteOrder;
use iovec::IoVec;

use std::{cmp, io, ptr, usize};
use std::{cmp, ptr, usize};

/// A trait for values that provide sequential write access to bytes.
///
Expand Down Expand Up @@ -34,8 +34,7 @@ pub trait BufMut {
/// # Examples
///
/// ```
/// use bytes::BufMut;
/// use std::io::Cursor;
/// use bytes::{BufMut, Cursor};
///
/// let mut dst = [0; 10];
/// let mut buf = Cursor::new(&mut dst[..]);
Expand Down Expand Up @@ -104,8 +103,7 @@ pub trait BufMut {
/// # Examples
///
/// ```
/// use bytes::BufMut;
/// use std::io::Cursor;
/// use bytes::{BufMut, Cursor};
///
/// let mut dst = [0; 5];
/// let mut buf = Cursor::new(&mut dst);
Expand Down Expand Up @@ -253,8 +251,7 @@ pub trait BufMut {
/// `self` must have enough remaining capacity to contain all of `src`.
///
/// ```
/// use bytes::BufMut;
/// use std::io::Cursor;
/// use bytes::{BufMut, Cursor};
///
/// let mut dst = [0; 6];
///
Expand Down Expand Up @@ -672,7 +669,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
}
}

impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for Cursor<T> {
fn remaining_mut(&self) -> usize {
use Buf;
self.remaining()
Expand Down
69 changes: 69 additions & 0 deletions src/buf/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// at http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::cmp;
use std::io::{self, BufRead, Read};

/// A `Cursor` wraps another type and provides it with a
/// [`Seek`] implementation.
///
/// `Cursor`s are typically used with in-memory buffers to allow them to
/// implement [`Read`] and/or [`Write`], allowing these buffers to be used
/// anywhere you might use a reader or writer that does actual I/O.
///
/// The standard library implements some I/O traits on various types which
/// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
/// `Cursor<`[`&[u8]`][bytes]`>`.
#[derive(Clone, Debug)]
pub struct Cursor<T> {
inner: T,
pos: u32,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a u64 in std::io, however @alexcrichton suggested using a smaller type here: #75 (comment)

}

impl<T> Cursor<T> {
/// Creates a new cursor wrapping the provided underlying I/O object.
///
/// Cursor initial position is `0` even if underlying object (e.
/// g. `Vec`) is not empty. So writing to cursor starts with
/// overwriting `Vec` content, not with appending to it.
pub fn new(inner: T) -> Cursor<T> {
Cursor { pos: 0, inner: inner }
}

/// Gets a reference to the underlying value in this cursor.
pub fn get_ref(&self) -> &T { &self.inner }

/// Gets a mutable reference to the underlying value in this cursor.
///
/// Care should be taken to avoid modifying the internal I/O state of the
/// underlying value as it may corrupt this cursor's position.
pub fn get_mut(&mut self) -> &mut T { &mut self.inner }

/// Returns the current position of this cursor.
pub fn position(&self) -> u32 { self.pos }

/// Sets the position of this cursor.
pub fn set_position(&mut self, pos: u32) { self.pos = pos; }
}

impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Read and BufRead impls here aren't actually used by bytes at all. I have included them as without them the following example fails:

https://github.com/tarcieri/bytes/blob/4c3faa7e501ee683629b1792d1e1a290fc83ed4e/src/buf/writer.rs#L67

That said, perhaps people are expecting them? (and possibly Write as well?)

fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let n = Read::read(&mut self.fill_buf()?, buf)?;
self.pos += n as u32;
Ok(n)
}
}

impl<T> BufRead for Cursor<T> where T: AsRef<[u8]> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
let amt = cmp::min(self.pos, self.inner.as_ref().len() as u32);
Ok(&self.inner.as_ref()[(amt as usize)..])
}
fn consume(&mut self, amt: usize) { self.pos += amt as u32; }
}
Loading