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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
pub mod feature;
pub mod fees;
pub mod hash;

use crate::{TLVWireItem, WireItem};
use std::borrow::Borrow;
use std::io::{Read, Write};

macro_rules! impl_wire_item_for_nums {
    (
        $(
            $(#[$attr:meta])*
            $num_ty:ty[$bytes:literal],
        )*
    ) => {
        $(
            $(#[$attr])*
            impl WireItem for $num_ty {
                fn encode<W: Write>(&self, w: &mut W) -> std::io::Result<usize> {
                    w.write(&<$num_ty>::to_be_bytes(*self))
                }

                fn decode<R: Read>(r: &mut R) -> std::io::Result<Self> {
                   let mut buf = [0_u8; $bytes];
                   r.read_exact(&mut buf)?;
                    Ok(<$num_ty>::from_be_bytes(buf))
                }
            }
        )*
    };
}

macro_rules! impl_wire_item_for_byte_array {
    (
        $(
            $(#[$attr:meta])*
            [u8; $bytes:literal],
        )*
    ) => {
        $(
            $(#[$attr])*
            impl WireItem for [u8; $bytes] {
                fn encode<W: Write>(&self, w: &mut W) -> std::io::Result<usize> {
                    w.write(self)
                }

                fn decode<R: Read>(r: &mut R) -> std::io::Result<Self> {
                   let mut buf = [0_u8; $bytes];
                   r.read_exact(&mut buf)?;
                    Ok(buf)
                }
            }
        )*
    };
}

impl_wire_item_for_nums!(
    u8[1],
    i8[1],
    u16[2],
    i16[2],
    u32[4],
    i32[4],
    u64[8],
    i64[8],
    u128[16],
    i128[16],
    #[cfg(target_pointer_width = "16")]
    usize[2],
    #[cfg(target_pointer_width = "16")]
    isize[2],
    #[cfg(target_pointer_width = "32")]
    usize[4],
    #[cfg(target_pointer_width = "32")]
    isize[4],
    #[cfg(target_pointer_width = "64")]
    usize[8],
    #[cfg(target_pointer_width = "64")]
    isize[8],
);

impl_wire_item_for_byte_array!([u8; 16],);

pub enum MaybeOwned<'a, O: Borrow<B>, B> {
    Owned(O),
    Borrowed(&'a B),
}
impl<'a, O, B> Borrow<B> for MaybeOwned<'a, O, B>
where
    O: Borrow<B>,
{
    fn borrow(&self) -> &B {
        match self {
            MaybeOwned::Owned(o) => o.borrow(),
            MaybeOwned::Borrowed(b) => b,
        }
    }
}
impl<'a, O, B> WireItem for MaybeOwned<'a, O, B>
where
    O: Borrow<B> + WireItem,
    B: crate::WireItemWriter,
{
    fn encode<W: Write>(&self, w: &mut W) -> std::io::Result<usize> {
        Borrow::<B>::borrow(self).encode(w)
    }

    fn decode<R: Read>(r: &mut R) -> std::io::Result<Self> {
        Ok(MaybeOwned::Owned(O::decode(r)?))
    }
}

impl WireItem for () {
    fn encode<W: Write>(&self, _: &mut W) -> std::io::Result<usize> {
        Ok(0)
    }

    fn decode<R: Read>(_: &mut R) -> std::io::Result<Self> {
        Ok(())
    }
}

#[derive(Clone, Debug)]
pub enum Buffer<T: Borrow<[u8]>> {
    Vector(Vec<u8>),
    Other(T),
}
impl<T> Buffer<T>
where
    T: Borrow<[u8]>,
{
    // Clones if not Vec variant
    pub fn to_vec(self) -> Vec<u8> {
        match self {
            Buffer::Vector(a) => a,
            Buffer::Other(a) => a.borrow().to_vec(),
        }
    }
}
impl<T> Borrow<[u8]> for Buffer<T>
where
    T: Borrow<[u8]>,
{
    fn borrow(&self) -> &[u8] {
        match self {
            Buffer::Vector(a) => a.borrow(),
            Buffer::Other(a) => a.borrow(),
        }
    }
}
impl<T> From<T> for Buffer<T>
where
    T: Borrow<[u8]>,
{
    fn from(t: T) -> Self {
        Buffer::Other(t)
    }
}
impl<T> WireItem for Buffer<T>
where
    T: Borrow<[u8]>,
{
    fn encode<W: Write>(&self, w: &mut W) -> std::io::Result<usize> {
        let mut count = 0;
        let slice: &[u8] = self.borrow();
        count += crate::write_varint(slice.len() as u64, w)?;
        count += w.write(slice)?;
        Ok(count)
    }

    fn decode<R: Read>(r: &mut R) -> std::io::Result<Self> {
        let len = crate::read_varint(r)? as usize;
        let mut buf = vec![0_u8; len];
        r.read_exact(&mut buf)?;
        Ok(Buffer::Vector(buf))
    }
}

#[derive(Clone, Debug)]
pub enum TLVBuffer<T: Borrow<[u8]>> {
    Vector(Vec<u8>),
    Other(T),
}
impl<T> Borrow<[u8]> for TLVBuffer<T>
where
    T: Borrow<[u8]>,
{
    fn borrow(&self) -> &[u8] {
        match self {
            TLVBuffer::Vector(a) => a.borrow(),
            TLVBuffer::Other(a) => a.borrow(),
        }
    }
}
impl<T> From<T> for TLVBuffer<T>
where
    T: Borrow<[u8]>,
{
    fn from(t: T) -> Self {
        TLVBuffer::Other(t)
    }
}
impl<T> TLVWireItem for TLVBuffer<T>
where
    T: Borrow<[u8]>,
{
    fn encode<W: Write>(&self, w: &mut W) -> std::io::Result<usize> {
        w.write(self.borrow())
    }

    fn decode<R: Read>(r: &mut R, len: usize) -> std::io::Result<Self> {
        let mut buf = vec![0_u8; len];
        r.read_exact(&mut buf)?;
        Ok(TLVBuffer::Vector(buf))
    }
}