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
use cid::Cid;

/// An Ipfs block consisting of a [`Cid`] and the bytes of the block.
///
/// Note: At the moment the equality is based on [`Cid`] equality, which is based on the triple
/// `(cid::Version, cid::Codec, multihash)`.
#[derive(Clone, Debug)]
pub struct Block {
    /// The content identifier for this block
    pub cid: Cid,
    /// The data of this block
    pub data: Box<[u8]>,
}

impl PartialEq for Block {
    fn eq(&self, other: &Self) -> bool {
        self.cid.hash() == other.cid.hash()
    }
}

impl Eq for Block {}

impl Block {
    pub fn new(data: Box<[u8]>, cid: Cid) -> Self {
        Self { cid, data }
    }

    pub fn cid(&self) -> &Cid {
        &self.cid
    }

    pub fn data(&self) -> &[u8] {
        &self.data
    }

    pub fn into_vec(self) -> Vec<u8> {
        self.data.into()
    }
}