0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-25 08:39:15 -05:00

feat: add String::write_utf8_uninit (#1019)

This commit is contained in:
Nugine 2022-10-07 01:26:38 +08:00 committed by GitHub
parent de7a1acbde
commit 8f8636b4f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<u8>],
nchars_ref: Option<&mut usize>,
options: WriteOptions,
) -> usize {
let mut nchars_ref_int: int = 0;
let bytes = unsafe {