From 95df06de58b711a3745762f5a97546f53a96c2cf Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Wed, 4 Sep 2024 11:38:17 -0500 Subject: [PATCH] ExactSizeIterator --- crates/libs/registry/src/key_iterator.rs | 6 ++++++ crates/libs/registry/src/value_iterator.rs | 6 ++++++ crates/tests/registry/tests/keys.rs | 4 ++++ crates/tests/registry/tests/values.rs | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/crates/libs/registry/src/key_iterator.rs b/crates/libs/registry/src/key_iterator.rs index aca76c5ff1..59dc533436 100644 --- a/crates/libs/registry/src/key_iterator.rs +++ b/crates/libs/registry/src/key_iterator.rs @@ -66,3 +66,9 @@ impl<'a> Iterator for KeyIterator<'a> { }) } } + +impl<'a> ExactSizeIterator for KeyIterator<'a> { + fn len(&self) -> usize { + self.range.len() + } +} diff --git a/crates/libs/registry/src/value_iterator.rs b/crates/libs/registry/src/value_iterator.rs index e11d319b5a..aa63bdf350 100644 --- a/crates/libs/registry/src/value_iterator.rs +++ b/crates/libs/registry/src/value_iterator.rs @@ -80,3 +80,9 @@ impl<'a> Iterator for ValueIterator<'a> { }) } } + +impl<'a> ExactSizeIterator for ValueIterator<'a> { + fn len(&self) -> usize { + self.range.len() + } +} diff --git a/crates/tests/registry/tests/keys.rs b/crates/tests/registry/tests/keys.rs index 4a8114bf33..269cb486a5 100644 --- a/crates/tests/registry/tests/keys.rs +++ b/crates/tests/registry/tests/keys.rs @@ -14,6 +14,10 @@ fn keys() -> Result<()> { let names: Vec = key.keys()?.collect(); assert_eq!(names, ["one", "three", "two"]); + // The "keys" iterator implements `ExactSizeIterator`. + let iter = key.keys()?; + assert_eq!(iter.len(), 3); + let err = key.open("missing").unwrap_err(); assert_eq!(err.code(), HRESULT(0x80070002u32 as i32)); // HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) assert_eq!(err.message(), "The system cannot find the file specified."); diff --git a/crates/tests/registry/tests/values.rs b/crates/tests/registry/tests/values.rs index 9ad13514c7..7a87229f6f 100644 --- a/crates/tests/registry/tests/values.rs +++ b/crates/tests/registry/tests/values.rs @@ -22,5 +22,9 @@ fn values() -> Result<()> { ] ); + // The "values" iterator implements `ExactSizeIterator`. + let iter = key.values()?; + assert_eq!(iter.len(), 3); + Ok(()) }