pub struct JsString(_);
Implementations§
source§impl JsString
impl JsString
sourcepub fn utf8_len(&self) -> Result<usize>
pub fn utf8_len(&self) -> Result<usize>
Examples found in repository?
src/js_values/string/mod.rs (line 59)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
pub fn into_utf8(self) -> Result<JsStringUtf8> {
let mut written_char_count = 0;
let len = self.utf8_len()? + 1;
let mut result = Vec::with_capacity(len);
let buf_ptr = result.as_mut_ptr();
check_status!(unsafe {
sys::napi_get_value_string_utf8(
self.0.env,
self.0.value,
buf_ptr,
len,
&mut written_char_count,
)
})?;
Ok(JsStringUtf8 {
inner: self,
buf: result,
})
}
sourcepub fn utf16_len(&self) -> Result<usize>
pub fn utf16_len(&self) -> Result<usize>
Examples found in repository?
src/js_values/string/mod.rs (line 80)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
pub fn into_utf16(self) -> Result<JsStringUtf16> {
let mut written_char_count = 0usize;
let len = self.utf16_len()? + 1;
let mut result = vec![0; len];
let buf_ptr = result.as_mut_ptr();
check_status!(unsafe {
sys::napi_get_value_string_utf16(
self.0.env,
self.0.value,
buf_ptr,
len,
&mut written_char_count,
)
})?;
Ok(JsStringUtf16 {
inner: self,
buf: result,
})
}
sourcepub fn latin1_len(&self) -> Result<usize>
pub fn latin1_len(&self) -> Result<usize>
Examples found in repository?
src/js_values/string/mod.rs (line 101)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
pub fn into_latin1(self) -> Result<JsStringLatin1> {
let mut written_char_count = 0usize;
let len = self.latin1_len()? + 1;
let mut result = Vec::with_capacity(len);
let buf_ptr = result.as_mut_ptr();
check_status!(unsafe {
sys::napi_get_value_string_latin1(
self.0.env,
self.0.value,
buf_ptr,
len,
&mut written_char_count,
)
})?;
mem::forget(result);
Ok(JsStringLatin1 {
inner: self,
buf: mem::ManuallyDrop::new(unsafe {
Vec::from_raw_parts(buf_ptr as *mut _, written_char_count, written_char_count)
}),
})
}
sourcepub fn into_utf8(self) -> Result<JsStringUtf8>
pub fn into_utf8(self) -> Result<JsStringUtf8>
Examples found in repository?
More examples
src/js_values/de.rs (line 41)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'x>,
{
let js_value_type = type_of!(self.0.env, self.0.value)?;
match js_value_type {
ValueType::Null | ValueType::Undefined => visitor.visit_unit(),
ValueType::Boolean => {
let js_boolean = unsafe { JsBoolean::from_raw_unchecked(self.0.env, self.0.value) };
visitor.visit_bool(js_boolean.get_value()?)
}
ValueType::Number => {
let js_number: f64 =
unsafe { JsNumber::from_raw_unchecked(self.0.env, self.0.value).try_into()? };
if (js_number.trunc() - js_number).abs() < f64::EPSILON {
visitor.visit_i64(js_number as i64)
} else {
visitor.visit_f64(js_number)
}
}
ValueType::String => {
let js_string = unsafe { JsString::from_raw_unchecked(self.0.env, self.0.value) };
visitor.visit_str(js_string.into_utf8()?.as_str()?)
}
ValueType::Object => {
let js_object = unsafe { JsObject::from_raw_unchecked(self.0.env, self.0.value) };
if js_object.is_array()? {
let mut deserializer =
JsArrayAccess::new(&js_object, js_object.get_array_length_unchecked()?);
visitor.visit_seq(&mut deserializer)
} else if js_object.is_buffer()? {
visitor.visit_bytes(&JsBufferValue::from_raw(self.0.env, self.0.value)?)
} else {
let mut deserializer = JsObjectAccess::new(&js_object)?;
visitor.visit_map(&mut deserializer)
}
}
#[cfg(feature = "napi6")]
ValueType::BigInt => {
let mut js_bigint = unsafe { JsBigInt::from_raw(self.0.env, self.0.value)? };
let (signed, v, _loss) = js_bigint.get_u128()?;
if signed {
visitor.visit_i128(-(v as i128))
} else {
visitor.visit_u128(v)
}
}
ValueType::External | ValueType::Function | ValueType::Symbol => Err(Error::new(
Status::InvalidArg,
format!("typeof {:?} value could not be deserialized", js_value_type),
)),
ValueType::Unknown => unreachable!(),
}
}
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'x>,
{
visitor.visit_bytes(&JsBufferValue::from_raw(self.0.env, self.0.value)?)
}
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'x>,
{
visitor.visit_bytes(&JsBufferValue::from_raw(self.0.env, self.0.value)?)
}
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where
V: Visitor<'x>,
{
match type_of!(self.0.env, self.0.value)? {
ValueType::Undefined | ValueType::Null => visitor.visit_none(),
_ => visitor.visit_some(self),
}
}
fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: Visitor<'x>,
{
let js_value_type = type_of!(self.0.env, self.0.value)?;
match js_value_type {
ValueType::String => visitor.visit_enum(JsEnumAccess::new(
unsafe { JsString::from_raw_unchecked(self.0.env, self.0.value) }
.into_utf8()?
.into_owned()?,
None,
)),
ValueType::Object => {
let js_object = unsafe { JsObject::from_raw_unchecked(self.0.env, self.0.value) };
let properties = js_object.get_property_names()?;
let property_len = properties.get_array_length_unchecked()?;
if property_len != 1 {
Err(Error::new(
Status::InvalidArg,
format!(
"object key length: {}, can not deserialize to Enum",
property_len
),
))
} else {
let key = properties.get_element::<JsString>(0)?;
let value: JsUnknown = js_object.get_property(key)?;
visitor.visit_enum(JsEnumAccess::new(
key.into_utf8()?.into_owned()?,
Some(&value.0),
))
}
}
_ => Err(Error::new(
Status::InvalidArg,
format!(
"{:?} type could not deserialize to Enum type",
js_value_type
),
)),
}
}
pub fn into_utf16(self) -> Result<JsStringUtf16>
pub fn into_latin1(self) -> Result<JsStringLatin1>
source§impl JsString
impl JsString
pub fn into_unknown(self) -> JsUnknown
pub fn coerce_to_bool(self) -> Result<JsBoolean>
pub fn coerce_to_number(self) -> Result<JsNumber>
pub fn coerce_to_string(self) -> Result<JsString>
pub fn coerce_to_object(self) -> Result<JsObject>
pub fn is_date(&self) -> Result<bool>
pub fn is_promise(&self) -> Result<bool>
pub fn is_error(&self) -> Result<bool>
pub fn is_typedarray(&self) -> Result<bool>
pub fn is_dataview(&self) -> Result<bool>
pub fn is_array(&self) -> Result<bool>
pub fn is_buffer(&self) -> Result<bool>
pub fn instanceof<Constructor>(&self, constructor: Constructor) -> Result<bool>where
Constructor: NapiRaw,
Trait Implementations§
source§impl NapiValue for JsString
impl NapiValue for JsString
unsafe fn from_raw(env: napi_env, value: napi_value) -> Result<JsString>
unsafe fn from_raw_unchecked(env: napi_env, value: napi_value) -> JsString
source§impl ValidateNapiValue for JsString
impl ValidateNapiValue for JsString
source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Safety Read more