From e24738a4d86ebe8c6778ce1ddb1bb29c95b91132 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 7 Jan 2021 11:13:47 +0800 Subject: [PATCH 1/2] fix(napi): remove into_*_ref from JsString Close https://github.com/napi-rs/napi-rs/issues/411 --- napi/src/js_values/arraybuffer.rs | 3 +-- napi/src/js_values/string/mod.rs | 20 +------------------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/napi/src/js_values/arraybuffer.rs b/napi/src/js_values/arraybuffer.rs index 4a8dd8d4..cfd91a14 100644 --- a/napi/src/js_values/arraybuffer.rs +++ b/napi/src/js_values/arraybuffer.rs @@ -4,8 +4,7 @@ use std::os::raw::c_void; use std::ptr; use std::slice; -use super::{Value, ValueType}; -use crate::{check_status, sys, JsUnknown, NapiValue, Ref, Result}; +use crate::{check_status, sys, JsUnknown, NapiValue, Ref, Result, Value, ValueType}; pub struct JsArrayBuffer(pub(crate) Value); diff --git a/napi/src/js_values/string/mod.rs b/napi/src/js_values/string/mod.rs index 83715efa..d2a2c5f5 100644 --- a/napi/src/js_values/string/mod.rs +++ b/napi/src/js_values/string/mod.rs @@ -1,9 +1,7 @@ use std::mem; use std::ptr; -use super::Value; -use crate::check_status; -use crate::{sys, Ref, Result}; +use crate::{check_status, sys, Result, Value}; pub use latin1::JsStringLatin1; pub use utf16::JsStringUtf16; @@ -13,7 +11,6 @@ mod latin1; mod utf16; mod utf8; -#[derive(Clone, Copy)] pub struct JsString(pub(crate) Value); impl JsString { @@ -70,11 +67,6 @@ impl JsString { }) } - #[inline] - pub fn into_utf8_ref(self) -> Result> { - Ref::new(self.0, 1, self.into_utf8()?) - } - #[inline] pub fn into_utf16(self) -> Result { let mut written_char_count = 0usize; @@ -100,11 +92,6 @@ impl JsString { }) } - #[inline] - pub fn into_utf16_ref(self) -> Result> { - Ref::new(self.0, 1, self.into_utf16()?) - } - #[inline] pub fn into_latin1(self) -> Result { let mut written_char_count = 0usize; @@ -130,9 +117,4 @@ impl JsString { }), }) } - - #[inline] - pub fn into_latin1_ref(self) -> Result> { - Ref::new(self.0, 1, self.into_latin1()?) - } } From c1215379f2deda197c23879d0b40ea177955017c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 7 Jan 2021 11:34:49 +0800 Subject: [PATCH 2/2] feat(napi): impl Clone + Copy for Primitive types --- napi/src/js_values/bigint.rs | 1 + napi/src/js_values/boolean.rs | 1 + napi/src/js_values/mod.rs | 2 ++ napi/src/js_values/number.rs | 1 + napi/src/js_values/string/mod.rs | 1 + napi/src/js_values/undefined.rs | 1 + 6 files changed, 7 insertions(+) diff --git a/napi/src/js_values/bigint.rs b/napi/src/js_values/bigint.rs index 1cfffe9a..6a034fd2 100644 --- a/napi/src/js_values/bigint.rs +++ b/napi/src/js_values/bigint.rs @@ -4,6 +4,7 @@ use std::ptr; use super::*; use crate::{check_status, sys, Result}; +#[derive(Clone, Copy)] pub struct JsBigint { pub(crate) raw: Value, pub word_count: usize, diff --git a/napi/src/js_values/boolean.rs b/napi/src/js_values/boolean.rs index 11ac20a3..55ef7f3b 100644 --- a/napi/src/js_values/boolean.rs +++ b/napi/src/js_values/boolean.rs @@ -4,6 +4,7 @@ use super::Value; use crate::check_status; use crate::{sys, Error, Result}; +#[derive(Clone, Copy)] pub struct JsBoolean(pub(crate) Value); impl JsBoolean { diff --git a/napi/src/js_values/mod.rs b/napi/src/js_values/mod.rs index 62040ae7..bf740c1b 100644 --- a/napi/src/js_values/mod.rs +++ b/napi/src/js_values/mod.rs @@ -61,8 +61,10 @@ pub use value_type::ValueType; pub struct JsUnknown(pub(crate) Value); +#[derive(Clone, Copy)] pub struct JsNull(pub(crate) Value); +#[derive(Clone, Copy)] pub struct JsSymbol(pub(crate) Value); pub struct JsExternal(pub(crate) Value); diff --git a/napi/src/js_values/number.rs b/napi/src/js_values/number.rs index b0cd3624..6e04c2cc 100644 --- a/napi/src/js_values/number.rs +++ b/napi/src/js_values/number.rs @@ -4,6 +4,7 @@ use super::Value; use crate::check_status; use crate::{sys, Error, Result}; +#[derive(Clone, Copy)] pub struct JsNumber(pub(crate) Value); impl JsNumber { diff --git a/napi/src/js_values/string/mod.rs b/napi/src/js_values/string/mod.rs index d2a2c5f5..25a00362 100644 --- a/napi/src/js_values/string/mod.rs +++ b/napi/src/js_values/string/mod.rs @@ -11,6 +11,7 @@ mod latin1; mod utf16; mod utf8; +#[derive(Clone, Copy)] pub struct JsString(pub(crate) Value); impl JsString { diff --git a/napi/src/js_values/undefined.rs b/napi/src/js_values/undefined.rs index 2d76bf79..53557ce5 100644 --- a/napi/src/js_values/undefined.rs +++ b/napi/src/js_values/undefined.rs @@ -1,3 +1,4 @@ use super::Value; +#[derive(Clone, Copy)] pub struct JsUndefined(pub(crate) Value);