rsd/types/
buffer.rs

1use crate::types::{Hash, Uint256};
2use hex::encode;
3use std::ops;
4
5//Our version of Buffer that is implemented in bio - > https://github.com/bcoin-org/bufio
6#[derive(Default, Debug, PartialEq)]
7pub struct Buffer(Vec<u8>);
8
9impl Buffer {
10    pub fn new() -> Self {
11        Buffer::default()
12    }
13
14    //Write u32 in Little Endian format
15    //Possibly return the amount of data written //TODO - see if needed anywhere.
16    pub fn write_u32(&mut self, data: u32) {
17        self.0.extend_from_slice(&data.to_le_bytes());
18    }
19
20    pub fn write_u64(&mut self, data: u64) {
21        self.0.extend_from_slice(&data.to_le_bytes());
22    }
23
24    pub fn write_hash(&mut self, hash: Hash) {
25        self.0.extend(&hash.to_array());
26    }
27
28    pub fn write_u256(&mut self, data: Uint256) {
29        self.0.extend_from_slice(&data.to_le_bytes());
30    }
31
32    //Return Hex string of the buffer.
33    pub fn to_hex(&self) -> String {
34        encode(&self.0)
35    }
36
37    //Return Hex string of the buffer, Consumes the Hex
38    pub fn into_hex(self) -> String {
39        encode(self.0)
40    }
41}
42
43//Allows us to grab specific bytes from the buffer e.g.
44//grab the merkle tree from the middle of the buffer.
45impl ops::Deref for Buffer {
46    type Target = Vec<u8>;
47
48    fn deref(&self) -> &Self::Target {
49        &self.0
50    }
51}
52
53//Allows us to grab specific bytes from the buffer e.g.
54//grab the merkle tree from the middle of the buffer.
55//Same as above, but allows us to grab those bytes and mutable, thus changing them without
56//having to allocate more mem.
57impl ops::DerefMut for Buffer {
58    fn deref_mut(&mut self) -> &mut Self::Target {
59        &mut self.0
60    }
61}
62
63//Allows Buffer to be used as a reference for a [u8] TODO double check this.
64//And thoroughly comment for everyone
65impl AsRef<[u8]> for Buffer {
66    fn as_ref(&self) -> &[u8] {
67        &self.0
68    }
69}
70
71//Allows Buffer to be used as a mut for a [u8] TODO double check this.
72//And thoroughly comment for everyone
73impl AsMut<[u8]> for Buffer {
74    fn as_mut(&mut self) -> &mut [u8] {
75        &mut self.0
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_write_u32() {
85        let version: u32 = 123456789;
86
87        let mut buffer = Buffer::new();
88
89        buffer.write_u32(version);
90
91        assert_eq!(buffer, Buffer([21, 205, 91, 7].to_vec()));
92    }
93
94    #[test]
95    fn test_write_hash() {
96        let hash = Hash::from("bb42edce1895f9a969e81d7371ec113a0966e5d55035a84f87ca098e4f0a1a86");
97
98        let mut buffer = Buffer::new();
99
100        buffer.write_hash(hash);
101
102        dbg!(buffer);
103    }
104
105    #[test]
106    fn test_to_hex() {
107        let version: u32 = 123456789;
108
109        let mut buffer = Buffer::new();
110
111        buffer.write_u32(version);
112
113        assert_eq!(buffer, Buffer([21, 205, 91, 7].to_vec()));
114
115        let hex = buffer.to_hex();
116
117        assert_eq!(hex, "15cd5b07")
118    }
119
120    #[test]
121    fn test_into_hex() {
122        let version: u32 = 123456789;
123
124        let mut buffer = Buffer::new();
125
126        buffer.write_u32(version);
127
128        assert_eq!(buffer, Buffer([21, 205, 91, 7].to_vec()));
129
130        let hex = buffer.into_hex();
131
132        assert_eq!(hex, "15cd5b07")
133    }
134
135}