Skip to content

Expose some useful tlv macros #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 changes: 1 addition & 1 deletion lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ pub struct CommitmentUpdate {

/// Messages could have optional fields to use with extended features
/// As we wish to serialize these differently from Option<T>s (Options get a tag byte, but
/// OptionalFeild simply gets Present if there are enough bytes to read into it), we have a
/// OptionalField simply gets Present if there are enough bytes to read into it), we have a
/// separate enum type for them.
/// (C-not exported) due to a free generic in T
#[derive(Clone, Debug, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
pub(crate) mod fuzz_wrappers;

#[macro_use]
pub(crate) mod ser_macros;
pub mod ser_macros;

pub mod events;
pub mod errors;
Expand Down
16 changes: 11 additions & 5 deletions lightning/src/util/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,24 @@ impl Writer for LengthCalculatingWriter {

/// Essentially std::io::Take but a bit simpler and with a method to walk the underlying stream
/// forward to ensure we always consume exactly the fixed length specified.
pub(crate) struct FixedLengthReader<R: Read> {
pub struct FixedLengthReader<R: Read> {
read: R,
bytes_read: u64,
total_bytes: u64,
}
impl<R: Read> FixedLengthReader<R> {
/// Returns a new FixedLengthReader.
pub fn new(read: R, total_bytes: u64) -> Self {
Self { read, bytes_read: 0, total_bytes }
}

/// Returns whether there are remaining bytes or not.
#[inline]
pub fn bytes_remain(&mut self) -> bool {
self.bytes_read != self.total_bytes
}

/// Consume the remaning bytes.
#[inline]
pub fn eat_remaining(&mut self) -> Result<(), DecodeError> {
copy(self, &mut sink()).unwrap();
Expand Down Expand Up @@ -136,11 +139,13 @@ impl<R: Read> Read for FixedLengthReader<R> {

/// A Read which tracks whether any bytes have been read at all. This allows us to distinguish
/// between "EOF reached before we started" and "EOF reached mid-read".
pub(crate) struct ReadTrackingReader<R: Read> {
pub struct ReadTrackingReader<R: Read> {
read: R,
/// Tells whether we have read from this reader or not yet.
pub have_read: bool,
}
impl<R: Read> ReadTrackingReader<R> {
/// Returns a new ReadTrackingReader.
pub fn new(read: R) -> Self {
Self { read, have_read: false }
}
Expand Down Expand Up @@ -237,7 +242,8 @@ impl<T: Readable> MaybeReadable for T {
}
}

pub(crate) struct OptionDeserWrapper<T: Readable>(pub Option<T>);
/// Wrapper to read a required (non-optional) TLV record.
pub struct OptionDeserWrapper<T: Readable>(pub Option<T>);
impl<T: Readable> Readable for OptionDeserWrapper<T> {
#[inline]
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
Expand All @@ -246,7 +252,7 @@ impl<T: Readable> Readable for OptionDeserWrapper<T> {
}

/// Wrapper to write each element of a Vec with no length prefix
pub(crate) struct VecWriteWrapper<'a, T: Writeable>(pub &'a Vec<T>);
pub struct VecWriteWrapper<'a, T: Writeable>(pub &'a Vec<T>);
impl<'a, T: Writeable> Writeable for VecWriteWrapper<'a, T> {
#[inline]
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
Expand All @@ -258,7 +264,7 @@ impl<'a, T: Writeable> Writeable for VecWriteWrapper<'a, T> {
}

/// Wrapper to read elements from a given stream until it reaches the end of the stream.
pub(crate) struct VecReadWrapper<T>(pub Vec<T>);
pub struct VecReadWrapper<T>(pub Vec<T>);
impl<T: MaybeReadable> Readable for VecReadWrapper<T> {
#[inline]
fn read<R: Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
Expand Down
Loading