Skip to content

Commit dacf272

Browse files
committed
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.
1 parent 47ea0cf commit dacf272

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/libcollectionstest/str.rs

+31
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,37 @@ fn test_bytes_revator() {
813813
}
814814
}
815815

816+
#[test]
817+
fn test_bytesator_nth() {
818+
let s = "ศไทย中华Việt Nam";
819+
let v = [
820+
224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
821+
184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
822+
109
823+
];
824+
825+
let mut b = s.bytes();
826+
assert_eq!(b.nth(2).unwrap(), v[2]);
827+
assert_eq!(b.nth(10).unwrap(), v[10]);
828+
assert_eq!(b.nth(200), None);
829+
}
830+
831+
#[test]
832+
fn test_bytesator_count() {
833+
let s = "ศไทย中华Việt Nam";
834+
835+
let b = s.bytes();
836+
assert_eq!(b.count(), 28)
837+
}
838+
839+
#[test]
840+
fn test_bytesator_last() {
841+
let s = "ศไทย中华Việt Nam";
842+
843+
let b = s.bytes();
844+
assert_eq!(b.last().unwrap(), 109)
845+
}
846+
816847
#[test]
817848
fn test_char_indicesator() {
818849
let s = "ศไทย中华Việt Nam";

src/libcore/str/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,21 @@ impl<'a> Iterator for Bytes<'a> {
387387
fn size_hint(&self) -> (usize, Option<usize>) {
388388
self.0.size_hint()
389389
}
390+
391+
#[inline]
392+
fn count(self) -> usize {
393+
self.0.count()
394+
}
395+
396+
#[inline]
397+
fn last(self) -> Option<Self::Item> {
398+
self.0.last()
399+
}
400+
401+
#[inline]
402+
fn nth(&mut self, n: usize) -> Option<Self::Item> {
403+
self.0.nth(n)
404+
}
390405
}
391406

392407
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)