Struct napi::JsStringUtf8

source ·
pub struct JsStringUtf8 { /* private fields */ }

Implementations§

Examples found in repository?
src/js_values/string/utf8.rs (line 33)
32
33
34
  pub fn into_owned(self) -> Result<String> {
    Ok(self.as_str()?.to_owned())
  }
More examples
Hide additional examples
src/env.rs (line 1038)
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
  pub fn get_napi_version(&self) -> Result<u32> {
    let global = self.get_global()?;
    let process: JsObject = global.get_named_property("process")?;
    let versions: JsObject = process.get_named_property("versions")?;
    let napi_version: JsString = versions.get_named_property("napi")?;
    napi_version
      .into_utf8()?
      .as_str()?
      .parse()
      .map_err(|e| Error::new(Status::InvalidArg, format!("{}", e)))
  }
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
  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!(),
    }
  }
Examples found in repository?
src/js_values/string/utf8.rs (line 37)
36
37
38
  pub fn take(self) -> Vec<u8> {
    self.as_slice().to_vec()
  }
Examples found in repository?
src/js_values/string/utf8.rs (line 49)
48
49
50
  fn try_from(value: JsStringUtf8) -> Result<String> {
    value.into_owned()
  }
More examples
Hide additional examples
src/js_values/de.rs (line 112)
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_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
        ),
      )),
    }
  }
Examples found in repository?
src/js_values/string/utf8.rs (line 55)
54
55
56
  fn from(value: JsStringUtf8) -> Self {
    value.take()
  }

Trait Implementations§

Converts to this type from the input type.
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.