Skip to main content

datex_core/instruction/
serializeable.rs

1//! The [ToBytes] implementations for converting instructions to bytes
2use crate::{
3    global::protocol_structures::serializable::Serializable,
4    instruction::Instruction, prelude::*,
5};
6use binrw::io::Write;
7
8impl Serializable for Instruction {
9    fn to_bytes(&self) -> Vec<u8> {
10        match self {
11            Instruction::Regular(instr) => instr.to_bytes(),
12            Instruction::Type(instr) => instr.to_bytes(),
13        }
14    }
15}
16impl Serializable for &Instruction {
17    fn to_bytes(&self) -> Vec<u8> {
18        match self {
19            Instruction::Regular(instr) => instr.to_bytes(),
20            Instruction::Type(instr) => instr.to_bytes(),
21        }
22    }
23}
24
25impl Serializable for Vec<Instruction> {
26    fn to_bytes(&self) -> Vec<u8> {
27        let mut cursor = binrw::io::Cursor::new(Vec::new());
28        for instruction in self {
29            cursor.write_all(&instruction.to_bytes()).unwrap();
30        }
31        cursor.into_inner()
32    }
33}
34impl Serializable for &[Instruction] {
35    fn to_bytes(&self) -> Vec<u8> {
36        let mut cursor = binrw::io::Cursor::new(Vec::new());
37        for instruction in *self {
38            cursor.write_all(&instruction.to_bytes()).unwrap();
39        }
40        cursor.into_inner()
41    }
42}