Skip to content

Commit da8d4d9

Browse files
committed
Implement HashMap read for MaybeReadable values
This allows us to read a `HashMap` that has values which may be skipped if they are some backwards-compatibility type. We also take this opportunity to fail deserialization if keys are duplicated.
1 parent 2009c23 commit da8d4d9

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lightning/src/util/ser.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,20 @@ impl<K, V> Writeable for HashMap<K, V>
502502

503503
impl<K, V> Readable for HashMap<K, V>
504504
where K: Readable + Eq + Hash,
505-
V: Readable
505+
V: MaybeReadable
506506
{
507507
#[inline]
508508
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
509509
let len: u16 = Readable::read(r)?;
510510
let mut ret = HashMap::with_capacity(len as usize);
511511
for _ in 0..len {
512-
ret.insert(K::read(r)?, V::read(r)?);
512+
let k = K::read(r)?;
513+
let v_opt = V::read(r)?;
514+
if let Some(v) = v_opt {
515+
if ret.insert(k, v).is_some() {
516+
return Err(DecodeError::InvalidValue);
517+
}
518+
}
513519
}
514520
Ok(ret)
515521
}

0 commit comments

Comments
 (0)