From f1281af260d4f6e747545e26c14109113e04f5b4 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 26 Sep 2020 17:59:58 +0800 Subject: [PATCH] feat(napi): add lifetime span to JsBuffer --- napi/src/env.rs | 16 +++++++--------- napi/src/js_values/buffer.rs | 23 +++++++++-------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/napi/src/env.rs b/napi/src/env.rs index 1ceaed1e..ae8fb30c 100644 --- a/napi/src/env.rs +++ b/napi/src/env.rs @@ -219,7 +219,7 @@ impl Env { Ok(JsObject::from_raw_unchecked(self.0, raw_value)) } - pub fn create_buffer(&self, length: u64) -> Result { + pub fn create_buffer<'env, 'buffer>(&'env self, length: u64) -> Result> { let mut raw_value = ptr::null_mut(); let mut data = Vec::with_capacity(length as usize); let mut data_ptr = data.as_mut_ptr(); @@ -228,7 +228,7 @@ impl Env { })?; mem::forget(data); - Ok(JsBuffer::from_raw_unchecked( + Ok(JsBuffer::new( self.0, raw_value, data_ptr as *mut u8, @@ -236,7 +236,10 @@ impl Env { )) } - pub fn create_buffer_with_data(&self, mut data: Vec) -> Result { + pub fn create_buffer_with_data<'env, 'buffer>( + &'env self, + mut data: Vec, + ) -> Result> { let length = data.len() as u64; let mut raw_value = ptr::null_mut(); let data_ptr = data.as_mut_ptr(); @@ -253,12 +256,7 @@ impl Env { let mut changed = 0; check_status(unsafe { sys::napi_adjust_external_memory(self.0, length as i64, &mut changed) })?; mem::forget(data); - Ok(JsBuffer::from_raw_unchecked( - self.0, - raw_value, - data_ptr, - length as usize, - )) + Ok(JsBuffer::new(self.0, raw_value, data_ptr, length as usize)) } pub fn create_arraybuffer(&self, length: u64) -> Result { diff --git a/napi/src/js_values/buffer.rs b/napi/src/js_values/buffer.rs index 8ecb27a5..37d10c4f 100644 --- a/napi/src/js_values/buffer.rs +++ b/napi/src/js_values/buffer.rs @@ -8,18 +8,13 @@ use crate::error::check_status; use crate::{sys, Error, Result}; #[derive(Debug)] -pub struct JsBuffer { +pub struct JsBuffer<'buffer> { pub value: JsObject, - pub data: &'static [u8], + pub data: &'buffer [u8], } -impl JsBuffer { - pub(crate) fn from_raw_unchecked( - env: sys::napi_env, - value: sys::napi_value, - data: *mut u8, - len: usize, - ) -> Self { +impl<'buffer> JsBuffer<'buffer> { + pub(crate) fn new(env: sys::napi_env, value: sys::napi_value, data: *mut u8, len: usize) -> Self { Self { value: JsObject(Value { env, @@ -139,7 +134,7 @@ impl JsBuffer { } } -impl NapiValue for JsBuffer { +impl<'buffer> NapiValue for JsBuffer<'buffer> { fn raw_value(&self) -> sys::napi_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] { self.data } } -impl Deref for JsBuffer { +impl<'buffer> Deref for JsBuffer<'buffer> { type Target = [u8]; fn deref(&self) -> &[u8] { @@ -191,9 +186,9 @@ impl Deref for JsBuffer { } } -impl TryFrom for JsBuffer { +impl<'buffer> TryFrom for JsBuffer<'buffer> { type Error = Error; - fn try_from(value: JsUnknown) -> Result { + fn try_from(value: JsUnknown) -> Result> { JsBuffer::from_raw(value.0.env, value.0.value) } }