]> git.bitcoin.ninja Git - rust-lightning/commitdiff
implement read for &mut FixedLenghtReader
authorArik Sosman <git@arik.io>
Thu, 18 Jul 2024 08:05:19 +0000 (01:05 -0700)
committerArik Sosman <git@arik.io>
Thu, 18 Jul 2024 08:06:47 +0000 (01:06 -0700)
lightning/src/util/ser.rs

index ec9022ca4ffdd0f3827810dcc956b451ecac16ca..c93cec3db66e8afc4f4375542474bed031adf334 100644 (file)
@@ -153,6 +153,24 @@ impl<'a, R: Read> Read for FixedLengthReader<'a, R> {
        }
 }
 
+impl<'a, R: Read> Read for &mut FixedLengthReader<'a, R> {
+       #[inline]
+       fn read(&mut self, dest: &mut [u8]) -> Result<usize, io::Error> {
+               if self.total_bytes == self.bytes_read {
+                       Ok(0)
+               } else {
+                       let read_len = cmp::min(dest.len() as u64, self.total_bytes - self.bytes_read);
+                       match self.read.read(&mut dest[0..(read_len as usize)]) {
+                               Ok(v) => {
+                                       self.bytes_read += v as u64;
+                                       Ok(v)
+                               },
+                               Err(e) => Err(e),
+                       }
+               }
+       }
+}
+
 impl<'a, R: Read> LengthRead for FixedLengthReader<'a, R> {
        #[inline]
        fn total_bytes(&self) -> u64 {