pub struct JsStringLatin1<'env> { /* private fields */ }Implementations§
Source§impl<'env> JsStringLatin1<'env>
impl<'env> JsStringLatin1<'env>
Sourcepub fn from_data(env: &'env Env, data: Vec<u8>) -> Result<JsStringLatin1<'env>>
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.
Sourcepub 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>>
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.