Skip to main content

ab_core_primitives/segments/
archival_history_segment.rs

1use crate::pieces::{FlatPieces, Piece, PieceArray, PiecePosition};
2use crate::segments::RecordedHistorySegment;
3use derive_more::{Deref, DerefMut};
4use std::ops::{Index, IndexMut};
5
6/// Archived history segment after archiving is applied.
7#[derive(Debug, Clone, Eq, PartialEq, Deref, DerefMut)]
8#[repr(transparent)]
9pub struct ArchivedHistorySegment(FlatPieces);
10
11impl Default for ArchivedHistorySegment {
12    #[inline]
13    fn default() -> Self {
14        Self(FlatPieces::new(Self::NUM_PIECES))
15    }
16}
17
18impl Index<PiecePosition> for ArchivedHistorySegment {
19    type Output = PieceArray;
20
21    fn index(&self, index: PiecePosition) -> &Self::Output {
22        // SAFETY: The size of the archived history segment is known and protected invariant
23        unsafe { self.get_unchecked(usize::from(index)) }
24    }
25}
26
27impl IndexMut<PiecePosition> for ArchivedHistorySegment {
28    fn index_mut(&mut self, index: PiecePosition) -> &mut Self::Output {
29        // SAFETY: The size of the archived history segment is known and protected invariant
30        unsafe { self.get_unchecked_mut(usize::from(index)) }
31    }
32}
33
34impl ArchivedHistorySegment {
35    /// Number of pieces in one segment of archived history.
36    pub const NUM_PIECES: usize = RecordedHistorySegment::NUM_PIECES;
37    /// Size of archived history segment in bytes.
38    ///
39    /// It includes erasure coded [`crate::pieces::PieceArray`]s (both source and parity) that are
40    /// composed of [`crate::pieces::Record`]s together with corresponding roots and
41    /// proofs.
42    pub const SIZE: usize = Piece::SIZE * Self::NUM_PIECES;
43
44    /// Ensure archived history segment contains cheaply cloneable shared data.
45    ///
46    /// Internally archived history segment uses CoW mechanism and can store either mutable owned
47    /// data or data that is cheap to clone, calling this method will ensure further clones and
48    /// returned pieces will not result in additional memory allocations.
49    pub fn to_shared(self) -> Self {
50        Self(self.0.to_shared())
51    }
52}