Skip to content

UnsafeSlice.to_str frozen string pitfalls #501

Description

@jscheid

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions