Uint8Array view unsafety clarification

I am writing an l-system generator using wasm-bindgen. The UI will be written in Javascript, whereas the rust code is responsible for parsing, evaluation, and geometry generation.

My aim is to pass a string representation of the system to rust, and rust will return a zero-copy buffer of geometry primitives which will be rendered in JS. It seems that the best way to do this is with Uint8Array::view. This method has a few unsafety caveats which make sense, except for the following:

Finally, the returned object is disconnected from the input slice’s lifetime, so there’s no guarantee that the data is read at the right time.

What exactly does this mean? Is this referring to the situation where the input slice is dropped before the JS actually receives the Uint8Array? Or is there another way for the data to be read at the "wrong" time?

Uint8Array::view gives you a view into Rusts memory in the Wasm Runtime. You can think of passing a slice to JS.
Now JS just as C does not have lifetimes so you are responsible to make sure the reference is not kept around after the lifetime ends.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.