feat(napi): add lifetime span to JsBuffer

This commit is contained in:
LongYinan 2020-09-26 17:59:58 +08:00
parent c0277542a2
commit f1281af260
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
2 changed files with 16 additions and 23 deletions

View file

@ -219,7 +219,7 @@ impl Env {
Ok(JsObject::from_raw_unchecked(self.0, raw_value)) Ok(JsObject::from_raw_unchecked(self.0, raw_value))
} }
pub fn create_buffer(&self, length: u64) -> Result<JsBuffer> { pub fn create_buffer<'env, 'buffer>(&'env self, length: u64) -> Result<JsBuffer<'buffer>> {
let mut raw_value = ptr::null_mut(); let mut raw_value = ptr::null_mut();
let mut data = Vec::with_capacity(length as usize); let mut data = Vec::with_capacity(length as usize);
let mut data_ptr = data.as_mut_ptr(); let mut data_ptr = data.as_mut_ptr();
@ -228,7 +228,7 @@ impl Env {
})?; })?;
mem::forget(data); mem::forget(data);
Ok(JsBuffer::from_raw_unchecked( Ok(JsBuffer::new(
self.0, self.0,
raw_value, raw_value,
data_ptr as *mut u8, data_ptr as *mut u8,
@ -236,7 +236,10 @@ impl Env {
)) ))
} }
pub fn create_buffer_with_data(&self, mut data: Vec<u8>) -> Result<JsBuffer> { pub fn create_buffer_with_data<'env, 'buffer>(
&'env self,
mut data: Vec<u8>,
) -> Result<JsBuffer<'buffer>> {
let length = data.len() as u64; let length = data.len() as u64;
let mut raw_value = ptr::null_mut(); let mut raw_value = ptr::null_mut();
let data_ptr = data.as_mut_ptr(); let data_ptr = data.as_mut_ptr();
@ -253,12 +256,7 @@ impl Env {
let mut changed = 0; let mut changed = 0;
check_status(unsafe { sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) })?; check_status(unsafe { sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) })?;
mem::forget(data); mem::forget(data);
Ok(JsBuffer::from_raw_unchecked( Ok(JsBuffer::new(self.0, raw_value, data_ptr, length as usize))
self.0,
raw_value,
data_ptr,
length as usize,
))
} }
pub fn create_arraybuffer(&self, length: u64) -> Result<JsArrayBuffer> { pub fn create_arraybuffer(&self, length: u64) -> Result<JsArrayBuffer> {

View file

@ -8,18 +8,13 @@ use crate::error::check_status;
use crate::{sys, Error, Result}; use crate::{sys, Error, Result};
#[derive(Debug)] #[derive(Debug)]
pub struct JsBuffer { pub struct JsBuffer<'buffer> {
pub value: JsObject, pub value: JsObject,
pub data: &'static [u8], pub data: &'buffer [u8],
} }
impl JsBuffer { impl<'buffer> JsBuffer<'buffer> {
pub(crate) fn from_raw_unchecked( pub(crate) fn new(env: sys::napi_env, value: sys::napi_value, data: *mut u8, len: usize) -> Self {
env: sys::napi_env,
value: sys::napi_value,
data: *mut u8,
len: usize,
) -> Self {
Self { Self {
value: JsObject(Value { value: JsObject(Value {
env, env,
@ -139,7 +134,7 @@ impl JsBuffer {
} }
} }
impl NapiValue for JsBuffer { impl<'buffer> NapiValue for JsBuffer<'buffer> {
fn raw_value(&self) -> sys::napi_value { fn raw_value(&self) -> sys::napi_value {
self.value.0.value self.value.0.value
} }
@ -177,13 +172,13 @@ impl NapiValue for JsBuffer {
} }
} }
impl AsRef<[u8]> for JsBuffer { impl<'buffer> AsRef<[u8]> for JsBuffer<'buffer> {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
self.data self.data
} }
} }
impl Deref for JsBuffer { impl<'buffer> Deref for JsBuffer<'buffer> {
type Target = [u8]; type Target = [u8];
fn deref(&self) -> &[u8] { fn deref(&self) -> &[u8] {
@ -191,9 +186,9 @@ impl Deref for JsBuffer {
} }
} }
impl TryFrom<JsUnknown> for JsBuffer { impl<'buffer> TryFrom<JsUnknown> for JsBuffer<'buffer> {
type Error = Error; type Error = Error;
fn try_from(value: JsUnknown) -> Result<JsBuffer> { fn try_from(value: JsUnknown) -> Result<JsBuffer<'buffer>> {
JsBuffer::from_raw(value.0.env, value.0.value) JsBuffer::from_raw(value.0.env, value.0.value)
} }
} }