mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-01-21 21:50:20 -05:00
refactor: Have ArrayBuffer::data
return Option<NonNull<c_void>>
(#1131)
The pointer returned by `ArrayBuffer::data` might be null if the backing store has zero length, but the return type `*mut c_void` does not force the user to consider this case. This change makes the return type `Option<NonNull<c_void>>`, which is semantically equivalent, but which forces users of the API to handle the `None` case. This PR is the `ArrayBuffer` counterpart to #817. This is a breaking API change.
This commit is contained in:
parent
3a05ab4cd6
commit
d718370a07
2 changed files with 10 additions and 3 deletions
|
@ -431,8 +431,9 @@ impl ArrayBuffer {
|
||||||
/// More efficient shortcut for GetBackingStore()->Data().
|
/// More efficient shortcut for GetBackingStore()->Data().
|
||||||
/// The returned pointer is valid as long as the ArrayBuffer is alive.
|
/// The returned pointer is valid as long as the ArrayBuffer is alive.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn data(&self) -> *mut c_void {
|
pub fn data(&self) -> Option<NonNull<c_void>> {
|
||||||
unsafe { v8__ArrayBuffer__Data(self) }
|
let raw_ptr = unsafe { v8__ArrayBuffer__Data(self) };
|
||||||
|
NonNull::new(raw_ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a shared pointer to the backing store of this array buffer. This
|
/// Get a shared pointer to the backing store of this array buffer. This
|
||||||
|
|
|
@ -7275,8 +7275,14 @@ fn backing_store_data() {
|
||||||
let store = v8::ArrayBuffer::new_backing_store_from_vec(v).make_shared();
|
let store = v8::ArrayBuffer::new_backing_store_from_vec(v).make_shared();
|
||||||
let buf = v8::ArrayBuffer::with_backing_store(&mut scope, &store);
|
let buf = v8::ArrayBuffer::with_backing_store(&mut scope, &store);
|
||||||
assert_eq!(buf.byte_length(), len);
|
assert_eq!(buf.byte_length(), len);
|
||||||
|
assert!(buf.data().is_some());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
unsafe { std::slice::from_raw_parts_mut(buf.data() as *mut u8, len) },
|
unsafe {
|
||||||
|
std::slice::from_raw_parts_mut(
|
||||||
|
buf.data().unwrap().cast::<u8>().as_ptr(),
|
||||||
|
len,
|
||||||
|
)
|
||||||
|
},
|
||||||
&[1, 2, 3, 4, 5]
|
&[1, 2, 3, 4, 5]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue