Skip to content

Commit

Permalink
Merge branch 'remove-shrink_to_fit' of github.com:rbran/binaryninja-a…
Browse files Browse the repository at this point in the history
…pi into dev
  • Loading branch information
rssor committed May 1, 2024
2 parents 62eaff4 + c54ccd2 commit 0e1e0d9
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions rust/src/disassembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,9 @@ impl std::fmt::Display for DisassemblyTextLine {
}

impl From<Vec<InstructionTextToken>> for DisassemblyTextLine {
fn from(mut tokens: Vec<InstructionTextToken>) -> Self {
tokens.shrink_to_fit();
fn from(tokens: Vec<InstructionTextToken>) -> Self {
let mut tokens: Box<[_]> = tokens.into();

assert!(tokens.len() == tokens.capacity());
// TODO: let (tokens_pointer, tokens_len, _) = unsafe { tokens.into_raw_parts() }; // Can't use for now...still a rust nightly feature
let tokens_pointer = tokens.as_mut_ptr();
let tokens_len = tokens.len();
Expand Down Expand Up @@ -345,14 +344,11 @@ impl From<Vec<InstructionTextToken>> for DisassemblyTextLine {

impl From<&Vec<&str>> for DisassemblyTextLine {
fn from(string_tokens: &Vec<&str>) -> Self {
let mut tokens: Vec<BNInstructionTextToken> = Vec::with_capacity(string_tokens.len());
tokens.extend(
string_tokens.iter().map(|&token| {
InstructionTextToken::new(token, InstructionTextTokenContents::Text).0
}),
);

assert!(tokens.len() == tokens.capacity());
let mut tokens: Box<[BNInstructionTextToken]> = string_tokens
.iter()
.map(|&token| InstructionTextToken::new(token, InstructionTextTokenContents::Text).0)
.collect();

// let (tokens_pointer, tokens_len, _) = unsafe { tokens.into_raw_parts() }; // Can't use for now...still a rust nighly feature
let tokens_pointer = tokens.as_mut_ptr();
let tokens_len = tokens.len();
Expand Down Expand Up @@ -416,8 +412,9 @@ impl Default for DisassemblyTextLine {

impl Drop for DisassemblyTextLine {
fn drop(&mut self) {
unsafe {
Vec::from_raw_parts(self.0.tokens, self.0.count, self.0.count);
if !self.0.tokens.is_null() {
let ptr = core::ptr::slice_from_raw_parts_mut(self.0.tokens, self.0.count);
let _ = unsafe { Box::from_raw(ptr) };
}
}
}
Expand Down

0 comments on commit 0e1e0d9

Please sign in to comment.