#[inline(always)]
pub fn len(&self) -> usize {
let mut l;
unsafe {
// first byte is length
l = (*self.ptr) as usize | (*self.ptr.offset(1) as usize) << 8;
if l < 65535 {
return l;
}
return (*self.ptr.offset(2) as usize) | (*self.ptr.offset(3) as usize) << 8 | (*self.ptr.offset(4) as usize) << 16 | (*self.ptr.offset(5) as usize) << 24;
}
}
pub fn new(slice: &[u8]) -> Self {
let len = slice.len();
let new_slice: *mut u8;
let size;
unsafe {
if len < 65535 {
new_slice = Vec::<u8>::with_capacity(len + 2).leak().as_mut_ptr();
*new_slice.offset(0) = len as u8;
*new_slice.offset(1) = (len >> 8) as u8;
size = 2;
} else {
new_slice = Vec::<u8>::with_capacity(len + 6).leak().as_mut_ptr();
*new_slice.offset(0) = 255u8;
*new_slice.offset(1) = 255u8;
*new_slice.offset(2) = len as u8;
*new_slice.offset(3) = (len >> 8) as u8;
*new_slice.offset(4) = (len >> 16) as u8;
*new_slice.offset(5) = (len >> 24) as u8;
size = 6;
}
ptr::copy_nonoverlapping(slice.as_ptr(), new_slice.offset(size), len);
}
BinValue {
ptr: new_slice
}
}
impl Drop for BinValue {
fn drop(&mut self) {
let len = self.len();
let size = if len < 65535 { 2 } else { 6 };
unsafe {
drop(Vec::from_raw_parts(self.ptr, len + size, len + size));
}
}
}