Skip to content

Commit

Permalink
Rust: fixing buffer reader memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
sjml committed Dec 11, 2024
1 parent 2dfc3e8 commit 32b6257
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions beschi/writers/boilerplate/Rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ impl<'a> BufferReader<'a> {
}

pub fn from_vec(buffer: Vec<u8>) -> BufferReader<'static> {
let buffer = Box::new(buffer);
BufferReader {
buffer: buffer.leak(),
current_position: 0
buffer: Box::leak(buffer),
current_position: 0,
}
}

Expand Down Expand Up @@ -120,20 +121,6 @@ impl<'a> BufferReader<'a> {
}
}

impl <'a> Drop for BufferReader<'a> {
fn drop(&mut self) {
if std::mem::needs_drop::<Vec<u8>>() {
unsafe {
let _ = Vec::from_raw_parts(
self.buffer.as_ptr() as *mut u8,
self.buffer.len(),
self.buffer.len()
);
}
}
}
}

pub trait MessageCodec {
fn get_message_type(&self) -> MessageType;
fn get_size_in_bytes(&self) -> usize;
Expand Down

0 comments on commit 32b6257

Please sign in to comment.