}
//TODO: performance issue with [u8; size] with impl_array!()
+impl_array!(3); // for rgb
+impl_array!(4); // for IPv4
+impl_array!(10); // for OnionV2
+impl_array!(16); // for IPv6
impl_array!(32); // for channel id & hmac
impl_array!(33); // for PublicKey
impl_array!(64); // for Signature
}
}
-impl Writeable for Option<Script> {
- fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
- if let &Some(ref script) = self {
- script.write(w)?;
- }
- Ok(())
- }
-}
-
-impl<R: Read> Readable<R> for Option<Script> {
- fn read(r: &mut R) -> Result<Self, DecodeError> {
- match <u16 as Readable<R>>::read(r) {
- Ok(len) => {
- let mut buf = vec![0; len as usize];
- r.read_exact(&mut buf)?;
- Ok(Some(Script::from(buf)))
- },
- Err(DecodeError::ShortRead) => Ok(None),
- Err(e) => Err(e)
- }
- }
-}
-
impl Writeable for PublicKey {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
self.serialize().write(w)
Ok(PaymentHash(buf))
}
}
+
+impl<T: Writeable> Writeable for Option<T> {
+ fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+ match *self {
+ None => 0u8.write(w)?,
+ Some(ref data) => {
+ 1u8.write(w)?;
+ data.write(w)?;
+ }
+ }
+ Ok(())
+ }
+}
+
+impl<R, T> Readable<R> for Option<T>
+ where R: Read,
+ T: Readable<R>
+{
+ fn read(r: &mut R) -> Result<Self, DecodeError> {
+ match <u8 as Readable<R>>::read(r)? {
+ 0 => Ok(None),
+ 1 => Ok(Some(Readable::read(r)?)),
+ _ => return Err(DecodeError::InvalidValue),
+ }
+ }
+}