~tim/rust-ruby-wrap-data

9f7cbbc16ab567ff78dd3675b7066d9539d4b35c — Tim Morgan 2 years ago 0f69c70
Move duplicate code to another func
1 files changed, 6 insertions(+), 2 deletions(-)

M src/wrap.rs
M src/wrap.rs => src/wrap.rs +6 -2
@@ 36,13 36,17 @@ extern "C" fn free<T>(data: *mut c_void) {
}

pub fn get<T>(itself: Value) -> Box<T> {
    let rdata: *mut RData = unsafe { mem::transmute(itself) };
    let rdata = rdata(itself);
    let datap = unsafe { (*rdata).data as *mut T };
    unsafe { Box::from_raw(datap) }
}

pub fn set<T>(itself: Value, data: Box<T>) {
    let rdata = rdata(itself);
    let datap = Box::into_raw(data) as *mut c_void;
    let rdata: *mut RData = unsafe { mem::transmute(itself) };
    unsafe { (*rdata).data = datap };
}

fn rdata(object: Value) -> *mut RData {
    unsafe { mem::transmute(object) }
}