exocore_chain/chain/
data.rs

1use std::ops::RangeBounds;
2
3use bytes::Bytes;
4use exocore_core::framing::FrameReader;
5
6use crate::data::Data;
7
8/// Data from the chain that can be from a mmap file or owned bytes.
9#[derive(Clone)]
10pub enum ChainData {
11    #[cfg(feature = "mmap")]
12    Mmap(crate::data::MmapData),
13    Bytes(Bytes),
14}
15
16impl Data for ChainData {
17    fn slice<R: RangeBounds<usize>>(&self, r: R) -> &[u8] {
18        match self {
19            #[cfg(feature = "mmap")]
20            ChainData::Mmap(m) => Data::slice(m, r),
21            ChainData::Bytes(m) => Data::slice(m, r),
22        }
23    }
24
25    fn view<R: RangeBounds<usize>>(&self, r: R) -> ChainData {
26        match self {
27            #[cfg(feature = "mmap")]
28            ChainData::Mmap(m) => ChainData::Mmap(Data::view(m, r)),
29            ChainData::Bytes(m) => ChainData::Bytes(Data::view(m, r)),
30        }
31    }
32
33    fn len(&self) -> usize {
34        match self {
35            #[cfg(feature = "mmap")]
36            ChainData::Mmap(m) => Data::len(m),
37            ChainData::Bytes(m) => Data::len(m),
38        }
39    }
40}
41
42impl FrameReader for ChainData {
43    type OwnedType = Bytes;
44
45    fn exposed_data(&self) -> &[u8] {
46        self.slice(..)
47    }
48
49    fn whole_data(&self) -> &[u8] {
50        self.slice(..)
51    }
52
53    fn to_owned_frame(&self) -> Self::OwnedType {
54        panic!("Cannot call to_owned_frame since it could be from an unbounded source")
55    }
56}