android_sparse/
block.rs

1//! A data structure for representing sparse blocks.
2
3use std::fmt;
4
5/// A sparse block and its associated data.
6#[derive(Clone)]
7pub enum Block {
8    /// A raw block holding a byte buffer of length `Block::SIZE`.
9    Raw(Box<[u8; Block::SIZE as usize]>),
10    /// A fill block holding a 4-byte fill value.
11    Fill([u8; 4]),
12    /// A block that signifies a part of the image that can be skipped.
13    Skip,
14    /// A CRC32 block holding a checksum value.
15    Crc32(u32),
16}
17
18impl Block {
19    /// The size of a sparse file block.
20    pub const SIZE: u32 = 4096;
21}
22
23impl fmt::Debug for Block {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        use self::Block::*;
26
27        match self {
28            Raw(r) => write!(f, "Raw({:?})", &r[..]),
29            Fill(_) | Skip | Crc32(_) => self.fmt(f),
30        }
31    }
32}
33
34impl PartialEq for Block {
35    fn eq(&self, other: &Self) -> bool {
36        use self::Block::*;
37
38        match (self, other) {
39            (Raw(r1), Raw(r2)) => r1[..] == r2[..],
40            (Fill(v1), Fill(v2)) => v1 == v2,
41            (Skip, Skip) => true,
42            (Crc32(c1), Crc32(c2)) => c1 == c2,
43            _ => false,
44        }
45    }
46}