Skip to content

Commit 34a082e

Browse files
authored
wasmi_ir2: add safe Decoder impl for &[u8] (#1664)
* add Decoder impl for &[u8] * apply rustfmt
1 parent 5082b07 commit 34a082e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

crates/ir2/src/decode/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ impl fmt::Display for DecodeError {
7777
}
7878
}
7979

80+
impl Decoder for &'_ [u8] {
81+
fn read_bytes(&mut self, buffer: &mut [u8]) -> Result<(), DecodeError> {
82+
let Some((bytes, rest)) = self.split_at_checked(buffer.len()) else {
83+
return Err(DecodeError::OutOfBytes);
84+
};
85+
buffer.copy_from_slice(bytes);
86+
*self = rest;
87+
Ok(())
88+
}
89+
}
90+
8091
/// Types that can be decoded using a type that implements [`Decoder`].
8192
pub trait Decode: Sized {
8293
/// Decodes `Self` via `decoder`.

0 commit comments

Comments
 (0)