Skip to content

Use From trait instead of Decode #29

Description

@Alignof

Currently, we use original decoding trait Decode.

use raki::{BaseIOpcode, Decode, Instruction, Isa, OpcodeKind};

fn main() {
    let inst_bytes: u32 = 0b1110_1110_1100_0010_1000_0010_1001_0011;
    let inst: Instruction = match inst_bytes.decode(Isa::Rv32) {
        Ok(inst) => inst,
        Err(e) => panic!("decoding failed due to {e:?}"),
    };

    assert_eq!(inst.opc, OpcodeKind::BaseI(BaseIOpcode::ADDI));
    println!("{inst}");
}
// --output--
// addi t0, t0, -276

But there are some problems with these.

  • The methods must be imported before they can be used.
  • They are not intuitive.
  • The effects of traits are large.

We should consider using the From trait instead.

fn main() {
    let inst_bytes: u32 = 0b1110_1110_1100_0010_1000_0010_1001_0011;
    let inst: Instruction = match Instruction::try_from(inst_bytes) {
        Ok(inst) => inst,
        Err(e) => panic!("decoding failed due to {e:?}"),
    };
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions