]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Drop Seek and SeekFrom usage.
authorArik Sosman <git@arik.io>
Thu, 15 Aug 2024 19:22:59 +0000 (12:22 -0700)
committerArik Sosman <git@arik.io>
Fri, 16 Aug 2024 16:23:09 +0000 (09:23 -0700)
In lieu of using `Seek` and `SeekFrom`, we will instead rely
exclusively on the `io::Cursor` type.

lightning/src/offers/invoice.rs
lightning/src/offers/invoice_request.rs
lightning/src/offers/parse.rs
lightning/src/offers/refund.rs
lightning/src/offers/static_invoice.rs
lightning/src/util/ser.rs
lightning/src/util/ser_macros.rs

index 835a077f294b97a3b60b19e22d5e529af052430c..39423f94c810f5a971c21e20bd8a4cfa931d89a6 100644 (file)
@@ -125,7 +125,7 @@ use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage}
 use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef};
 use crate::offers::refund::{IV_BYTES_WITH_METADATA as REFUND_IV_BYTES_WITH_METADATA, IV_BYTES_WITHOUT_METADATA as REFUND_IV_BYTES_WITHOUT_METADATA, Refund, RefundContents};
 use crate::offers::signer::{Metadata, self};
-use crate::util::ser::{HighZeroBytesDroppedBigSize, Iterable, Readable, SeekReadable, WithoutLength, Writeable, Writer};
+use crate::util::ser::{CursorReadable, HighZeroBytesDroppedBigSize, Iterable, Readable, WithoutLength, Writeable, Writer};
 use crate::util::string::PrintableString;
 
 #[allow(unused_imports)]
@@ -1266,13 +1266,13 @@ type FullInvoiceTlvStreamRef<'a> = (
        SignatureTlvStreamRef<'a>,
 );
 
-impl SeekReadable for FullInvoiceTlvStream {
-       fn read<R: io::Read + io::Seek>(r: &mut R) -> Result<Self, DecodeError> {
-               let payer = SeekReadable::read(r)?;
-               let offer = SeekReadable::read(r)?;
-               let invoice_request = SeekReadable::read(r)?;
-               let invoice = SeekReadable::read(r)?;
-               let signature = SeekReadable::read(r)?;
+impl CursorReadable for FullInvoiceTlvStream {
+       fn read<R: AsRef<[u8]>>(r: &mut io::Cursor<R>) -> Result<Self, DecodeError> {
+               let payer = CursorReadable::read(r)?;
+               let offer = CursorReadable::read(r)?;
+               let invoice_request = CursorReadable::read(r)?;
+               let invoice = CursorReadable::read(r)?;
+               let signature = CursorReadable::read(r)?;
 
                Ok((payer, offer, invoice_request, invoice, signature))
        }
@@ -1288,12 +1288,12 @@ type PartialInvoiceTlvStreamRef<'a> = (
        InvoiceTlvStreamRef<'a>,
 );
 
-impl SeekReadable for PartialInvoiceTlvStream {
-       fn read<R: io::Read + io::Seek>(r: &mut R) -> Result<Self, DecodeError> {
-               let payer = SeekReadable::read(r)?;
-               let offer = SeekReadable::read(r)?;
-               let invoice_request = SeekReadable::read(r)?;
-               let invoice = SeekReadable::read(r)?;
+impl CursorReadable for PartialInvoiceTlvStream {
+       fn read<R: AsRef<[u8]>>(r: &mut io::Cursor<R>) -> Result<Self, DecodeError> {
+               let payer = CursorReadable::read(r)?;
+               let offer = CursorReadable::read(r)?;
+               let invoice_request = CursorReadable::read(r)?;
+               let invoice = CursorReadable::read(r)?;
 
                Ok((payer, offer, invoice_request, invoice))
        }
index 6ed9b2a1295086f6598a404047df0667ad527027..f8815beb6d3bf7c456685a406161a37815014ed6 100644 (file)
@@ -75,7 +75,7 @@ use crate::offers::offer::{Offer, OfferContents, OfferId, OfferTlvStream, OfferT
 use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError};
 use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
 use crate::offers::signer::{Metadata, MetadataMaterial};
-use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, SeekReadable, WithoutLength, Writeable, Writer};
+use crate::util::ser::{CursorReadable, HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer};
 use crate::util::string::{PrintableString, UntrustedString};
 
 #[cfg(not(c_bindings))]
@@ -1070,12 +1070,12 @@ type FullInvoiceRequestTlvStreamRef<'a> = (
        SignatureTlvStreamRef<'a>,
 );
 
-impl SeekReadable for FullInvoiceRequestTlvStream {
-       fn read<R: io::Read + io::Seek>(r: &mut R) -> Result<Self, DecodeError> {
-               let payer = SeekReadable::read(r)?;
-               let offer = SeekReadable::read(r)?;
-               let invoice_request = SeekReadable::read(r)?;
-               let signature = SeekReadable::read(r)?;
+impl CursorReadable for FullInvoiceRequestTlvStream {
+       fn read<R: AsRef<[u8]>>(r: &mut io::Cursor<R>) -> Result<Self, DecodeError> {
+               let payer = CursorReadable::read(r)?;
+               let offer = CursorReadable::read(r)?;
+               let invoice_request = CursorReadable::read(r)?;
+               let signature = CursorReadable::read(r)?;
 
                Ok((payer, offer, invoice_request, signature))
        }
index c48d745a9ffd365ecf09800bc6b6bd24d6e988b5..8b9f64d8b3902944f99b385e9b9fab935781b306 100644 (file)
@@ -12,7 +12,7 @@
 use bitcoin::secp256k1;
 use crate::io;
 use crate::ln::msgs::DecodeError;
-use crate::util::ser::SeekReadable;
+use crate::util::ser::CursorReadable;
 
 #[allow(unused_imports)]
 use crate::prelude::*;
@@ -91,17 +91,17 @@ mod sealed {
 
 /// A wrapper for reading a message as a TLV stream `T` from a byte sequence, while still
 /// maintaining ownership of the bytes for later use.
-pub(super) struct ParsedMessage<T: SeekReadable> {
+pub(super) struct ParsedMessage<T: CursorReadable> {
        pub bytes: Vec<u8>,
        pub tlv_stream: T,
 }
 
-impl<T: SeekReadable> TryFrom<Vec<u8>> for ParsedMessage<T> {
+impl<T: CursorReadable> TryFrom<Vec<u8>> for ParsedMessage<T> {
        type Error = DecodeError;
 
        fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
                let mut cursor = io::Cursor::new(bytes);
-               let tlv_stream: T = SeekReadable::read(&mut cursor)?;
+               let tlv_stream: T = CursorReadable::read(&mut cursor)?;
 
                // Ensure that there are no more TLV records left to parse.
                if cursor.position() < cursor.get_ref().len() as u64 {
index 10f56f0a14ce638f50766eea5f157cfde1f443de..0795bbadc6471c553956dae61e2bc5e7f6b84bdc 100644 (file)
@@ -104,7 +104,7 @@ use crate::offers::offer::{OfferTlvStream, OfferTlvStreamRef};
 use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
 use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
 use crate::offers::signer::{Metadata, MetadataMaterial, self};
-use crate::util::ser::{SeekReadable, Readable, WithoutLength, Writeable, Writer};
+use crate::util::ser::{CursorReadable, Readable, WithoutLength, Writeable, Writer};
 use crate::util::string::PrintableString;
 
 #[cfg(not(c_bindings))]
@@ -801,11 +801,11 @@ type RefundTlvStreamRef<'a> = (
        InvoiceRequestTlvStreamRef<'a>,
 );
 
-impl SeekReadable for RefundTlvStream {
-       fn read<R: io::Read + io::Seek>(r: &mut R) -> Result<Self, DecodeError> {
-               let payer = SeekReadable::read(r)?;
-               let offer = SeekReadable::read(r)?;
-               let invoice_request = SeekReadable::read(r)?;
+impl CursorReadable for RefundTlvStream {
+       fn read<R: AsRef<[u8]>>(r: &mut io::Cursor<R>) -> Result<Self, DecodeError> {
+               let payer = CursorReadable::read(r)?;
+               let offer = CursorReadable::read(r)?;
+               let invoice_request = CursorReadable::read(r)?;
 
                Ok((payer, offer, invoice_request))
        }
index 614903bdfcc1f125671e58aaa418d4a5c7239282..93014fce22aa7ca1f9b1540b7b1d06f9313808ce 100644 (file)
@@ -27,7 +27,7 @@ use crate::offers::offer::{
        Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, Quantity,
 };
 use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
-use crate::util::ser::{Iterable, SeekReadable, WithoutLength, Writeable, Writer};
+use crate::util::ser::{CursorReadable, Iterable, WithoutLength, Writeable, Writer};
 use crate::util::string::PrintableString;
 use bitcoin::address::Address;
 use bitcoin::constants::ChainHash;
@@ -457,11 +457,11 @@ impl TryFrom<Vec<u8>> for StaticInvoice {
 
 type FullInvoiceTlvStream = (OfferTlvStream, InvoiceTlvStream, SignatureTlvStream);
 
-impl SeekReadable for FullInvoiceTlvStream {
-       fn read<R: io::Read + io::Seek>(r: &mut R) -> Result<Self, DecodeError> {
-               let offer = SeekReadable::read(r)?;
-               let invoice = SeekReadable::read(r)?;
-               let signature = SeekReadable::read(r)?;
+impl CursorReadable for FullInvoiceTlvStream {
+       fn read<R: AsRef<[u8]>>(r: &mut io::Cursor<R>) -> Result<Self, DecodeError> {
+               let offer = CursorReadable::read(r)?;
+               let invoice = CursorReadable::read(r)?;
+               let signature = CursorReadable::read(r)?;
 
                Ok((offer, invoice, signature))
        }
index 02d57ebf63a66266450a67134a85fea57836c424..e3e97f2d6f08b8ad472cd2e24032b5ec1b58dab4 100644 (file)
@@ -14,7 +14,7 @@
 //! [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor
 
 use crate::prelude::*;
-use crate::io::{self, Read, Seek, Write};
+use crate::io::{self, Read, Write};
 use crate::io_extras::{copy, sink};
 use core::hash::Hash;
 use crate::sync::{Mutex, RwLock};
@@ -247,10 +247,10 @@ pub trait Readable
 }
 
 /// A trait that various LDK types implement allowing them to be read in from a
-/// [`Read`]` + `[`Seek`].
-pub(crate) trait SeekReadable where Self: Sized {
+/// [`io::Cursor`].
+pub(crate) trait CursorReadable where Self: Sized {
        /// Reads a `Self` in from the given [`Read`].
-       fn read<R: Read + Seek>(reader: &mut R) -> Result<Self, DecodeError>;
+       fn read<R: AsRef<[u8]>>(reader: &mut io::Cursor<R>) -> Result<Self, DecodeError>;
 }
 
 /// A trait that various higher-level LDK types implement allowing them to be read in
index a6f3e08f62a655c291e90dfa4e87d0f6ad9b7a8f..e56b2d9c68d0f3d57d232f427d56ed1ec65ef53e 100644 (file)
@@ -952,13 +952,13 @@ macro_rules! tlv_stream {
                        }
                }
 
-               impl $crate::util::ser::SeekReadable for $name {
-                       fn read<R: $crate::io::Read + $crate::io::Seek>(reader: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
+               impl $crate::util::ser::CursorReadable for $name {
+                       fn read<R: AsRef<[u8]>>(reader: &mut crate::io::Cursor<R>) -> Result<Self, $crate::ln::msgs::DecodeError> {
                                $(
                                        _init_tlv_field_var!($field, option);
                                )*
-                               let rewind = |cursor: &mut R, offset: usize| {
-                                       cursor.seek($crate::io::SeekFrom::Current(-(offset as i64))).expect("");
+                               let rewind = |cursor: &mut crate::io::Cursor<R>, offset: usize| {
+                                       cursor.set_position(cursor.position().checked_sub(offset as u64).expect("Cannot rewind past 0."));
                                };
                                _decode_tlv_stream_range!(reader, $range, rewind, {
                                        $(($type, $field, (option, encoding: $fieldty))),*