2025-11-19 14:28:18 +02:00
|
|
|
use borsh::{BorshDeserialize, BorshSerialize};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)]
|
2025-10-14 17:15:04 -03:00
|
|
|
pub struct Message {
|
|
|
|
|
pub(crate) bytecode: Vec<u8>,
|
|
|
|
|
}
|
2025-10-15 20:14:19 -03:00
|
|
|
|
|
|
|
|
impl Message {
|
2026-03-03 23:21:08 +03:00
|
|
|
#[must_use]
|
2026-03-09 18:27:56 +03:00
|
|
|
pub const fn new(bytecode: Vec<u8>) -> Self {
|
2025-10-15 20:14:19 -03:00
|
|
|
Self { bytecode }
|
|
|
|
|
}
|
2026-01-24 04:05:59 +03:00
|
|
|
|
2026-03-03 23:21:08 +03:00
|
|
|
#[must_use]
|
2026-01-24 04:05:59 +03:00
|
|
|
pub fn into_bytecode(self) -> Vec<u8> {
|
|
|
|
|
self.bytecode
|
|
|
|
|
}
|
2025-10-15 20:14:19 -03:00
|
|
|
}
|
2026-06-11 17:37:21 +08:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::Message;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn bytecode_roundtrip() {
|
|
|
|
|
// `Message::new(b).into_bytecode()` must return exactly `b`. Catches
|
|
|
|
|
// mutations of `into_bytecode` returning `vec![]`, `vec![0]`, or `vec![1]`.
|
|
|
|
|
let bytecode = vec![0x7F_u8, 0x45, 0x4C, 0x46]; // ELF magic
|
|
|
|
|
assert_eq!(Message::new(bytecode.clone()).into_bytecode(), bytecode);
|
|
|
|
|
assert!(Message::new(vec![]).into_bytecode().is_empty());
|
|
|
|
|
assert_eq!(Message::new(vec![0xAB]).into_bytecode(), vec![0xAB_u8]);
|
|
|
|
|
}
|
|
|
|
|
}
|