backingstore: impl Drop for CBytes and add From<Vec<u8>> for CBytes

Summary:
Some clean up.

`CByte` should clean up `vec` in `Drop` since we are going to have `CBytes` in other struct's field and this will allow Rust automatically manage this memory.

Reviewed By: chadaustin

Differential Revision: D18365441

fbshipit-source-id: 7038bc6ff457415ee8234cc4c6b7df0f70005cfe
This commit is contained in:
Zeyi (Rice) Fan 2019-11-19 17:56:59 -08:00 committed by Facebook Github Bot
parent f27e1eb2e0
commit 1713cfca79

View File

@ -19,7 +19,6 @@ pub struct CBytes {
}
impl CBytes {
#[allow(dead_code)]
pub fn from_vec(vec: Vec<u8>) -> Self {
let vec = Box::new(vec);
let ptr = vec.as_ptr();
@ -32,10 +31,21 @@ impl CBytes {
}
}
impl From<Vec<u8>> for CBytes {
fn from(vec: Vec<u8>) -> Self {
CBytes::from_vec(vec)
}
}
impl Drop for CBytes {
fn drop(&mut self) {
let vec = unsafe { Box::from_raw(self.vec) };
drop(vec);
}
}
#[no_mangle]
pub extern "C" fn rust_cbytes_free(vec: *mut CBytes) {
let ptr = unsafe { Box::from_raw(vec) };
let vec = unsafe { Box::from_raw(ptr.vec) };
drop(vec);
drop(ptr);
}