Skip to content
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

sliceAsBytes: include sentinel #19984

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

david-vanderson
Copy link
Contributor

(This is a reboot of #18021 after pull request bankruptcy)

  • sliceAsBytes() includes the sentinel value if there is one
  • fixes 2 bugs the Allocator interface for resize and realloc (they assumed no sentinel before)
  • cleans up Allocator.free (was manually handling the sentinel)
  • cleans up resinator (was manually handling the sentinel)

Also add bytesAsSliceSentinel() for completeness and because I want to use it in dvui to provide storage for arbitrary user slices.

(This is a reboot of ziglang#18021 after pull request bankruptcy)

* `sliceAsBytes()` includes the sentinel value if there is one
* fixes 2 bugs the Allocator interface for resize and realloc (they
  assumed no sentinel before)
* cleans up Allocator.free (was manually handling the sentinel)
* cleans up resinator (was manually handling the sentinel)

Also add `bytesAsSliceSentinel()` for completeness and because I want to
use it in [dvui](https://github.com/david-vanderson/dvui) to provide
storage for arbitrary user slices.
@@ -3835,6 +3835,7 @@ fn CopyPtrAttrs(
comptime source: type,
comptime size: std.builtin.Type.Pointer.Size,
comptime child: type,
comptime sentinel: ?*const anyopaque,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me like this should be comptime sentinel: ?child

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that first, but ran into a bunch of weird errors like error: comptime dereference requires '[:0]u8' to have a well-defined layout (when a sentinel is present), and error: sentinels are only allowed on slices and unknown-length pointers (when no sentinel).

I think there's something about how sentinels are represented at comptime that interferes with that, but I don't understand it. Can you make that work?

Copy link
Contributor

@rohlem rohlem May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this same code is present in the previous #18021 .
The comptime deference requires [...] well-defined layout error (or at least false positives) should have been resolved by #19630 , which was only merged on 04-17.
Have you checked since then (when creating this new PR)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Sorry I should have been clearer - I tried it originally with the first PR, and I tried it again after @InKryption commented and those are the errors I currently get (when trying comptime sentinel: ?child).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I'm doubting myself - let me recheck.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I get those errors when trying comptime sentinel: ?child with zig version 0.13.0-dev.211+6a65561e3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants