bytecodealliance / bytecodealliance/wasmtime-rb

`UnsafeSlice.to_str` frozen string pitfalls

Open
#501 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
146
Forks
28
Avg merge
7h 52m
Merged PRs (30d)
9

Description

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:
> ```ruby
> String.new(capacity: str.bytesize).concat(str)
> ```
> To copy a slice:
>
> ```ruby
> IO::Buffer.for(str).get_string(ofs, len)
> ```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.