Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNineteen committed Oct 25, 2023
1 parent 6c4a612 commit 04ede14
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 47 deletions.
46 changes: 4 additions & 42 deletions src/bincode/bincode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/core/snapshot_utils.zig → src/core/append_vec.zig
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,20 @@ 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
// 2) change paths for `accounts_db_fields_path` and `accounts_dir_path`
// 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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/snapshot_fields.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 04ede14

Please sign in to comment.