iterator sizing lib
bendn 2024-03-20
commit 52ea1f5
-rw-r--r--.gitignore1
-rw-r--r--Cargo.toml9
-rw-r--r--LICENSE21
-rw-r--r--README.md3
-rw-r--r--src/lib.rs35
5 files changed, 69 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ffa3bbd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+Cargo.lock \ No newline at end of file
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..d176b56
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "hinted"
+version = "0.0.0"
+edition = "2021"
+authors = ["bend-n <[email protected]>"]
+license = "MIT"
+repository = "https://github.com/bend-n/hinted"
+exclude = [".gitignore"]
+description = "small crate for providing a size hint on an iterator"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..2f002a4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 bendn
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..9bdde41
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# hinted
+
+small library providing a `hinted` function to set the lower bound size hint of any iterator. \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..86bb130
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,35 @@
+/// Provides a hint for an iterator.
+pub struct Hinted<I: Iterator> {
+ /// Wrapped iterator.
+ pub iter: I,
+ /// Size hint, decreased on every call to [`next()`](Iterator::next).
+ pub hint: usize,
+}
+
+/// Set the size hint for an iterator.
+pub trait HintExt: Iterator
+where
+ Self: Sized,
+{
+ fn hinted(self, hint: usize) -> Hinted<Self> {
+ Hinted { iter: self, hint }
+ }
+}
+
+impl<I: Iterator> HintExt for I {}
+
+impl<I> Iterator for Hinted<I>
+where
+ I: Iterator + Sized,
+{
+ type Item = <I as Iterator>::Item;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.hint = self.hint.saturating_sub(1);
+ self.iter.next()
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (self.hint, self.iter.size_hint().1)
+ }
+}