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

feat: infer ZigType(ZT) from SSZType (ST) #10

Merged
merged 2 commits into from
Dec 27, 2024
Merged

feat: infer ZigType(ZT) from SSZType (ST) #10

merged 2 commits into from
Dec 27, 2024

Conversation

twoeths
Copy link
Owner

@twoeths twoeths commented Dec 27, 2024

Motivation

  • for typescript, to define a SSZ type it's pretty simple
export const ValidatorType = {
  pubkey: BLSPubkey,
  withdrawalCredentials: Bytes32,
  effectiveBalance: UintNum64,
  slashed: Boolean,
  activationEligibilityEpoch: EpochInf,
  activationEpoch: EpochInf,
  exitEpoch: EpochInf,
  withdrawableEpoch: EpochInf,
};

based on that it uses ValueOf utility to infer Typescript type

  • however for now in zig it has to define both ssz type and zig type
pub fn createContainerType(comptime ST: type, comptime ZT: type, hashFn: HashFn) type {

should be able to infer ZT from ST similar to typescript

Description

  • infer zig type ZT from ST
    • each type needs to define getZigType() type and getZigTypeAlignment() usize at struct level
    • at compile time, infer zig type for parent type from child type using the 2 methods above
    • the creation of zig type becomes simpler since we drop ZT, it is now the same to typescript's with 1 extra param for hashFn
pub fn createContainerType(comptime ST: type, hashFn: HashFn) type {
comptime var new_fields: [ssz_struct_info.fields.len]std.builtin.Type.StructField = undefined;
    comptime var alignment: usize = 0;
    inline for (ssz_struct_info.fields, 0..) |field, i| {
        if (field.type.getZigTypeAlignment() > alignment) {
            alignment = field.type.getZigTypeAlignment();
        }
        new_fields[i] = .{
            .name = field.name,
            .type = field.type.getZigType(),
            // TODO: implement this
            .default_value = null,
            .is_comptime = false,
            .alignment = field.type.getZigTypeAlignment(),
        };
    }

    const ZT = comptime @Type(.{
        .Struct = .{
            .layout = .auto,
            .backing_integer = null,
            .fields = new_fields[0..],
            // TODO: do we need to assign this value?
            .decls = &[_]std.builtin.Type.Declaration{},
            .is_tuple = false,
        },
    });

@twoeths twoeths marked this pull request as ready for review December 27, 2024 04:05
@twoeths twoeths merged commit 086a9fe into main Dec 27, 2024
1 check passed
@twoeths twoeths deleted the infer_zig_type branch December 27, 2024 04:05
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.

1 participant