vortex_file/footer/
segment.rs

1use std::ops::Range;
2
3use vortex_buffer::Alignment;
4use vortex_error::VortexError;
5use vortex_flatbuffers::footer as fb;
6
7/// The location of a segment within a Vortex file.
8///
9/// A segment is a contiguous block of bytes in a file that contains a part of the file's data.
10/// The `SegmentSpec` struct specifies the location and properties of a segment.
11#[derive(Clone, Debug)]
12pub struct SegmentSpec {
13    /// The byte offset of the segment from the start of the file.
14    pub offset: u64,
15    /// The length of the segment in bytes.
16    pub length: u32,
17    /// The memory alignment requirement of the segment.
18    pub alignment: Alignment,
19}
20
21impl SegmentSpec {
22    /// Returns the byte range of the segment within the file.
23    ///
24    /// The range starts at the segment's offset and extends for its length.
25    pub fn byte_range(&self) -> Range<u64> {
26        self.offset..self.offset + u64::from(self.length)
27    }
28}
29
30impl From<&SegmentSpec> for fb::SegmentSpec {
31    fn from(value: &SegmentSpec) -> Self {
32        fb::SegmentSpec::new(value.offset, value.length, value.alignment.exponent(), 0, 0)
33    }
34}
35
36impl TryFrom<&fb::SegmentSpec> for SegmentSpec {
37    type Error = VortexError;
38
39    fn try_from(value: &fb::SegmentSpec) -> Result<Self, Self::Error> {
40        Ok(Self {
41            offset: value.offset(),
42            length: value.length(),
43            alignment: Alignment::from_exponent(value.alignment_exponent()),
44        })
45    }
46}