| 170 | let mut bytes: Vec<MaybeUninit<u8>> = Vec::with_capacity(size); |
| 171 | unsafe { |
| 172 | bytes.set_len(size); |
| 173 | } |
| 174 | |
| 175 | Buffer(maybeuninit_to_u8(bytes)).into_js(&ctx) |
| 176 | } |
| 177 | |
| 178 | fn maybeuninit_to_u8(vec: Vec<MaybeUninit<u8>>) -> Vec<u8> { |
| 179 | let len = vec.len(); |
| 180 | let capacity = vec.capacity(); |
| 181 | let ptr = vec.as_ptr() as *mut u8; |
| 182 | |
| 183 | std::mem::forget(vec); |
| 184 | |
| 185 | // This conversion is safe because MaybeUninit has the same memory layout as u8, meaning the underlying bytes are identical. |
| 186 | // Since Vec<MaybeUninit> and Vec share the same memory representation, a simple reinterpretation of the pointer is valid. |