wasmtime-rb icon indicating copy to clipboard operation
wasmtime-rb copied to clipboard

`UnsafeSlice.to_str` frozen string pitfalls

Open jscheid opened this issue 5 months ago • 1 comments

Now, it's quite obvious that UnsafeSlice is... unsafe, but to help the next surprised poor soul I suggest you add something like the following to the documentation of UnsafeSlice.to_str:

The returned string is frozen, which means that any slices, slices of slices, and even the result of .dup, .clone, String.new(str) (of the whole string or any slices) will still reference the underlying memory. To make a true copy that won't keep a reference:

String.new(capacity: str.bytesize).concat(str)

To copy a slice:

IO::Buffer.for(str).get_string(ofs, len)

jscheid avatar Sep 24 '25 14:09 jscheid