From 04ede1418290661a1aa317904ff19bd7bf34fd48 Mon Sep 17 00:00:00 2001 From: x19 <0x39015319@gmail.com> Date: Wed, 25 Oct 2023 08:49:36 -0400 Subject: [PATCH] rename --- src/bincode/bincode.zig | 46 ++----------------- .../{snapshot_utils.zig => append_vec.zig} | 6 +-- src/core/snapshot_fields.zig | 2 +- src/lib.zig | 2 +- 4 files changed, 9 insertions(+), 47 deletions(-) rename src/core/{snapshot_utils.zig => append_vec.zig} (97%) diff --git a/src/bincode/bincode.zig b/src/bincode/bincode.zig index b180f8b91..e787ba10f 100644 --- a/src/bincode/bincode.zig +++ b/src/bincode/bincode.zig @@ -212,7 +212,7 @@ pub fn Deserializer(comptime Reader: type) type { }, .Struct => |*info| { inline for (info.fields) |field| { - if (get_field_config(T, field)) |config| { + if (getFieldConfig(T, field)) |config| { if (config.free) |free_fcn| { var field_value = @field(value, field.name); switch (@typeInfo(field.type)) { @@ -274,7 +274,7 @@ pub fn Deserializer(comptime Reader: type) type { inline for (info.fields) |field| { if (!field.is_comptime) { - if (get_field_config(T, field)) |config| { + if (getFieldConfig(T, field)) |config| { if (shouldUseDefaultValue(field, config)) |default_value| { @field(data, field.name) = @as(*const field.type, @ptrCast(@alignCast(default_value))).*; continue; @@ -580,7 +580,7 @@ pub fn Serializer( inline for (info.fields) |field| { if (!field.is_comptime) { - if (get_field_config(T, field)) |config| { + if (getFieldConfig(T, field)) |config| { if (config.skip) { continue; } @@ -680,7 +680,7 @@ pub fn FieldConfig(comptime T: type) type { }; } -pub fn get_field_config(comptime struct_type: type, comptime field: std.builtin.Type.StructField) ?FieldConfig(field.type) { +pub fn getFieldConfig(comptime struct_type: type, comptime field: std.builtin.Type.StructField) ?FieldConfig(field.type) { const bincode_field = "!bincode-config:" ++ field.name; if (@hasDecl(struct_type, bincode_field)) { const config = @field(struct_type, bincode_field); @@ -743,28 +743,6 @@ pub fn getSerializedSize(alloc: std.mem.Allocator, data: anytype, params: Params return list.items.len; } -pub fn getComptimeSize(comptime T: type) usize { - const U = @typeInfo(T); - var size: usize = 0; - switch (U) { - .Struct => |*info| { - inline for (info.fields) |field| { - if (get_field_config(T, field)) |config| { - if (config.skip or config.default_on_eof) { - continue; - } - } - size += getComptimeSize(field.type); - } - }, - else => { - // TODO: support other types better - size += @sizeOf(T); - }, - } - return size; -} - // can call if dont require an allocator pub fn readFromSlice(alloc: ?std.mem.Allocator, comptime T: type, slice: []const u8, params: Params) !T { var stream = std.io.fixedBufferStream(slice); @@ -815,22 +793,6 @@ fn TestSliceConfig(comptime Child: type) FieldConfig([]Child) { }; } -test "bincode: comptime size with skip" { - const Foo = struct { - value: u8 = 0, - pub const @"!bincode-config:value" = .{ .skip = true }; - }; - - const size = try getComptimeSize(Foo); - try std.testing.expect(size == 0); - - const Foo2 = struct { - value: u8, - }; - const size2 = try getComptimeSize(Foo2); - try std.testing.expect(size2 == 1); -} - test "bincode: default on eof" { const defaultArrayListOnEOFConfig = @import("../utils/arraylist.zig").defaultArrayListOnEOFConfig; const Foo = struct { diff --git a/src/core/snapshot_utils.zig b/src/core/append_vec.zig similarity index 97% rename from src/core/snapshot_utils.zig rename to src/core/append_vec.zig index 0e699d223..5339b4b20 100644 --- a/src/core/snapshot_utils.zig +++ b/src/core/append_vec.zig @@ -187,12 +187,12 @@ pub const AppendVec = struct { } pub fn getType(self: *const Self, start_index_ptr: *usize, comptime T: type) error{EOF}!*T { - const length = bincode.getComptimeSize(T); + const length = @sizeOf(T); return @alignCast(@ptrCast(try self.getSlice(start_index_ptr, length))); } }; -test "core.snapshot_utils: parse accounts out of append vec" { +test "core.append_vec: parse accounts out of append vec" { // to run this test // 1) run the test `core.snapshot_fields: parse snapshot fields` // - to build accounts_db.bincode file @@ -200,7 +200,7 @@ test "core.snapshot_utils: parse accounts out of append vec" { // 3) run the test const alloc = std.testing.allocator; - const accounts_db_fields_path = "/Users/tmp/Documents/zig-solana/snapshots/accounts_db.bincode"; + const accounts_db_fields_path = "/Users/tmp2/Documents/zig-solana/snapshots/accounts_db.bincode"; const accounts_db_fields_file = std.fs.openFileAbsolute(accounts_db_fields_path, .{}) catch |err| { std.debug.print("failed to open accounts-db fields file: {s} ... skipping test\n", .{@errorName(err)}); return; diff --git a/src/core/snapshot_fields.zig b/src/core/snapshot_fields.zig index edad95593..b21bf6312 100644 --- a/src/core/snapshot_fields.zig +++ b/src/core/snapshot_fields.zig @@ -316,7 +316,7 @@ test "core.snapshot_fields: parse snapshot fields" { // 4) run this // const snapshot_path = "/test_data/slot/slot"; - const snapshot_path = "/Users/tmp/Documents/zig-solana/snapshots/snapshots/225552163/225552163"; + const snapshot_path = "/Users/tmp2/Documents/zig-solana/snapshots/snapshots/225552163/225552163"; const alloc = std.testing.allocator; // open file diff --git a/src/lib.zig b/src/lib.zig index 6837a584c..3b2d4717a 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -16,7 +16,7 @@ pub const core = struct { pub usingnamespace @import("core/shred.zig"); pub usingnamespace @import("core/genesis_config.zig"); pub usingnamespace @import("core/snapshot_fields.zig"); - pub usingnamespace @import("core/snapshot_utils.zig"); + pub usingnamespace @import("core/append_vec.zig"); }; pub const gossip = struct {