Skip to content

Commit

Permalink
feat: add String::write_utf8_uninit (denoland#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Oct 6, 2022
1 parent de7a1ac commit 8f8636b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8f8636b

Please sign in to comment.