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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Copyright (c) 2019 RepliXio Ltd. All rights reserved.
// Use is subject to license terms.
//

use itertools::Either::{Left, Right};
use std::iter::{empty, once, repeat};

use serde::de::{self, Error};
use serde::{Serialize, Serializer};

/// ZLE (Zero Length Encoding) is a compact representation of the sequence of zeros
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Zle(usize);

const ZLE_SLICE_SIZE: usize = 512;

static ZLE_SLICE: &[u8] = &[0; ZLE_SLICE_SIZE];

impl Zle {
    /// Constructs fresh `Zle` of a specified size
    pub fn new(capacity: usize) -> Self {
        Self(capacity)
    }

    #[inline]
    /// Get the number of slices this Zle represents
    pub fn size(&self) -> usize {
        (self.0 - 1) / ZLE_SLICE_SIZE + 1
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.size() == 0
    }

    pub fn iter_bytes(&self) -> impl Iterator<Item = u8> {
        repeat(0).take(self.0)
    }

    /// Get a view of an `Element` as an iterator of slices
    /// Note `Zle` is represented by the same slice repeated
    pub fn iter(&self) -> impl Iterator<Item = &[u8]> {
        let take_len = self.0 / ZLE_SLICE_SIZE;
        let residue = self.0 % ZLE_SLICE_SIZE;

        repeat(ZLE_SLICE).take(take_len).chain(if 0 == residue {
            Left(empty())
        } else {
            Right(once(&ZLE_SLICE[0..residue]))
        })
    }
}

impl From<Zle> for Vec<u8> {
    fn from(zle: Zle) -> Self {
        vec![0; zle.0]
    }
}

impl Serialize for Zle {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.collect_seq(self.iter_bytes())
    }
}

impl<'de> de::Deserialize<'de> for Zle {
    fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        Err(D::Error::custom("Cannot deserialize Zle"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn zle() {
        assert_eq!(Zle::new(1024), Zle::new(1024));
    }

    #[test]
    fn zle_debug() {
        let e = Zle::new(1024);
        assert_eq!(format!("{:?}", e), "Zle(1024)");
    }
}