Skip to content

Instantly share code, notes, and snippets.

@sethdusek
Created June 22, 2016 08:08
Show Gist options
  • Save sethdusek/e01a6ba88ca3c7c89afa7169a58f8e14 to your computer and use it in GitHub Desktop.
Save sethdusek/e01a6ba88ca3c7c89afa7169a58f8e14 to your computer and use it in GitHub Desktop.
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