Skip to content

Commit

Permalink
roll back the unsound borrowing API
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Sep 21, 2023
1 parent 58e1e51 commit 745bb86
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/sys/tskbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::ptr::NonNull;

pub struct TskBox<T> {
tsk: NonNull<T>,
teardown: Option<unsafe extern "C" fn(*mut T) -> i32>,
owning: bool,
teardown: unsafe extern "C" fn(*mut T) -> i32,
}

impl<T> TskBox<T> {
Expand All @@ -14,11 +13,7 @@ impl<T> TskBox<T> {
let x = unsafe { libc::malloc(std::mem::size_of::<T>()) as *mut T };
let _ = init(x);
let tsk = NonNull::new(x).unwrap();
Self {
tsk,
teardown: Some(teardown),
owning: true,
}
Self { tsk, teardown }
}

pub fn as_ref(&self) -> &T {

Check warning on line 19 in src/sys/tskbox.rs

View workflow job for this annotation

GitHub Actions / Test (Miri)

methods `as_ref`, `as_mut`, `as_ptr`, and `as_mut_ptr` are never used
Expand All @@ -40,14 +35,10 @@ impl<T> TskBox<T> {

impl<T> Drop for TskBox<T> {
fn drop(&mut self) {
if let Some(teardown) = self.teardown {
unsafe {
(teardown)(self.tsk.as_mut() as *mut T);
}
}
if self.owning {
unsafe { libc::free(self.tsk.as_ptr() as *mut libc::c_void) }
unsafe {
(self.teardown)(self.tsk.as_mut() as *mut T);
}
unsafe { libc::free(self.tsk.as_ptr() as *mut libc::c_void) }
}
}

Expand All @@ -70,7 +61,7 @@ fn test_miri() {

let options = 0_i32;

let b = TskBox::new(
let _ = TskBox::new(
|x: *mut X| unsafe {
(*x).data = options;
0
Expand All @@ -93,7 +84,7 @@ fn test_table_collection_tskbox() {
#[test]
fn test_table_collection_tskbox_shared_ptr() {
let flags: u32 = 0;
let tables = TskBox::new(
let _ = TskBox::new(
|t: *mut super::bindings::tsk_table_collection_t| unsafe {
super::bindings::tsk_table_collection_init(t, flags)
},
Expand Down

0 comments on commit 745bb86

Please sign in to comment.