ipfs_bitswap/
block.rs

1use cid::Cid;
2
3/// An Ipfs block consisting of a [`Cid`] and the bytes of the block.
4///
5/// Note: At the moment the equality is based on [`Cid`] equality, which is based on the triple
6/// `(cid::Version, cid::Codec, multihash)`.
7#[derive(Clone, Debug)]
8pub struct Block {
9    /// The content identifier for this block
10    pub cid: Cid,
11    /// The data of this block
12    pub data: Box<[u8]>,
13}
14
15impl PartialEq for Block {
16    fn eq(&self, other: &Self) -> bool {
17        self.cid.hash() == other.cid.hash()
18    }
19}
20
21impl Eq for Block {}
22
23impl Block {
24    pub fn new(data: Box<[u8]>, cid: Cid) -> Self {
25        Self { cid, data }
26    }
27
28    pub fn cid(&self) -> &Cid {
29        &self.cid
30    }
31
32    pub fn data(&self) -> &[u8] {
33        &self.data
34    }
35
36    pub fn into_vec(self) -> Vec<u8> {
37        self.data.into()
38    }
39}