#![allow(dead_code)]
use crate::marker::PhantomData;
use crate::num::NonZeroU16;
use crate::ptr::NonNull;
pub struct WStrUnits<'a> {
lpwstr: NonNull<u16>,
lifetime: PhantomData<&'a [u16]>,
}
impl WStrUnits<'_> {
pub unsafe fn new(lpwstr: *const u16) -> Option<Self> {
Some(Self { lpwstr: NonNull::new(lpwstr as _)?, lifetime: PhantomData })
}
pub fn peek(&self) -> Option<NonZeroU16> {
unsafe { NonZeroU16::new(*self.lpwstr.as_ptr()) }
}
pub fn advance_while<P: FnMut(NonZeroU16) -> bool>(&mut self, mut predicate: P) -> usize {
let mut counter = 0;
while let Some(w) = self.peek() {
if !predicate(w) {
break;
}
counter += 1;
self.next();
}
counter
}
}
impl Iterator for WStrUnits<'_> {
type Item = NonZeroU16;
fn next(&mut self) -> Option<NonZeroU16> {
unsafe {
let next = self.peek()?;
self.lpwstr = NonNull::new_unchecked(self.lpwstr.as_ptr().add(1));
Some(next)
}
}
}