Created
May 7, 2019 10:22
-
-
Save fudini/5cbafce5571ccf79de18a788c148f08b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use std::mem; | |
use std::slice; | |
pub struct Pool { | |
uncommited: usize, | |
ptr: *mut u8 | |
} | |
impl Pool { | |
pub fn with_size(size: usize) -> Pool { | |
Pool::from_vec(vec![0; size]) | |
} | |
pub fn from_vec(mut vec: Vec<u8>) -> Pool { | |
let ptr = vec.as_mut_ptr(); | |
mem::forget(vec); | |
Pool { | |
uncommited: 0, | |
ptr, | |
} | |
} | |
pub fn slice<T>(&mut self) -> &mut T { | |
let mut slice_type: &mut T = unsafe { | |
&mut *(self.ptr as *mut _) | |
}; | |
unsafe { | |
self.ptr = self.ptr.add(mem::size_of::<T>()); | |
}; | |
slice_type | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment