Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Clone for the Insn and cs_insn Structures #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

c3rb3ru5d3d53c
Copy link

Typically, when writing more complex code using capstone we can collect cs_insn structures on their own.

For example, we can create a list of cs_insn structures in Python without them going out of scope.

Currently this is not appearing to be possible in capstone-rs because the Insn and cs_insn structures only being accessed by reference when contained in Instructions iterator.

Most of the work on implementing the clone functionality has already been done for cs_detail structure and others.

This PR simply implements clone for both cs_insn and Insn structures, so it is possible to store these for later use in more complex projects.

Here is an example of using this functionality:

pub fn disassemble_instruction(&self, address: u64) -> Result<Insn, Error> {
        let instructions = match self.disassemble_instructions(address, 1) {
            Ok(instructions) => instructions,
            Err(error) => return Err(Error::new(ErrorKind::Other, "failed to disassemble instruction")),
        };
        let instruction: Insn<'_> = instructions.iter().next().unwrap().clone();
        return Ok(instruction);
    }

It also helps for when we need to iterate a single instruction at a time collecting them for later post-processing.

This becomes an issue when having to unpack from the Instructions iterator and only getting references without the possibility of cloning for later use.

This should not break any other features, it simply continues what was already done with cs_detail and other places clone has already been implemented in the capstone-rs project.

That being said, this is my first Rust-based PR, so proceed with caution.

Thank you 😄

@c3rb3ru5d3d53c
Copy link
Author

Nightly only appeared to have failed due to an unrelated cargo update segmentation fault, not sure why that is the case, all other tests appear to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant