pub struct ValueView<'s>(/* private fields */);Expand description
Returns a view onto a string’s contents.
WARNING: This does not copy the string’s contents, and will therefore be invalidated if the GC can move the string while the ValueView is alive. It is therefore required that no GC or allocation can happen while there is an active ValueView. This requirement may be relaxed in the future.
V8 strings are either encoded as one-byte or two-bytes per character.
Implementations§
Source§impl<'s> ValueView<'s>
impl<'s> ValueView<'s>
pub fn new(isolate: &mut Isolate, string: Local<'s, String>) -> Self
pub fn data(&self) -> ValueViewData<'_>
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Returns a zero-copy &str if the string is one-byte and pure ASCII.
This is the fastest way to access a V8 string’s contents as a Rust
&str — no allocation, no copy, no transcoding. Returns None for
strings that contain non-ASCII Latin-1 bytes or are two-byte encoded.
The returned reference is valid as long as this ValueView is alive.
Sourcepub fn to_cow_lossy(&self) -> Cow<'_, str>
pub fn to_cow_lossy(&self) -> Cow<'_, str>
Returns the string contents as a Cow<str>.
- One-byte ASCII: returns
Cow::Borrowed(&str)— true zero-copy. - One-byte Latin-1 (non-ASCII): transcodes to UTF-8, returns
Cow::Owned. - Two-byte (UTF-16): transcodes to UTF-8 via
std::string::String::from_utf16_lossy, returnsCow::Owned.
For the common case of ASCII strings this is zero-copy. The Latin-1 transcoding uses a SIMD-friendly loop that processes 8 bytes at a time.