aboutsummaryrefslogtreecommitdiff
path: root/database/src/prefix_iterator.rs
diff options
context:
space:
mode:
Diffstat (limited to 'database/src/prefix_iterator.rs')
-rw-r--r--database/src/prefix_iterator.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/database/src/prefix_iterator.rs b/database/src/prefix_iterator.rs
new file mode 100644
index 0000000..3586ae2
--- /dev/null
+++ b/database/src/prefix_iterator.rs
@@ -0,0 +1,22 @@
+/*
+ This file is part of jellything (https://codeberg.org/metamuffin/jellything)
+ which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
+ Copyright (C) 2025 metamuffin <metamuffin.org>
+*/
+
+use anyhow::Result;
+use std::borrow::Cow;
+
+pub struct PrefixIterator<'a> {
+ pub inner: Box<dyn Iterator<Item = Result<Vec<u8>>> + 'a>,
+ pub prefix: Cow<'a, [u8]>,
+}
+impl Iterator for PrefixIterator<'_> {
+ type Item = Result<Vec<u8>>;
+ fn next(&mut self) -> Option<Self::Item> {
+ self.inner.next().filter(|k| match k {
+ Ok(v) => v.starts_with(&self.prefix),
+ Err(_) => true,
+ })
+ }
+}