Skip to content

Commit

Permalink
feat: add function to decode an entire slice (#23)
Browse files Browse the repository at this point in the history
* implement decode_full

* add tests

* add CHANGELOG

* add PR number

* Update crates/rlp/src/decode.rs

Co-authored-by: Matthias Seitz <[email protected]>

* revert CHANGELOG changes

* fix imports

* add CHANGELOG

* remove TrailingBytes to avoid breaking change

---------

Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
Wollac and mattsse authored Aug 5, 2024
1 parent d3df251 commit 2ba859b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `decode_exact` function ([#23])

### Fixed

- Fix `RlpEncodableWrapper` doc ([#22])

[#22]: https://github.com/alloy-rs/rlp/pull/22
[#23]: https://github.com/alloy-rs/rlp/pull/23

## [0.3.7] - 2024-06-29

Expand Down
38 changes: 37 additions & 1 deletion crates/rlp/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ mod std_impl {
}
}

/// Decodes the entire input, ensuring no trailing bytes remain.
///
/// # Errors
///
/// Returns an error if the encoding is invalid or if data remains after decoding the RLP item.
#[inline]
pub fn decode_exact<T: Decodable>(bytes: impl AsRef<[u8]>) -> Result<T> {
let mut buf = bytes.as_ref();
let out = T::decode(&mut buf)?;

// check if there are any remaining bytes after decoding
if !buf.is_empty() {
// TODO: introduce a new variant TrailingBytes to better distinguish this error
return Err(Error::UnexpectedLength);
}

Ok(out)
}

/// Left-pads a slice to a statically known size array.
///
/// # Errors
Expand Down Expand Up @@ -212,7 +231,7 @@ fn slice_to_array<const N: usize>(slice: &[u8]) -> Result<[u8; N]> {
#[cfg(test)]
mod tests {
use super::*;
use crate::Encodable;
use crate::{encode, Encodable};
use core::fmt::Debug;
use hex_literal::hex;

Expand Down Expand Up @@ -356,4 +375,21 @@ mod tests {
check_decode::<u8, _>([(Err(Error::InputTooShort), &hex!("82")[..])]);
check_decode::<u64, _>([(Err(Error::InputTooShort), &hex!("82")[..])]);
}

#[test]
fn rlp_full() {
fn check_decode_exact<T: Decodable + Encodable + PartialEq + Debug>(input: T) {
let encoded = encode(&input);
assert_eq!(decode_exact::<T>(&encoded), Ok(input));
assert_eq!(
decode_exact::<T>([encoded, vec![0x00]].concat()),
Err(Error::UnexpectedLength)
);
}

check_decode_exact::<String>("".into());
check_decode_exact::<String>("test1234".into());
check_decode_exact::<Vec<u64>>(vec![]);
check_decode_exact::<Vec<u64>>(vec![0; 4]);
}
}
2 changes: 1 addition & 1 deletion crates/rlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
extern crate alloc;

mod decode;
pub use decode::{Decodable, Rlp};
pub use decode::{decode_exact, Decodable, Rlp};

mod error;
pub use error::{Error, Result};
Expand Down

0 comments on commit 2ba859b

Please sign in to comment.