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

deserialization of comptime structs #22

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

Conversation

gballet
Copy link
Collaborator

@gballet gballet commented Jan 2, 2025

This is probably a dead-end since I am trying to force the deserialization of a structure that is inherently comptime. Still, I want to keep experimenting in order to see how far I can push this boundary.

The issues with this approach are that:

  • I need an allocator for slices
  • I can not really write to const (although some test successfully does that, which is a strange behavior and probably invalid).

@gballet gballet added the enhancement New feature or request label Jan 2, 2025
if (try isFixedSizeObject(T)) {
var i: usize = 0;
const pitch = @sizeOf(T);
while (i < out.len) : (i += pitch) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that used to be inline in the calling code. That made no sense and yet it worked.

Comment on lines +154 to +159
.name = try std.testing.allocator.alloc(u8, 5),
.age = 32,
.company = try std.testing.allocator.alloc(u8, 8),
};
@memcpy(data.name, "James");
@memcpy(data.company, "DEV Inc.");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this can be fixed using @as(, @constCast

@@ -347,17 +366,17 @@ test "deserializes a string" {
}

const Pastry = struct {
name: []const u8,
name: []u8,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

deserialization only worked because it was returning a direct pointer to the serialized payload, which is []const u8 itself.

// Taken from ssz.cr
const Person = struct {
name: []const u8,
name: []u8,
age: u8,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

interestingly, this also causes the same "comptime ref" issue, and so I need a more complete workaround for it.

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

Successfully merging this pull request may close these issues.

1 participant