Created
June 22, 2016 08:08
-
-
Save sethdusek/e01a6ba88ca3c7c89afa7169a58f8e14 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
| extern crate libc; | |
| use libc::malloc; | |
| use std::ptr; | |
| struct Vecr<T> { | |
| ptr: *mut T, | |
| len: isize, | |
| capacity: isize | |
| } | |
| impl <T> Vecr<T> { | |
| fn new() -> Vecr<T> { | |
| Vecr { | |
| ptr: unsafe { libc::malloc(std::mem::size_of::<T>()) as *mut T }, | |
| len: 0, | |
| capacity: 5 | |
| } | |
| } | |
| fn idx(&self, i: isize) -> &T { | |
| unsafe { &*self.ptr.offset(i) } | |
| } | |
| fn push(&mut self, val: T) { | |
| unsafe { ptr::write(self.ptr.offset(self.len), val) } | |
| self.len+=1; | |
| } | |
| } | |
| fn main() { | |
| let mut vecr = Vecr::new(); | |
| vecr.push(5); | |
| vecr.push(10); | |
| vecr.push(15); | |
| println!("{}", vecr.idx(0)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment