-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::ptr::NonNull; | ||
|
||
/// A box but without the uniqueness guarantees. | ||
pub struct Alloc<T> { | ||
data: NonNull<T>, | ||
} | ||
|
||
impl<T> Alloc<T> { | ||
pub fn new(data: T) -> Self { | ||
let data = Box::new(data); | ||
let data = Box::into_raw(data); | ||
Alloc { | ||
data: unsafe { NonNull::new_unchecked(data) }, | ||
} | ||
} | ||
} | ||
|
||
impl<T> Drop for Alloc<T> { | ||
fn drop(&mut self) { | ||
let data: *mut T = self.data.as_ptr(); | ||
let data: Box<T> = unsafe { Box::from_raw(data) }; | ||
drop(data); | ||
} | ||
} | ||
|
||
impl<T> std::ops::Deref for Alloc<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
unsafe { self.data.as_ref() } | ||
} | ||
} | ||
|
||
impl<T> std::ops::DerefMut for Alloc<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
unsafe { self.data.as_mut() } | ||
} | ||
} | ||
|
||
unsafe impl<T> Send for Alloc<T> where T: Send {} | ||
|
||
unsafe impl<T> Sync for Alloc<T> where T: Sync {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod accumulator; | ||
mod alloc; | ||
pub mod cancelled; | ||
pub mod cycle; | ||
pub mod database; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters