Skip to content

Commit 0e1e0d9

Browse files
committed
Merge branch 'remove-shrink_to_fit' of github.com:rbran/binaryninja-api into dev
2 parents 62eaff4 + c54ccd2 commit 0e1e0d9

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

rust/src/disassembly.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,9 @@ impl std::fmt::Display for DisassemblyTextLine {
307307
}
308308

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

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

346345
impl From<&Vec<&str>> for DisassemblyTextLine {
347346
fn from(string_tokens: &Vec<&str>) -> Self {
348-
let mut tokens: Vec<BNInstructionTextToken> = Vec::with_capacity(string_tokens.len());
349-
tokens.extend(
350-
string_tokens.iter().map(|&token| {
351-
InstructionTextToken::new(token, InstructionTextTokenContents::Text).0
352-
}),
353-
);
354-
355-
assert!(tokens.len() == tokens.capacity());
347+
let mut tokens: Box<[BNInstructionTextToken]> = string_tokens
348+
.iter()
349+
.map(|&token| InstructionTextToken::new(token, InstructionTextTokenContents::Text).0)
350+
.collect();
351+
356352
// let (tokens_pointer, tokens_len, _) = unsafe { tokens.into_raw_parts() }; // Can't use for now...still a rust nighly feature
357353
let tokens_pointer = tokens.as_mut_ptr();
358354
let tokens_len = tokens.len();
@@ -416,8 +412,9 @@ impl Default for DisassemblyTextLine {
416412

417413
impl Drop for DisassemblyTextLine {
418414
fn drop(&mut self) {
419-
unsafe {
420-
Vec::from_raw_parts(self.0.tokens, self.0.count, self.0.count);
415+
if !self.0.tokens.is_null() {
416+
let ptr = core::ptr::slice_from_raw_parts_mut(self.0.tokens, self.0.count);
417+
let _ = unsafe { Box::from_raw(ptr) };
421418
}
422419
}
423420
}

0 commit comments

Comments
 (0)