typed_store/rocks/
values.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::marker::PhantomData;
6
7use serde::de::DeserializeOwned;
8
9use super::RocksDBRawIter;
10use crate::TypedStoreError;
11
12/// An iterator over the values of a prefix.
13pub struct Values<'a, V> {
14    db_iter: RocksDBRawIter<'a>,
15    _phantom: PhantomData<V>,
16}
17
18impl<'a, V: DeserializeOwned> Values<'a, V> {
19    pub(crate) fn new(db_iter: RocksDBRawIter<'a>) -> Self {
20        Self {
21            db_iter,
22            _phantom: PhantomData,
23        }
24    }
25}
26
27impl<V: DeserializeOwned> Iterator for Values<'_, V> {
28    type Item = Result<V, TypedStoreError>;
29
30    fn next(&mut self) -> Option<Self::Item> {
31        if self.db_iter.valid() {
32            let value = self
33                .db_iter
34                .key()
35                .and_then(|_| self.db_iter.value().and_then(|v| bcs::from_bytes(v).ok()));
36
37            self.db_iter.next();
38            value.map(Ok)
39        } else {
40            match self.db_iter.status() {
41                Ok(_) => None,
42                Err(err) => Some(Err(TypedStoreError::RocksDB(format!("{err}")))),
43            }
44        }
45    }
46}