1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::collections::BTreeSet;
use std::fmt::{self, Display, Formatter};

use blstrs::Scalar as Fr;
use byteorder::{ByteOrder, LittleEndian};
use serde::{Deserialize, Serialize};

/// An ordered set of `SectorId`s.
pub type OrderedSectorSet = BTreeSet<SectorId>;

/// Identifier for a single sector.
#[derive(
    Default, Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize,
)]
pub struct SectorId(u64);

impl From<u64> for SectorId {
    fn from(n: u64) -> Self {
        SectorId(n)
    }
}

impl From<SectorId> for u64 {
    fn from(n: SectorId) -> Self {
        n.0
    }
}

impl From<SectorId> for Fr {
    fn from(n: SectorId) -> Self {
        Fr::from(n.0)
    }
}

impl Display for SectorId {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "SectorId({})", self.0)
    }
}

impl SectorId {
    pub fn as_fr_safe(self) -> [u8; 32] {
        let mut buf: [u8; 32] = [0; 32];
        LittleEndian::write_u64(&mut buf[0..8], self.0);
        buf
    }
}