From d41ec992afc20fb86b5ee61c39a577e953066e6a Mon Sep 17 00:00:00 2001 From: Peter Limkilde Svendsen Date: Sun, 27 Oct 2024 13:43:37 +0000 Subject: [PATCH] Implement FusedIterator for NulTerminated, NulTerminatedInclusive --- src/iter.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/iter.rs b/src/iter.rs index c383d81f7e..a705ab1eff 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -1,6 +1,11 @@ //! Utilities to help use Rust iterators on C strings. -use core::{iter::Iterator, marker::PhantomData, mem::MaybeUninit, ptr::NonNull}; +use core::{ + iter::{FusedIterator, Iterator}, + marker::PhantomData, + mem::MaybeUninit, + ptr::NonNull, +}; use crate::platform::types::*; @@ -77,6 +82,10 @@ impl<'a, T: Zero> NulTerminated<'a, T> { } } +// Once the terminating nul has been encountered, the pointer will not advance +// further and the iterator will thus keep returning None. +impl<'a, T: Zero> FusedIterator for NulTerminated<'a, T> {} + /// An iterator over a nul-terminated buffer, including the terminating nul. /// /// Similar to [`NulTerminated`], but includes the terminating nul. @@ -130,6 +139,10 @@ impl<'a, T: Zero> NulTerminatedInclusive<'a, T> { } } +// Once the terminating nul has been encountered, the internal Option will be +// set to None, ensuring that we will keep returning None. +impl<'a, T: Zero> FusedIterator for NulTerminatedInclusive<'a, T> {} + /// A zipped iterator mapping an input iterator to an "out" pointer. /// /// This is intended to allow safe, iterative writing to an "out pointer".