Description
TrustedLen
is implemented for Range for every integer primitve T. TrustedLen requires that the upper bound of .size_hint
is None if the iterator produces more than usize::MAX
elements. <Range as Iterator>.size_hint
uses steps_between
to produce its result. The implementation of steps_between
subtracts the lower from the upper value and casts it to usize. This is implemented undconditionally for u32. On 16bit platforms, the result of the subtraction of two u32 values may not fit inside usize
, producing an invalid size_hint
.
This means, that e.g. collecting into a Vec using its SpecExtend::spec_extend
specialization for TrustedLen allows writing beyond its reserved buffer. One such example input would be (0..(usize::MAX as u32 + 1)).collect::<Vec<_>>()
.
Reference: #47944 (comment)
/cc @bluss