Debian Codegraph @

third_party/rust/rkv/src/backend/impl_lmdb/iter.rs

raw
1 // Copyright 2018-2019 Mozilla
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 // this file except in compliance with the License. You may obtain a copy of the
5 // License at http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software distributed
7 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 // specific language governing permissions and limitations under the License.
10
11 use super::ErrorImpl;
12 use crate::backend::traits::BackendIter;
13
14 pub struct IterImpl<'i, C> {
15 // LMDB semantics dictate that a cursor must be valid for the entire lifetime
16 // of an iterator. In other words, cursors must not be dropped while an
17 // iterator built from it is alive. Unfortunately, the LMDB crate API does
18 // not express this through the type system, so we must enforce it somehow.
19 #[allow(dead_code)]
20 cursor: C,
21 iter: lmdb::Iter<'i>,
22 }
23
24 impl<'i, C> IterImpl<'i, C> {
25 pub(crate) fn new(
26 mut cursor: C,
27 to_iter: impl FnOnce(&mut C) -> lmdb::Iter<'i>,
28 ) -> IterImpl<'i, C> {
29 let iter = to_iter(&mut cursor);
30 IterImpl { cursor, iter }
31 }
32 }
33
34 impl<'i, C> BackendIter<'i> for IterImpl<'i, C> {
35 type Error = ErrorImpl;
36
37 #[allow(clippy::type_complexity)]
38 fn next(&mut self) -> Option<Result<(&'i [u8], &'i [u8]), Self::Error>> {
39 self.iter.next().map(|e| e.map_err(ErrorImpl::LmdbError))
40 }
41 }