Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bytes-sgx
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Markus Becker
bytes-sgx
Commits
8fec8a92
Commit
8fec8a92
authored
8 years ago
by
Carl Lerche
Browse files
Options
Downloads
Patches
Plain Diff
Implement iterator adapter for `Buf`
parent
4f8c5651
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/buf/buf.rs
+20
-1
20 additions, 1 deletion
src/buf/buf.rs
src/buf/iter.rs
+113
-0
113 additions, 0 deletions
src/buf/iter.rs
src/buf/mod.rs
+2
-0
2 additions, 0 deletions
src/buf/mod.rs
src/bytes.rs
+38
-1
38 additions, 1 deletion
src/bytes.rs
with
173 additions
and
2 deletions
src/buf/buf.rs
+
20
−
1
View file @
8fec8a92
use
super
::{
Take
,
Reader
,
FromBuf
};
use
super
::{
Take
,
Reader
,
Iter
,
FromBuf
};
use
byteorder
::
ByteOrder
;
use
byteorder
::
ByteOrder
;
use
std
::{
cmp
,
ptr
};
use
std
::{
cmp
,
ptr
};
...
@@ -546,4 +546,23 @@ pub trait Buf {
...
@@ -546,4 +546,23 @@ pub trait Buf {
fn
reader
(
self
)
->
Reader
<
Self
>
where
Self
:
Sized
{
fn
reader
(
self
)
->
Reader
<
Self
>
where
Self
:
Sized
{
super
::
reader
::
new
(
self
)
super
::
reader
::
new
(
self
)
}
}
/// Returns an iterator over the bytes contained by the buffer.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
/// assert_eq!(iter.next(), Some(b'b'));
/// assert_eq!(iter.next(), Some(b'c'));
/// assert_eq!(iter.next(), None);
/// ```
fn
iter
(
self
)
->
Iter
<
Self
>
where
Self
:
Sized
{
super
::
iter
::
new
(
self
)
}
}
}
This diff is collapsed.
Click to expand it.
src/buf/iter.rs
0 → 100644
+
113
−
0
View file @
8fec8a92
use
Buf
;
/// Iterator over the bytes contained by the buffer.
///
/// This struct is created by the [`iter`] method on [`Buf`].
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
/// assert_eq!(iter.next(), Some(b'b'));
/// assert_eq!(iter.next(), Some(b'c'));
/// assert_eq!(iter.next(), None);
/// ```
///
/// [`iter`]: trait.Buf.html#method.iter
/// [`Buf`]: trait.Buf.html
pub
struct
Iter
<
T
>
{
inner
:
T
,
}
impl
<
T
>
Iter
<
T
>
{
/// Consumes this `Iter`, returning the underlying value.
///
/// # Examples
///
/// ```rust
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
///
/// let buf = iter.into_inner();
/// assert_eq!(2, buf.remaining());
/// ```
pub
fn
into_inner
(
self
)
->
T
{
self
.inner
}
/// Gets a reference to the underlying `Buf`.
///
/// It is inadvisable to directly read from the underlying `Buf`.
///
/// # Examples
///
/// ```rust
/// use bytes::{Buf, IntoBuf, Bytes};
///
/// let buf = Bytes::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
///
/// assert_eq!(2, iter.get_ref().remaining());
/// ```
pub
fn
get_ref
(
&
self
)
->
&
T
{
&
self
.inner
}
/// Gets a mutable reference to the underlying `Buf`.
///
/// It is inadvisable to directly read from the underlying `Buf`.
///
/// # Examples
///
/// ```rust
/// use bytes::{Buf, IntoBuf, BytesMut};
///
/// let buf = BytesMut::from(&b"abc"[..]).into_buf();
/// let mut iter = buf.iter();
///
/// assert_eq!(iter.next(), Some(b'a'));
///
/// iter.get_mut().set_position(0);
///
/// assert_eq!(iter.next(), Some(b'a'));
/// ```
pub
fn
get_mut
(
&
mut
self
)
->
&
mut
T
{
&
mut
self
.inner
}
}
pub
fn
new
<
T
>
(
inner
:
T
)
->
Iter
<
T
>
{
Iter
{
inner
:
inner
}
}
impl
<
T
:
Buf
>
Iterator
for
Iter
<
T
>
{
type
Item
=
u8
;
fn
next
(
&
mut
self
)
->
Option
<
u8
>
{
if
!
self
.inner
.has_remaining
()
{
return
None
;
}
let
b
=
self
.inner
.bytes
()[
0
];
self
.inner
.advance
(
1
);
Some
(
b
)
}
fn
size_hint
(
&
self
)
->
(
usize
,
Option
<
usize
>
)
{
let
rem
=
self
.inner
.remaining
();
(
rem
,
Some
(
rem
))
}
}
This diff is collapsed.
Click to expand it.
src/buf/mod.rs
+
2
−
0
View file @
8fec8a92
...
@@ -22,6 +22,7 @@ mod buf;
...
@@ -22,6 +22,7 @@ mod buf;
mod
buf_mut
;
mod
buf_mut
;
mod
from_buf
;
mod
from_buf
;
mod
into_buf
;
mod
into_buf
;
mod
iter
;
mod
reader
;
mod
reader
;
mod
source
;
mod
source
;
mod
take
;
mod
take
;
...
@@ -31,6 +32,7 @@ pub use self::buf::Buf;
...
@@ -31,6 +32,7 @@ pub use self::buf::Buf;
pub
use
self
::
buf_mut
::
BufMut
;
pub
use
self
::
buf_mut
::
BufMut
;
pub
use
self
::
from_buf
::
FromBuf
;
pub
use
self
::
from_buf
::
FromBuf
;
pub
use
self
::
into_buf
::
IntoBuf
;
pub
use
self
::
into_buf
::
IntoBuf
;
pub
use
self
::
iter
::
Iter
;
pub
use
self
::
reader
::
Reader
;
pub
use
self
::
reader
::
Reader
;
pub
use
self
::
source
::
Source
;
pub
use
self
::
source
::
Source
;
pub
use
self
::
take
::
Take
;
pub
use
self
::
take
::
Take
;
...
...
This diff is collapsed.
Click to expand it.
src/bytes.rs
+
38
−
1
View file @
8fec8a92
use
{
IntoBuf
,
BufMut
};
use
{
IntoBuf
,
Buf
,
BufMut
};
use
buf
::
Iter
;
use
std
::{
cmp
,
fmt
,
mem
,
hash
,
ops
,
slice
,
ptr
,
usize
};
use
std
::{
cmp
,
fmt
,
mem
,
hash
,
ops
,
slice
,
ptr
,
usize
};
use
std
::
borrow
::
Borrow
;
use
std
::
borrow
::
Borrow
;
...
@@ -733,6 +734,24 @@ impl Borrow<[u8]> for Bytes {
...
@@ -733,6 +734,24 @@ impl Borrow<[u8]> for Bytes {
}
}
}
}
impl
IntoIterator
for
Bytes
{
type
Item
=
u8
;
type
IntoIter
=
Iter
<
Cursor
<
Bytes
>>
;
fn
into_iter
(
self
)
->
Self
::
IntoIter
{
self
.into_buf
()
.iter
()
}
}
impl
<
'a
>
IntoIterator
for
&
'a
Bytes
{
type
Item
=
u8
;
type
IntoIter
=
Iter
<
Cursor
<&
'a
Bytes
>>
;
fn
into_iter
(
self
)
->
Self
::
IntoIter
{
self
.into_buf
()
.iter
()
}
}
/*
/*
*
*
* ===== BytesMut =====
* ===== BytesMut =====
...
@@ -1286,6 +1305,24 @@ impl Clone for BytesMut {
...
@@ -1286,6 +1305,24 @@ impl Clone for BytesMut {
}
}
}
}
impl
IntoIterator
for
BytesMut
{
type
Item
=
u8
;
type
IntoIter
=
Iter
<
Cursor
<
BytesMut
>>
;
fn
into_iter
(
self
)
->
Self
::
IntoIter
{
self
.into_buf
()
.iter
()
}
}
impl
<
'a
>
IntoIterator
for
&
'a
BytesMut
{
type
Item
=
u8
;
type
IntoIter
=
Iter
<
Cursor
<&
'a
BytesMut
>>
;
fn
into_iter
(
self
)
->
Self
::
IntoIter
{
self
.into_buf
()
.iter
()
}
}
/*
/*
*
*
* ===== Inner =====
* ===== Inner =====
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment