mithril_persistence/sqlite/
cursor.rs

1use sqlite::CursorWithOwnership;
2use std::marker::PhantomData;
3
4use super::SqLiteEntity;
5
6/// Database query result Iterator wrapper. This wrapper allows to call entity
7/// hydration for each extracted result.
8pub struct EntityCursor<'a, T> {
9    cursor: CursorWithOwnership<'a>,
10    phantom: PhantomData<T>,
11}
12
13impl<'a, T> EntityCursor<'a, T> {
14    /// [EntityCursor] constructor.
15    pub fn new(cursor: CursorWithOwnership<'a>) -> Self {
16        Self {
17            cursor,
18            phantom: PhantomData,
19        }
20    }
21}
22
23impl<T> Iterator for EntityCursor<'_, T>
24where
25    T: SqLiteEntity,
26{
27    type Item = T;
28
29    /// Spawning entities from Result iterator.
30    /// This iterator will crash the application if an error occures during this process.
31    /// This is intended because it prevents inconsistent data to spread accross the application.
32    fn next(&mut self) -> Option<T> {
33        self.cursor
34            .next()
35            .map(|res| T::hydrate(res.map_err(|e| panic!("{e}")).unwrap()).unwrap())
36    }
37}