Function encode_unicode::iter_bytes [−][src]
pub fn iter_bytes<U: Borrow<Utf8Char>, I: IntoIterator<Item = U>>(
iterable: I
) -> Utf8CharSplitter<U, I::IntoIter>ⓘNotable traits for Utf8CharSplitter<U, I>
impl<U: Borrow<Utf8Char>, I: Iterator<Item = U>> Iterator for Utf8CharSplitter<U, I> type Item = u8;impl<U: Borrow<Utf8Char>, I: Iterator<Item = U>> Read for Utf8CharSplitter<U, I>
Converts an iterator of Utf8Char
(or &Utf8Char
)
to an iterator of u8
s.
Is equivalent to calling .flat_map()
on the original iterator,
but the returned iterator is ~40% faster.
The iterator also implements Read
(if the std
feature isn’t disabled).
Reading will never produce an error, and calls to .read()
and .next()
can be mixed.
The exact number of bytes cannot be known in advance, but size_hint()
gives the possible range.
(min: all remaining characters are ASCII, max: all require four bytes)
Examples
From iterator of values:
use encode_unicode::{iter_bytes, CharExt}; let iterator = "foo".chars().map(|c| c.to_utf8() ); let mut bytes = [0; 4]; for (u,dst) in iter_bytes(iterator).zip(&mut bytes) {*dst=u;} assert_eq!(&bytes, b"foo\0");
From iterator of references:
use encode_unicode::{iter_bytes, CharExt, Utf8Char}; let chars: Vec<Utf8Char> = "💣 bomb 💣".chars().map(|c| c.to_utf8() ).collect(); let bytes: Vec<u8> = iter_bytes(&chars).collect(); let flat_map: Vec<u8> = chars.iter().flat_map(|u8c| *u8c ).collect(); assert_eq!(bytes, flat_map);
Read
ing from it:
use encode_unicode::{iter_bytes, CharExt}; use std::io::Read; let s = "Ååh‽"; assert_eq!(s.len(), 8); let mut buf = [b'E'; 9]; let mut reader = iter_bytes(s.chars().map(|c| c.to_utf8() )); assert_eq!(reader.read(&mut buf[..]).unwrap(), 8); assert_eq!(reader.read(&mut buf[..]).unwrap(), 0); assert_eq!(&buf[..8], s.as_bytes()); assert_eq!(buf[8], b'E');