Skip to main content

ruda_dataset/dataset/
base.rs

1use std::sync::Arc;
2
3use crate::DatasetIterator;
4
5/// The dataset trait defines a basic collection of items with a predefined size.
6pub trait Dataset<I>: Send + Sync {
7    /// Gets the item at the given index.
8    fn get(&self, index: usize) -> Option<I>;
9
10    /// Fetch actual items in requested order, preserving repeated indices.
11    /// None means at least one requested item is absent; no filler is returned.
12    /// Storage-backed implementations may combine I/O without changing order.
13    fn get_many(&self, indices: &[usize]) -> Option<Vec<I>> {
14        indices.iter().map(|&index| self.get(index)).collect()
15    }
16
17    /// Gets the number of items in the dataset.
18    fn len(&self) -> usize;
19
20    /// Checks if the dataset is empty.
21    fn is_empty(&self) -> bool {
22        self.len() == 0
23    }
24
25    /// Returns an iterator over the dataset.
26    fn iter(&self) -> DatasetIterator<'_, I>
27    where
28        Self: Sized,
29    {
30        DatasetIterator::new(self)
31    }
32}
33
34impl<D, I> Dataset<I> for Arc<D>
35where
36    D: Dataset<I>,
37{
38    fn get(&self, index: usize) -> Option<I> {
39        self.as_ref().get(index)
40    }
41
42    fn get_many(&self, indices: &[usize]) -> Option<Vec<I>> {
43        self.as_ref().get_many(indices)
44    }
45
46    fn len(&self) -> usize {
47        self.as_ref().len()
48    }
49}
50
51impl<I> Dataset<I> for Arc<dyn Dataset<I>> {
52    fn get(&self, index: usize) -> Option<I> {
53        self.as_ref().get(index)
54    }
55
56    fn get_many(&self, indices: &[usize]) -> Option<Vec<I>> {
57        self.as_ref().get_many(indices)
58    }
59
60    fn len(&self) -> usize {
61        self.as_ref().len()
62    }
63}
64
65impl<D, I> Dataset<I> for Box<D>
66where
67    D: Dataset<I>,
68{
69    fn get(&self, index: usize) -> Option<I> {
70        self.as_ref().get(index)
71    }
72
73    fn get_many(&self, indices: &[usize]) -> Option<Vec<I>> {
74        self.as_ref().get_many(indices)
75    }
76
77    fn len(&self) -> usize {
78        self.as_ref().len()
79    }
80}
81
82impl<I> Dataset<I> for Box<dyn Dataset<I>> {
83    fn get(&self, index: usize) -> Option<I> {
84        self.as_ref().get(index)
85    }
86
87    fn get_many(&self, indices: &[usize]) -> Option<Vec<I>> {
88        self.as_ref().get_many(indices)
89    }
90
91    fn len(&self) -> usize {
92        self.as_ref().len()
93    }
94}