Skip to content

Commit

Permalink
api: add as_slice, size_hint and FusedIterator to Bytes
Browse files Browse the repository at this point in the history
Fixes #61, Closes #62
  • Loading branch information
fogti authored and BurntSushi committed Oct 18, 2020
1 parent e5b99c3 commit 282ff2b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/ext_slice.rs
Expand Up @@ -5,12 +5,7 @@ use std::ffi::OsStr;
#[cfg(feature = "std")]
use std::path::Path;

use core::cmp;
use core::ops;
use core::ptr;
use core::slice;
use core::str;

use core::{cmp, iter, ops, ptr, slice, str};
use memchr::{memchr, memrchr};

use ascii;
Expand Down Expand Up @@ -3255,13 +3250,28 @@ pub struct Bytes<'a> {
it: slice::Iter<'a, u8>,
}

impl<'a> Bytes<'a> {
/// Views the remaining underlying data as a subslice of the original data.
/// This has the same lifetime as the original slice,
/// and so the iterator can continue to be used while this exists.
#[inline]
pub fn as_slice(&self) -> &'a [u8] {
self.it.as_slice()
}
}

impl<'a> Iterator for Bytes<'a> {
type Item = u8;

#[inline]
fn next(&mut self) -> Option<u8> {
self.it.next().map(|&b| b)
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.it.size_hint()
}
}

impl<'a> DoubleEndedIterator for Bytes<'a> {
Expand All @@ -3278,6 +3288,8 @@ impl<'a> ExactSizeIterator for Bytes<'a> {
}
}

impl<'a> iter::FusedIterator for Bytes<'a> {}

/// An iterator over the fields in a byte string, separated by whitespace.
///
/// This iterator splits on contiguous runs of whitespace, such that the fields
Expand Down

0 comments on commit 282ff2b

Please sign in to comment.