Here is a way to use unsafe by its usual meaning:
trait Iterator {
...
fn size_hint(&self) -> SizeHint { SizeHint::from_bounds(0, None) }
}
pub enum Size {
Exact(usize),
Hint(usize, Option<usize>),
}
pub struct SizeHint(/* private */ Size);
impl SizeHint {
pub fn from_bounds(low: usize, high: Option<usize>) -> Self {
SizeHint(Size::Hint(low, high))
}
/* **unsafe** */
pub unsafe fn exact(size: usize) -> Self { SizeHint(Size::Exact(size)) }
/* and then accessor methods etc */
pub fn exact_size(&self) -> Option<usize> { .. }
pub fn lower_bound(&self) -> usize { .. }
}
While size hints are revisited, we might want to simplify & remove the upper bound, it is never used.
I love spewing ideas, so why not this model instead?
pub enum Size {
Exact(usize),
LowerBound(usize),
Endless,
}