From 8f8636b4f7586e7ff0a075e55926acdc34149a84 Mon Sep 17 00:00:00 2001 From: Nugine Date: Fri, 7 Oct 2022 01:26:38 +0800 Subject: [PATCH] feat: add String::write_utf8_uninit (#1019) --- src/string.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/string.rs b/src/string.rs index 57b31b36..ca353b3a 100644 --- a/src/string.rs +++ b/src/string.rs @@ -1,6 +1,7 @@ use std::convert::TryInto; use std::default::Default; use std::mem::forget; +use std::mem::MaybeUninit; use std::slice; use crate::support::char; @@ -264,6 +265,28 @@ impl String { buffer: &mut [u8], nchars_ref: Option<&mut usize>, options: WriteOptions, + ) -> usize { + unsafe { + // SAFETY: + // We assume that v8 will overwrite the buffer without de-initializing any byte in it. + // So the type casting of the buffer is safe. + + let buffer = { + let len = buffer.len(); + let data = buffer.as_mut_ptr().cast(); + slice::from_raw_parts_mut(data, len) + }; + self.write_utf8_uninit(scope, buffer, nchars_ref, options) + } + } + + /// Writes the contents of the string to an external buffer, as UTF-8. + pub fn write_utf8_uninit( + &self, + scope: &mut Isolate, + buffer: &mut [MaybeUninit], + nchars_ref: Option<&mut usize>, + options: WriteOptions, ) -> usize { let mut nchars_ref_int: int = 0; let bytes = unsafe {