3 use lightning::util::ser::Writer;
4 pub struct VecWriter(pub Vec<u8>);
5 impl Writer for VecWriter {
6 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
7 assert!(self.0.capacity() >= self.0.len() + buf.len());
8 self.0.extend_from_slice(buf);
11 fn size_hint(&mut self, size: usize) {
12 self.0.reserve_exact(size);
17 macro_rules! test_msg {
18 ($MsgType: path, $data: ident) => {
20 use lightning::util::ser::{Writeable, Readable};
21 let mut r = ::std::io::Cursor::new($data);
22 if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
23 let p = r.position() as usize;
24 let mut w = VecWriter(Vec::new());
25 msg.write(&mut w).unwrap();
27 assert_eq!(w.0.len(), p);
28 assert_eq!(&r.into_inner()[..p], &w.0[..p]);
35 macro_rules! test_msg_simple {
36 ($MsgType: path, $data: ident) => {
38 use lightning::util::ser::{Writeable, Readable};
39 let mut r = ::std::io::Cursor::new($data);
40 if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
41 let mut w = VecWriter(Vec::new());
42 msg.write(&mut w).unwrap();
49 macro_rules! test_msg_exact {
50 ($MsgType: path, $data: ident) => {
52 use lightning::util::ser::{Writeable, Readable};
53 let mut r = ::std::io::Cursor::new($data);
54 if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
55 let mut w = VecWriter(Vec::new());
56 msg.write(&mut w).unwrap();
58 assert_eq!(&r.into_inner()[..], &w.0[..]);
65 macro_rules! test_msg_hole {
66 ($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
68 use lightning::util::ser::{Writeable, Readable};
69 let mut r = ::std::io::Cursor::new($data);
70 if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
71 let mut w = VecWriter(Vec::new());
72 msg.write(&mut w).unwrap();
73 let p = w.0.len() as usize;
75 assert_eq!(w.0.len(), p);
76 assert_eq!(&r.get_ref()[..$hole], &w.0[..$hole]);
77 assert_eq!(&r.get_ref()[$hole+$hole_len..p], &w.0[$hole+$hole_len..]);