JsStringLatin1

Struct JsStringLatin1 

Source
pub struct JsStringLatin1<'env> { /* private fields */ }

Implementations§

Source§

impl<'env> JsStringLatin1<'env>

Source

pub fn from_data(env: &'env Env, data: Vec<u8>) -> Result<JsStringLatin1<'env>>

Try to create a new JavaScript latin1 string from a Rust Vec<u8> without copying the data

§Behavior

The copied parameter in the underlying node_api_create_external_string_latin1 call indicates whether the string data was copied into V8’s heap rather than being used as an external reference.

§When copied is true:
  • String data is copied to V8’s heap
  • Finalizer is called immediately if provided
  • Original buffer can be freed after the call
  • Performance benefit of external strings is not achieved
§When copied is false:
  • V8 creates an external string that references the original buffer without copying
  • Original buffer must remain valid for the lifetime of the JS string
  • Finalizer called when string is garbage collected
  • Memory usage and copying overhead is reduced
§Common scenarios where copied is true:
  • String is too short (typically < 10-15 characters)
  • V8 heap is under memory pressure
  • V8 is running with pointer compression or sandbox features
  • Invalid Latin-1 encoding that requires sanitization
  • Platform doesn’t support external strings
  • Memory alignment issues with the provided buffer

The copied parameter serves as feedback to understand whether the external string optimization was successful or if V8 fell back to traditional string creation.

Source

pub unsafe fn from_external<T: 'env, F: FnOnce(Env, T) + 'env>( env: &'env Env, data: *const u8, len: usize, finalize_hint: T, finalize_callback: F, ) -> Result<JsStringLatin1<'env>>

Creates an external Latin-1 string from raw data with a custom finalize callback.

§Safety

The caller must ensure that:

  • The data pointer is valid for the lifetime of the string
  • The finalize callback properly cleans up the data
§Behavior

The copied parameter in the underlying node_api_create_external_string_latin1 call indicates whether the string data was copied into V8’s heap rather than being used as an external reference.

§When copied is true:
  • String data is copied to V8’s heap
  • Finalizer is called immediately if provided
  • Original buffer can be freed after the call
  • Performance benefit of external strings is not achieved
§When copied is false:
  • V8 creates an external string that references the original buffer without copying
  • Original buffer must remain valid for the lifetime of the JS string
  • Finalizer called when string is garbage collected
  • Memory usage and copying overhead is reduced
§Common scenarios where copied is true:
  • String is too short (typically < 10-15 characters)
  • V8 heap is under memory pressure
  • V8 is running with pointer compression or sandbox features
  • Invalid Latin-1 encoding that requires sanitization
  • Platform doesn’t support external strings
  • Memory alignment issues with the provided buffer

The copied parameter serves as feedback to understand whether the external string optimization was successful or if V8 fell back to traditional string creation.

Source

pub fn from_static( env: &'env Env, string: &'static str, ) -> Result<JsStringLatin1<'env>>

Source

pub fn as_slice(&self) -> &[u8]

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn take(self) -> Vec<u8>

Source

pub fn into_value(self) -> JsString<'env>

Source

pub fn into_latin1_string(self) -> Result<String>

Trait Implementations§

Source§

impl From<JsStringLatin1<'_>> for Vec<u8>

Source§

fn from(value: JsStringLatin1<'_>) -> Self

Converts to this type from the input type.
Source§

impl ToNapiValue for JsStringLatin1<'_>

Auto Trait Implementations§

§

impl<'env> Freeze for JsStringLatin1<'env>

§

impl<'env> RefUnwindSafe for JsStringLatin1<'env>

§

impl<'env> !Send for JsStringLatin1<'env>

§

impl<'env> !Sync for JsStringLatin1<'env>

§

impl<'env> Unpin for JsStringLatin1<'env>

§

impl<'env> UnwindSafe for JsStringLatin1<'env>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> JsValuesTupleIntoVec for T
where T: ToNapiValue,

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more