Skip to content

Commit

Permalink
chore: inline std.meta.trait and fix some incompatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Nov 29, 2023
1 parent fc3b255 commit f1b3ff9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
30 changes: 20 additions & 10 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
const std = @import("std");
const meta = std.meta;
const trait = meta.trait;
const assert = std.debug.assert;
const utils = @import("./utils.zig");
const Field = std.builtin.Type.StructField;
const testing = std.testing;

// See https://github.com/ziglang/zig/pull/18061
pub const TraitFn = fn (type) callconv(.Inline) bool;
pub fn is(comptime id: std.builtin.TypeId) TraitFn {
const Closure = struct {
pub inline fn trait(comptime T: type) bool {
return id == @typeInfo(T);
}
};
return Closure.trait;
}

/// Error
pub const Error = error{
/// Env variable is not existed.
Expand Down Expand Up @@ -79,7 +89,7 @@ pub const StructEnv = struct {
break :blk key;
};

var buf = self.allocator.alloc(u8, new_key.len) catch return "";
const buf = self.allocator.alloc(u8, new_key.len) catch return "";
defer self.allocator.free(buf);

const upper_key = std.ascii.upperString(buf, new_key);
Expand All @@ -101,7 +111,7 @@ pub const StructEnv = struct {
_ = self;
if (field.default_value) |default_value| {
const anyopaque_pointer: *anyopaque = @constCast(default_value);
return @as(*T, @ptrCast(@alignCast( anyopaque_pointer))).*;
return @as(*T, @ptrCast(@alignCast(anyopaque_pointer))).*;
}

return null;
Expand All @@ -110,7 +120,7 @@ pub const StructEnv = struct {
/// Deserialize into a value
fn deserializeInto(self: *Self, ptr: anytype, comptime field: ?Field) !void {
const T = @TypeOf(ptr);
comptime assert(trait.is(.Pointer)(T));
comptime assert(is(.Pointer)(T));

const C = comptime meta.Child(T);

Expand Down Expand Up @@ -171,7 +181,7 @@ pub const StructEnv = struct {

/// Deserialize a boole
fn deserializeBool(self: *Self, comptime T: type, comptime field: Field) !T {
var value = self.getEnv(field.name);
const value = self.getEnv(field.name);
if (value) |v| {
return try utils.str2bool(self.allocator, v);
}
Expand Down Expand Up @@ -277,29 +287,29 @@ pub const StructEnv = struct {
pub fn fromEnv(allocator: std.mem.Allocator, comptime T: type) !T {
var e = StructEnv.init(allocator, null);
defer e.deinit();
var v = try e.fromEnv(T);
const v = try e.fromEnv(T);
return v;
}

/// Load env with common prefix and return a value of the specified type T.
pub fn fromPrefixedEnv(allocator: std.mem.Allocator, comptime T: type, comptime prefix: []const u8) !T {
var e = StructEnv.init(allocator, prefix);
defer e.deinit();
var v = try e.fromEnv(T);
const v = try e.fromEnv(T);
return v;
}

fn fromEnvMock(allocator: std.mem.Allocator, comptime T: type, env_map: std.process.EnvMap) !T {
var e = StructEnv.init(allocator, null);
defer e.deinit();
var v = try e.fromEnvMock(T, env_map);
const v = try e.fromEnvMock(T, env_map);
return v;
}

fn fromPrefixedEnvMock(allocator: std.mem.Allocator, comptime T: type, env_map: std.process.EnvMap, comptime prefix: []const u8) !T {
var e = StructEnv.init(allocator, prefix);
defer e.deinit();
var v = try e.fromEnvMock(T, env_map);
const v = try e.fromEnvMock(T, env_map);
return v;
}

Expand All @@ -313,7 +323,7 @@ pub fn free(allocator: std.mem.Allocator, value: anytype) void {
else => {
switch (@typeInfo(struct_field.type)) {
.Pointer => {
var need_free = true;
const need_free = true;
// const cur = @field(value, struct_field.name);

// if (struct_field.default_value) |default_value| {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");

pub fn str2bool(allocator: std.mem.Allocator, s: []const u8) !bool {
var buf = try allocator.alloc(u8, s.len);
const buf = try allocator.alloc(u8, s.len);
defer allocator.free(buf);

const truth = std.ascii.upperString(buf, s);
Expand Down

0 comments on commit f1b3ff9

Please sign in to comment.