-
Notifications
You must be signed in to change notification settings - Fork 309
io::Cursor
is a bit out of place
#75
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
Comments
I also just twitch a bit whenever I see |
Makes sense. I mostly opted for Another option would be to bring back |
I think a local "cursor type" (w/e the name) may be right here, but I wouldn't add it just yet (I don't know what it should look like) |
A bit off topic, but note that I love it being in std, you can easily use it to Read over a buffer. Very interesting when you only offer a |
Using
|
+1 on this. Perhaps I should change that PR to just stop using |
Extracts the minimal parts of std::io::Cursor needed to support bytes use cases. This is needed to support no_std as std::io is unavailable in these contexts. It also (arguably) improves the API ergonomics by simplifying imports. Per notes on tokio-rs#75, changes the u64 position to a u32 to save space.
So to clarify what will happen in v0.5:
Look right? |
Looks bout right. |
I am looking at implementing Am I reading the issues correctly that you want to get rid of Cursor. Does the replacement implementation you have in mind look like adding a position field to |
Never mind, I think I figured out how to do it using the fields of The only problem I ran into is the removal of With the current design, it would be impossible to seek backwards when in Update: I got the tests to pass, but it reveals in my opinion a dubious iterator implementation for Buf that consumes the Buf, emptying it of data. This went unnoticed before because Cursor was wrapping the underlying storage. diff --git a/src/buf/iter.rs b/src/buf/iter.rs
index 9345c05..389c252 100644
--- a/src/buf/iter.rs
+++ b/src/buf/iter.rs
@@ -76,14 +76,14 @@ impl<T> Iter<T> {
/// ```rust
/// use bytes::{Buf, IntoBuf, BytesMut};
///
- /// let buf = BytesMut::from(&b"abc"[..]).into_buf();
+ /// let buf = BytesMut::from(&b"abc"[..]);
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
///
- /// iter.get_mut().set_position(0);
+ /// iter.get_mut().advance(1);
///
- /// assert_eq!(iter.next(), Some(b'a'));
+ /// assert_eq!(iter.next(), Some(b'c'));
/// ```
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
diff --git a/src/bytes.rs b/src/bytes.rs
index 4650235..979f3fc 100644
--- a/src/bytes.rs
+++ b/src/bytes.rs
@@ -1516,6 +1516,26 @@ impl BytesMut {
}
}
+impl Buf for BytesMut {
+ #[inline]
+ fn remaining(&self) -> usize {
+ self.len()
+ }
+
+ fn bytes(&self) -> &[u8] {
+ &(self.inner.as_ref())
+ }
+
+ //fn bytes_vec<'b>(&'b self, dst: &mut [IoVec<'b>]) -> usize {
+ // (**self).bytes_vec(dst)
+ //}
+
+ fn advance(&mut self, cnt: usize) {
+ assert!(cnt <= self.inner.as_ref().len(), "cannot advance past `remaining`");
+ unsafe { self.inner.set_start(cnt); }
+ }
+}
+
impl BufMut for BytesMut {
#[inline]
fn remaining_mut(&self) -> usize {
@@ -1561,22 +1581,6 @@ impl BufMut for BytesMut {
}
}
-impl IntoBuf for BytesMut {
- type Buf = Cursor<Self>;
-
- fn into_buf(self) -> Self::Buf {
- Cursor::new(self)
- }
-}
-
-impl<'a> IntoBuf for &'a BytesMut {
- type Buf = Cursor<&'a BytesMut>;
-
- fn into_buf(self) -> Self::Buf {
- Cursor::new(self)
- }
-}
-
impl AsRef<[u8]> for BytesMut {
#[inline]
fn as_ref(&self) -> &[u8] {
@@ -1741,19 +1745,19 @@ impl Clone for BytesMut {
impl IntoIterator for BytesMut {
type Item = u8;
- type IntoIter = Iter<Cursor<BytesMut>>;
+ type IntoIter = Iter<BytesMut>;
fn into_iter(self) -> Self::IntoIter {
- self.into_buf().iter()
+ self.iter()
}
}
-impl<'a> IntoIterator for &'a BytesMut {
+impl<'a> IntoIterator for &'a mut BytesMut {
type Item = u8;
- type IntoIter = Iter<Cursor<&'a BytesMut>>;
+ type IntoIter = Iter<&'a mut BytesMut>;
fn into_iter(self) -> Self::IntoIter {
- self.into_buf().iter()
+ self.iter()
}
}
diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs
index ccc89fa..67ff12c 100644
--- a/tests/test_bytes.rs
+++ b/tests/test_bytes.rs
@@ -181,11 +181,11 @@ fn split_off_to_loop() {
}
{
let mut bytes = BytesMut::from(&s[..]);
- let off = bytes.split_off(i);
+ let mut off = bytes.split_off(i);
assert_eq!(i, bytes.len());
let mut sum = Vec::new();
- sum.extend(&bytes);
- sum.extend(&off);
+ sum.extend(&mut bytes);
+ sum.extend(&mut off);
assert_eq!(&s[..], &sum[..]);
}
{
@@ -199,11 +199,11 @@ fn split_off_to_loop() {
}
{
let mut bytes = BytesMut::from(&s[..]);
- let off = bytes.split_to(i);
+ let mut off = bytes.split_to(i);
assert_eq!(i, off.len());
let mut sum = Vec::new();
- sum.extend(&off);
- sum.extend(&bytes);
+ sum.extend(&mut off);
+ sum.extend(&mut bytes);
assert_eq!(&s[..], &sum[..]);
}
} |
I think this may not wish to use
io::Cursor
pervasively. This use case doesn't have much to do withstd::io
. The std version also stores au64
which isn't needed forbytes
's use case I believe.The text was updated successfully, but these errors were encountered: