pub fn iter_units<U: Borrow<Utf16Char>, I: IntoIterator<Item = U>>(
iterable: I,
) -> Utf16CharSplitter<U, I::IntoIter> โ
Expand description
Converts an iterator of Utf16Char
(or &Utf16Char
)
to an iterator of u16
s.
Is equivalent to calling .flat_map()
on the original iterator,
but the returned iterator is about twice as fast.
The exact number of units cannot be known in advance, but size_hint()
gives the possible range.
ยงExamples
From iterator of values:
use encode_unicode::{iter_units, CharExt};
let iterator = "foo".chars().map(|c| c.to_utf16() );
let mut units = [0; 4];
for (u,dst) in iter_units(iterator).zip(&mut units) {*dst=u;}
assert_eq!(units, ['f' as u16, 'o' as u16, 'o' as u16, 0]);
From iterator of references:
use encode_unicode::{iter_units, CharExt, Utf16Char};
// (๐ฃ takes two units)
let chars: Vec<Utf16Char> = "๐ฃ bomb ๐ฃ".chars().map(|c| c.to_utf16() ).collect();
let units: Vec<u16> = iter_units(&chars).collect();
let flat_map: Vec<u16> = chars.iter().flat_map(|u16c| *u16c ).collect();
assert_eq!(units, flat_map);