-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mutable version of UnsafeTypedArray #360
Comments
Yep, you're right, although I'm not 100% sure how to make a a mutable The current macro_rules! impl_for_unsafe_typed_array {
($ty:ty, $kind:expr) => {
impl< 'r > JsSerialize for UnsafeTypedArray< 'r, $ty > {
#[doc(hidden)]
#[inline]
fn _into_js< 'a >( &'a self ) -> SerializedValue< 'a > {
SerializedUntaggedUnsafeTypedArray {
pointer: self.0.as_ptr() as u32 / mem::size_of::< $ty >() as u32,
length: self.0.len() as u32,
kind: $kind
}.into()
}
}
__js_serializable_boilerplate!( impl< 'a > for UnsafeTypedArray< 'a, $ty > );
}
} So basically it just pushes raw pointers to the JS side. I wonder, would using |
Ah - that seems like a pretty reasonable implementation. Since What if we defined a type like this, though? pub struct MutableUnsafeTypedArray(*mut [T], PhantomData<(&'a mut [T])>) Then we would turn the reference into a |
I've been reading up a bit more. While not everything makes since, I think if there's a raw pointer stored in the data structure it should be sound. Like you said, there are a lot of rules that aren't defined. But https://doc.rust-lang.org/nomicon/aliasing.html mentions that raw pointers have no aliasing requirement, and I think that if we create one then even taking a reference to it should be fine. I'd be wary of just using |
UnsafeTypedArray
takes&[T]
in the constructor, and stores that internally. It's nice for reading values efficiently, but as I understand it rustc will assume that values behind a reference are never mutated, and thus it's unsound to mutate values using anUnsafeTypedArray
.Could a similar structure be made which exposes a mutable view of an array to JS? For manual initialization code, like
The text was updated successfully, but these errors were encountered: