Implement iterators on pointers
I've been thinking for a while about how to best be able to use Rust iterators on C-style strings, and I've now come up with this implementation:
-
NulTerminated
, an iterator constructed from a*const T
on a nul-terminated buffer with read access -
SrcDstPtrIter
(somewhat awkward name?), a zipped input-and-output iterator built from an input iterator and a*mut U
. This allows safe iteration with elements of type(T, &mut MaybeUninit<U>)
, whereT
is the item type of the input iterator.
The implementation is designed to be as misuse-resistant as possible. Soundness, including avoiding buffer overreads and dereferences past the end of output buffers, depends on the caller to use them correctly, but this should be encapsulated in the unsafe
constructor functions and their documented caller requirements.
This MR also pulls in a dependency on num-traits to implement type-generic nul-termination. Local mini-trait implemented instead.
To illustrate the use of these iterators, I've refactored the functions strcpy
, strlen
, strnlen
and wcslen
to use them.
Edited by Peter Limkilde Svendsen