1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! A very simple serialization framework which is used to serialize/deserialize messages as well
11 //! as ChannelsManagers and ChannelMonitors.
14 use std::io::{Read, Write};
15 use std::collections::HashMap;
20 use bitcoin::secp256k1::Signature;
21 use bitcoin::secp256k1::key::{PublicKey, SecretKey};
22 use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, COMPACT_SIGNATURE_SIZE};
23 use bitcoin::blockdata::script::Script;
24 use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut};
25 use bitcoin::consensus;
26 use bitcoin::consensus::Encodable;
27 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
28 use bitcoin::hash_types::{Txid, BlockHash};
29 use core::marker::Sized;
30 use ln::msgs::DecodeError;
31 use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
33 use util::byte_utils::{be48_to_array, slice_to_be48};
35 /// serialization buffer size
36 pub const MAX_BUF_SIZE: usize = 64 * 1024;
38 /// A trait that is similar to std::io::Write but has one extra function which can be used to size
39 /// buffers being written into.
40 /// An impl is provided for any type that also impls std::io::Write which simply ignores size
43 /// (C-not exported) as we only export serialization to/from byte arrays instead
45 /// Writes the given buf out. See std::io::Write::write_all for more
46 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>;
47 /// Hints that data of the given size is about the be written. This may not always be called
48 /// prior to data being written and may be safely ignored.
49 fn size_hint(&mut self, size: usize);
52 impl<W: Write> Writer for W {
54 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
55 <Self as ::std::io::Write>::write_all(self, buf)
58 fn size_hint(&mut self, _size: usize) { }
61 pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W);
62 impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> {
64 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
68 fn write(&mut self, buf: &[u8]) -> Result<usize, ::std::io::Error> {
69 self.0.write_all(buf)?;
73 fn flush(&mut self) -> Result<(), ::std::io::Error> {
78 pub(crate) struct VecWriter(pub Vec<u8>);
79 impl Writer for VecWriter {
81 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
82 self.0.extend_from_slice(buf);
86 fn size_hint(&mut self, size: usize) {
87 self.0.reserve_exact(size);
91 /// Writer that only tracks the amount of data written - useful if you need to calculate the length
92 /// of some data when serialized but don't yet need the full data.
93 pub(crate) struct LengthCalculatingWriter(pub usize);
94 impl Writer for LengthCalculatingWriter {
96 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
101 fn size_hint(&mut self, _size: usize) {}
104 /// Essentially std::io::Take but a bit simpler and with a method to walk the underlying stream
105 /// forward to ensure we always consume exactly the fixed length specified.
106 pub(crate) struct FixedLengthReader<R: Read> {
111 impl<R: Read> FixedLengthReader<R> {
112 pub fn new(read: R, total_bytes: u64) -> Self {
113 Self { read, bytes_read: 0, total_bytes }
117 pub fn bytes_remain(&mut self) -> bool {
118 self.bytes_read != self.total_bytes
122 pub fn eat_remaining(&mut self) -> Result<(), DecodeError> {
123 ::std::io::copy(self, &mut ::std::io::sink()).unwrap();
124 if self.bytes_read != self.total_bytes {
125 Err(DecodeError::ShortRead)
131 impl<R: Read> Read for FixedLengthReader<R> {
133 fn read(&mut self, dest: &mut [u8]) -> Result<usize, ::std::io::Error> {
134 if self.total_bytes == self.bytes_read {
137 let read_len = cmp::min(dest.len() as u64, self.total_bytes - self.bytes_read);
138 match self.read.read(&mut dest[0..(read_len as usize)]) {
140 self.bytes_read += v as u64;
149 /// A Read which tracks whether any bytes have been read at all. This allows us to distinguish
150 /// between "EOF reached before we started" and "EOF reached mid-read".
151 pub(crate) struct ReadTrackingReader<R: Read> {
155 impl<R: Read> ReadTrackingReader<R> {
156 pub fn new(read: R) -> Self {
157 Self { read, have_read: false }
160 impl<R: Read> Read for ReadTrackingReader<R> {
162 fn read(&mut self, dest: &mut [u8]) -> Result<usize, ::std::io::Error> {
163 match self.read.read(dest) {
166 self.have_read = true;
174 /// A trait that various rust-lightning types implement allowing them to be written out to a Writer
176 /// (C-not exported) as we only export serialization to/from byte arrays instead
177 pub trait Writeable {
178 /// Writes self out to the given Writer
179 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
181 /// Writes self out to a Vec<u8>
182 fn encode(&self) -> Vec<u8> {
183 let mut msg = VecWriter(Vec::new());
184 self.write(&mut msg).unwrap();
188 /// Writes self out to a Vec<u8>
189 fn encode_with_len(&self) -> Vec<u8> {
190 let mut msg = VecWriter(Vec::new());
191 0u16.write(&mut msg).unwrap();
192 self.write(&mut msg).unwrap();
193 let len = msg.0.len();
194 msg.0[..2].copy_from_slice(&(len as u16 - 2).to_be_bytes());
198 /// Gets the length of this object after it has been serialized. This can be overridden to
199 /// optimize cases where we prepend an object with its length.
200 // Note that LLVM optimizes this away in most cases! Check that it isn't before you override!
202 fn serialized_length(&self) -> usize {
203 let mut len_calc = LengthCalculatingWriter(0);
204 self.write(&mut len_calc).expect("No in-memory data may fail to serialize");
209 impl<'a, T: Writeable> Writeable for &'a T {
210 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> { (*self).write(writer) }
213 /// A trait that various rust-lightning types implement allowing them to be read in from a Read
215 /// (C-not exported) as we only export serialization to/from byte arrays instead
219 /// Reads a Self in from the given Read
220 fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError>;
223 /// A trait that various higher-level rust-lightning types implement allowing them to be read in
224 /// from a Read given some additional set of arguments which is required to deserialize.
226 /// (C-not exported) as we only export serialization to/from byte arrays instead
227 pub trait ReadableArgs<P>
230 /// Reads a Self in from the given Read
231 fn read<R: Read>(reader: &mut R, params: P) -> Result<Self, DecodeError>;
234 /// A trait that various rust-lightning types implement allowing them to (maybe) be read in from a Read
236 /// (C-not exported) as we only export serialization to/from byte arrays instead
237 pub trait MaybeReadable
240 /// Reads a Self in from the given Read
241 fn read<R: Read>(reader: &mut R) -> Result<Option<Self>, DecodeError>;
244 pub(crate) struct OptionDeserWrapper<T: Readable>(pub Option<T>);
245 impl<T: Readable> Readable for OptionDeserWrapper<T> {
247 fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
248 Ok(Self(Some(Readable::read(reader)?)))
252 /// Wrapper to write each element of a Vec with no length prefix
253 pub(crate) struct VecWriteWrapper<'a, T: Writeable>(pub &'a Vec<T>);
254 impl<'a, T: Writeable> Writeable for VecWriteWrapper<'a, T> {
256 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
257 for ref v in self.0.iter() {
263 /// Wrapper to read elements from a given stream until it reaches the end of the stream.
264 pub(crate) struct VecReadWrapper<T: Readable>(pub Vec<T>);
265 impl<T: Readable> Readable for VecReadWrapper<T> {
267 fn read<R: Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
268 let mut values = Vec::new();
270 let mut track_read = ReadTrackingReader::new(&mut reader);
271 match Readable::read(&mut track_read) {
272 Ok(v) => { values.push(v); },
273 // If we failed to read any bytes at all, we reached the end of our TLV
274 // stream and have simply exhausted all entries.
275 Err(ref e) if e == &DecodeError::ShortRead && !track_read.have_read => break,
276 Err(e) => return Err(e),
283 pub(crate) struct U48(pub u64);
284 impl Writeable for U48 {
286 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
287 writer.write_all(&be48_to_array(self.0))
290 impl Readable for U48 {
292 fn read<R: Read>(reader: &mut R) -> Result<U48, DecodeError> {
293 let mut buf = [0; 6];
294 reader.read_exact(&mut buf)?;
295 Ok(U48(slice_to_be48(&buf)))
299 /// Lightning TLV uses a custom variable-length integer called BigSize. It is similar to Bitcoin's
300 /// variable-length integers except that it is serialized in big-endian instead of little-endian.
302 /// Like Bitcoin's variable-length integer, it exhibits ambiguity in that certain values can be
303 /// encoded in several different ways, which we must check for at deserialization-time. Thus, if
304 /// you're looking for an example of a variable-length integer to use for your own project, move
305 /// along, this is a rather poor design.
306 pub(crate) struct BigSize(pub u64);
307 impl Writeable for BigSize {
309 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
312 (self.0 as u8).write(writer)
315 0xFDu8.write(writer)?;
316 (self.0 as u16).write(writer)
318 0x10000...0xFFFFFFFF => {
319 0xFEu8.write(writer)?;
320 (self.0 as u32).write(writer)
323 0xFFu8.write(writer)?;
324 (self.0 as u64).write(writer)
329 impl Readable for BigSize {
331 fn read<R: Read>(reader: &mut R) -> Result<BigSize, DecodeError> {
332 let n: u8 = Readable::read(reader)?;
335 let x: u64 = Readable::read(reader)?;
337 Err(DecodeError::InvalidValue)
343 let x: u32 = Readable::read(reader)?;
345 Err(DecodeError::InvalidValue)
347 Ok(BigSize(x as u64))
351 let x: u16 = Readable::read(reader)?;
353 Err(DecodeError::InvalidValue)
355 Ok(BigSize(x as u64))
358 n => Ok(BigSize(n as u64))
363 /// In TLV we occasionally send fields which only consist of, or potentially end with, a
364 /// variable-length integer which is simply truncated by skipping high zero bytes. This type
365 /// encapsulates such integers implementing Readable/Writeable for them.
366 #[cfg_attr(test, derive(PartialEq, Debug))]
367 pub(crate) struct HighZeroBytesDroppedVarInt<T>(pub T);
369 macro_rules! impl_writeable_primitive {
370 ($val_type:ty, $len: expr) => {
371 impl Writeable for $val_type {
373 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
374 writer.write_all(&self.to_be_bytes())
377 impl Writeable for HighZeroBytesDroppedVarInt<$val_type> {
379 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
380 // Skip any full leading 0 bytes when writing (in BE):
381 writer.write_all(&self.0.to_be_bytes()[(self.0.leading_zeros()/8) as usize..$len])
384 impl Readable for $val_type {
386 fn read<R: Read>(reader: &mut R) -> Result<$val_type, DecodeError> {
387 let mut buf = [0; $len];
388 reader.read_exact(&mut buf)?;
389 Ok(<$val_type>::from_be_bytes(buf))
392 impl Readable for HighZeroBytesDroppedVarInt<$val_type> {
394 fn read<R: Read>(reader: &mut R) -> Result<HighZeroBytesDroppedVarInt<$val_type>, DecodeError> {
395 // We need to accept short reads (read_len == 0) as "EOF" and handle them as simply
396 // the high bytes being dropped. To do so, we start reading into the middle of buf
397 // and then convert the appropriate number of bytes with extra high bytes out of
399 let mut buf = [0; $len*2];
400 let mut read_len = reader.read(&mut buf[$len..])?;
401 let mut total_read_len = read_len;
402 while read_len != 0 && total_read_len != $len {
403 read_len = reader.read(&mut buf[($len + total_read_len)..])?;
404 total_read_len += read_len;
406 if total_read_len == 0 || buf[$len] != 0 {
407 let first_byte = $len - ($len - total_read_len);
408 let mut bytes = [0; $len];
409 bytes.copy_from_slice(&buf[first_byte..first_byte + $len]);
410 Ok(HighZeroBytesDroppedVarInt(<$val_type>::from_be_bytes(bytes)))
412 // If the encoding had extra zero bytes, return a failure even though we know
413 // what they meant (as the TLV test vectors require this)
414 Err(DecodeError::InvalidValue)
421 impl_writeable_primitive!(u64, 8);
422 impl_writeable_primitive!(u32, 4);
423 impl_writeable_primitive!(u16, 2);
425 impl Writeable for u8 {
427 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
428 writer.write_all(&[*self])
431 impl Readable for u8 {
433 fn read<R: Read>(reader: &mut R) -> Result<u8, DecodeError> {
434 let mut buf = [0; 1];
435 reader.read_exact(&mut buf)?;
440 impl Writeable for bool {
442 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
443 writer.write_all(&[if *self {1} else {0}])
446 impl Readable for bool {
448 fn read<R: Read>(reader: &mut R) -> Result<bool, DecodeError> {
449 let mut buf = [0; 1];
450 reader.read_exact(&mut buf)?;
451 if buf[0] != 0 && buf[0] != 1 {
452 return Err(DecodeError::InvalidValue);
459 macro_rules! impl_array {
461 impl Writeable for [u8; $size]
464 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
469 impl Readable for [u8; $size]
472 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
473 let mut buf = [0u8; $size];
474 r.read_exact(&mut buf)?;
481 //TODO: performance issue with [u8; size] with impl_array!()
482 impl_array!(3); // for rgb
483 impl_array!(4); // for IPv4
484 impl_array!(10); // for OnionV2
485 impl_array!(16); // for IPv6
486 impl_array!(32); // for channel id & hmac
487 impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
488 impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
489 impl_array!(1300); // for OnionPacket.hop_data
492 impl<K, V> Writeable for HashMap<K, V>
493 where K: Writeable + Eq + Hash,
497 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
498 (self.len() as u16).write(w)?;
499 for (key, value) in self.iter() {
507 impl<K, V> Readable for HashMap<K, V>
508 where K: Readable + Eq + Hash,
512 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
513 let len: u16 = Readable::read(r)?;
514 let mut ret = HashMap::with_capacity(len as usize);
516 ret.insert(K::read(r)?, V::read(r)?);
523 impl Writeable for Vec<u8> {
525 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
526 (self.len() as u16).write(w)?;
531 impl Readable for Vec<u8> {
533 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
534 let len: u16 = Readable::read(r)?;
535 let mut ret = Vec::with_capacity(len as usize);
536 ret.resize(len as usize, 0);
537 r.read_exact(&mut ret)?;
541 impl Writeable for Vec<Signature> {
543 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
544 (self.len() as u16).write(w)?;
545 for e in self.iter() {
552 impl Readable for Vec<Signature> {
554 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
555 let len: u16 = Readable::read(r)?;
556 let byte_size = (len as usize)
557 .checked_mul(COMPACT_SIGNATURE_SIZE)
558 .ok_or(DecodeError::BadLengthDescriptor)?;
559 if byte_size > MAX_BUF_SIZE {
560 return Err(DecodeError::BadLengthDescriptor);
562 let mut ret = Vec::with_capacity(len as usize);
563 for _ in 0..len { ret.push(Signature::read(r)?); }
568 impl Writeable for Script {
569 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
570 (self.len() as u16).write(w)?;
571 w.write_all(self.as_bytes())
575 impl Readable for Script {
576 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
577 let len = <u16 as Readable>::read(r)? as usize;
578 let mut buf = vec![0; len];
579 r.read_exact(&mut buf)?;
580 Ok(Script::from(buf))
584 impl Writeable for PublicKey {
585 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
586 self.serialize().write(w)
589 fn serialized_length(&self) -> usize {
594 impl Readable for PublicKey {
595 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
596 let buf: [u8; PUBLIC_KEY_SIZE] = Readable::read(r)?;
597 match PublicKey::from_slice(&buf) {
599 Err(_) => return Err(DecodeError::InvalidValue),
604 impl Writeable for SecretKey {
605 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
606 let mut ser = [0; SECRET_KEY_SIZE];
607 ser.copy_from_slice(&self[..]);
611 fn serialized_length(&self) -> usize {
616 impl Readable for SecretKey {
617 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
618 let buf: [u8; SECRET_KEY_SIZE] = Readable::read(r)?;
619 match SecretKey::from_slice(&buf) {
621 Err(_) => return Err(DecodeError::InvalidValue),
626 impl Writeable for Sha256dHash {
627 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
628 w.write_all(&self[..])
632 impl Readable for Sha256dHash {
633 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
634 use bitcoin::hashes::Hash;
636 let buf: [u8; 32] = Readable::read(r)?;
637 Ok(Sha256dHash::from_slice(&buf[..]).unwrap())
641 impl Writeable for Signature {
642 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
643 self.serialize_compact().write(w)
646 fn serialized_length(&self) -> usize {
647 COMPACT_SIGNATURE_SIZE
651 impl Readable for Signature {
652 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
653 let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
654 match Signature::from_compact(&buf) {
656 Err(_) => return Err(DecodeError::InvalidValue),
661 impl Writeable for PaymentPreimage {
662 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
667 impl Readable for PaymentPreimage {
668 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
669 let buf: [u8; 32] = Readable::read(r)?;
670 Ok(PaymentPreimage(buf))
674 impl Writeable for PaymentHash {
675 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
680 impl Readable for PaymentHash {
681 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
682 let buf: [u8; 32] = Readable::read(r)?;
687 impl Writeable for PaymentSecret {
688 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
693 impl Readable for PaymentSecret {
694 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
695 let buf: [u8; 32] = Readable::read(r)?;
696 Ok(PaymentSecret(buf))
700 impl<T: Writeable> Writeable for Option<T> {
701 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
703 None => 0u8.write(w)?,
705 BigSize(data.serialized_length() as u64 + 1).write(w)?;
713 impl<T: Readable> Readable for Option<T>
715 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
716 match BigSize::read(r)?.0 {
719 let mut reader = FixedLengthReader::new(r, len - 1);
720 Ok(Some(Readable::read(&mut reader)?))
726 impl Writeable for Txid {
727 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
728 w.write_all(&self[..])
732 impl Readable for Txid {
733 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
734 use bitcoin::hashes::Hash;
736 let buf: [u8; 32] = Readable::read(r)?;
737 Ok(Txid::from_slice(&buf[..]).unwrap())
741 impl Writeable for BlockHash {
742 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
743 w.write_all(&self[..])
747 impl Readable for BlockHash {
748 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
749 use bitcoin::hashes::Hash;
751 let buf: [u8; 32] = Readable::read(r)?;
752 Ok(BlockHash::from_slice(&buf[..]).unwrap())
756 impl Writeable for OutPoint {
757 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
764 impl Readable for OutPoint {
765 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
766 let txid = Readable::read(r)?;
767 let vout = Readable::read(r)?;
775 macro_rules! impl_consensus_ser {
776 ($bitcoin_type: ty) => {
777 impl Writeable for $bitcoin_type {
778 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
779 match self.consensus_encode(WriterWriteAdaptor(writer)) {
786 impl Readable for $bitcoin_type {
787 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
788 match consensus::encode::Decodable::consensus_decode(r) {
790 Err(consensus::encode::Error::Io(ref e)) if e.kind() == ::std::io::ErrorKind::UnexpectedEof => Err(DecodeError::ShortRead),
791 Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e.kind())),
792 Err(_) => Err(DecodeError::InvalidValue),
798 impl_consensus_ser!(Transaction);
799 impl_consensus_ser!(TxOut);
801 impl<T: Readable> Readable for Mutex<T> {
802 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
803 let t: T = Readable::read(r)?;
807 impl<T: Writeable> Writeable for Mutex<T> {
808 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
809 self.lock().unwrap().write(w)
813 impl<A: Readable, B: Readable> Readable for (A, B) {
814 fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
815 let a: A = Readable::read(r)?;
816 let b: B = Readable::read(r)?;
820 impl<A: Writeable, B: Writeable> Writeable for (A, B) {
821 fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {