projects
/
rust-lightning
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge pull request #1134 from jkczyz/2021-10-payee-arg
[rust-lightning]
/
lightning
/
src
/
util
/
ser.rs
diff --git
a/lightning/src/util/ser.rs
b/lightning/src/util/ser.rs
index 83059620ed3f3382be4b349197efa1b357de72cf..ba8a9fd0d3551efe8062e907f4d52d5f2b635d51 100644
(file)
--- a/
lightning/src/util/ser.rs
+++ b/
lightning/src/util/ser.rs
@@
-35,18
+35,13
@@
use util::byte_utils::{be48_to_array, slice_to_be48};
/// serialization buffer size
pub const MAX_BUF_SIZE: usize = 64 * 1024;
/// serialization buffer size
pub const MAX_BUF_SIZE: usize = 64 * 1024;
-/// A trait that is similar to std::io::Write but has one extra function which can be used to size
-/// buffers being written into.
-/// An impl is provided for any type that also impls std::io::Write which simply ignores size
-/// hints.
+/// A simplified version of std::io::Write that exists largely for backwards compatibility.
+/// An impl is provided for any type that also impls std::io::Write.
///
/// (C-not exported) as we only export serialization to/from byte arrays instead
pub trait Writer {
/// Writes the given buf out. See std::io::Write::write_all for more
fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error>;
///
/// (C-not exported) as we only export serialization to/from byte arrays instead
pub trait Writer {
/// Writes the given buf out. See std::io::Write::write_all for more
fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error>;
- /// Hints that data of the given size is about the be written. This may not always be called
- /// prior to data being written and may be safely ignored.
- fn size_hint(&mut self, size: usize);
}
impl<W: Write> Writer for W {
}
impl<W: Write> Writer for W {
@@
-54,8
+49,6
@@
impl<W: Write> Writer for W {
fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error> {
<Self as io::Write>::write_all(self, buf)
}
fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error> {
<Self as io::Write>::write_all(self, buf)
}
- #[inline]
- fn size_hint(&mut self, _size: usize) { }
}
pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W);
}
pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W);
@@
-82,10
+75,6
@@
impl Writer for VecWriter {
self.0.extend_from_slice(buf);
Ok(())
}
self.0.extend_from_slice(buf);
Ok(())
}
- #[inline]
- fn size_hint(&mut self, size: usize) {
- self.0.reserve_exact(size);
- }
}
/// Writer that only tracks the amount of data written - useful if you need to calculate the length
}
/// Writer that only tracks the amount of data written - useful if you need to calculate the length
@@
-97,8
+86,6
@@
impl Writer for LengthCalculatingWriter {
self.0 += buf.len();
Ok(())
}
self.0 += buf.len();
Ok(())
}
- #[inline]
- fn size_hint(&mut self, _size: usize) {}
}
/// Essentially std::io::Take but a bit simpler and with a method to walk the underlying stream
}
/// Essentially std::io::Take but a bit simpler and with a method to walk the underlying stream
@@
-515,14
+502,20
@@
impl<K, V> Writeable for HashMap<K, V>
impl<K, V> Readable for HashMap<K, V>
where K: Readable + Eq + Hash,
impl<K, V> Readable for HashMap<K, V>
where K: Readable + Eq + Hash,
- V: Readable
+ V:
Maybe
Readable
{
#[inline]
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
let len: u16 = Readable::read(r)?;
let mut ret = HashMap::with_capacity(len as usize);
for _ in 0..len {
{
#[inline]
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
let len: u16 = Readable::read(r)?;
let mut ret = HashMap::with_capacity(len as usize);
for _ in 0..len {
- ret.insert(K::read(r)?, V::read(r)?);
+ let k = K::read(r)?;
+ let v_opt = V::read(r)?;
+ if let Some(v) = v_opt {
+ if ret.insert(k, v).is_some() {
+ return Err(DecodeError::InvalidValue);
+ }
+ }
}
Ok(ret)
}
}
Ok(ret)
}
@@
-892,6
+885,11
@@
impl<A: Writeable, B: Writeable, C: Writeable> Writeable for (A, B, C) {
}
}
}
}
+impl Writeable for () {
+ fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
+ Ok(())
+ }
+}
impl Readable for () {
fn read<R: Read>(_r: &mut R) -> Result<Self, DecodeError> {
Ok(())
impl Readable for () {
fn read<R: Read>(_r: &mut R) -> Result<Self, DecodeError> {
Ok(())
@@
-905,7
+903,6
@@
impl Writeable for String {
w.write_all(self.as_bytes())
}
}
w.write_all(self.as_bytes())
}
}
-
impl Readable for String {
#[inline]
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
impl Readable for String {
#[inline]
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {