Add overrides to iterator methods for str::Bytes
Specifically, `count`, `last`, and `nth` are implemented to use the methods of the underlying slice iterator. Partially closes #24214.
This commit is contained in:
parent
47ea0cfb6b
commit
dacf2725ec
2 changed files with 46 additions and 0 deletions
|
@ -813,6 +813,37 @@ fn test_bytes_revator() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytesator_nth() {
|
||||
let s = "ศไทย中华Việt Nam";
|
||||
let v = [
|
||||
224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
|
||||
184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
|
||||
109
|
||||
];
|
||||
|
||||
let mut b = s.bytes();
|
||||
assert_eq!(b.nth(2).unwrap(), v[2]);
|
||||
assert_eq!(b.nth(10).unwrap(), v[10]);
|
||||
assert_eq!(b.nth(200), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytesator_count() {
|
||||
let s = "ศไทย中华Việt Nam";
|
||||
|
||||
let b = s.bytes();
|
||||
assert_eq!(b.count(), 28)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytesator_last() {
|
||||
let s = "ศไทย中华Việt Nam";
|
||||
|
||||
let b = s.bytes();
|
||||
assert_eq!(b.last().unwrap(), 109)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char_indicesator() {
|
||||
let s = "ศไทย中华Việt Nam";
|
||||
|
|
|
@ -387,6 +387,21 @@ impl<'a> Iterator for Bytes<'a> {
|
|||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.0.size_hint()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn count(self) -> usize {
|
||||
self.0.count()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn last(self) -> Option<Self::Item> {
|
||||
self.0.last()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nth(&mut self, n: usize) -> Option<Self::Item> {
|
||||
self.0.nth(n)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue