Skip to content

Commit

Permalink
chore: use TitleCase for types
Browse files Browse the repository at this point in the history
Make them jump to the eye more easily
env -> Env
resource_type -> ResourceType
pid -> Pid
term -> Term
  • Loading branch information
rbino committed Aug 2, 2023
1 parent a1b6ed1 commit c5da384
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 121 deletions.
22 changes: 11 additions & 11 deletions src/account_batch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ const AccountFlags = tb.AccountFlags;
pub const AccountBatch = batch.Batch(Account);
pub const AccountBatchResource = batch.BatchResource(Account);

pub fn create(env: beam.env, argc: c_int, argv: [*c]const beam.term) callconv(.C) beam.term {
pub fn create(env: beam.Env, argc: c_int, argv: [*c]const beam.Term) callconv(.C) beam.Term {
assert(argc == 1);

const args = @ptrCast([*]const beam.term, argv)[0..@intCast(usize, argc)];
const args = @ptrCast([*]const beam.Term, argv)[0..@intCast(usize, argc)];

const capacity: u32 = beam.get_u32(env, args[0]) catch
return beam.raise_function_clause_error(env);

return batch.create(Account, env, capacity);
}

pub fn add_account(env: beam.env, argc: c_int, argv: [*c]const beam.term) callconv(.C) beam.term {
pub fn add_account(env: beam.Env, argc: c_int, argv: [*c]const beam.Term) callconv(.C) beam.Term {
assert(argc == 1);

const args = @ptrCast([*]const beam.term, argv)[0..@intCast(usize, argc)];
const args = @ptrCast([*]const beam.Term, argv)[0..@intCast(usize, argc)];

return batch.add_item(Account, env, args[0]) catch |err| switch (err) {
error.MutexLocked => return scheduler.reschedule(env, "add_account", add_account, argc, argv),
Expand All @@ -39,18 +39,18 @@ pub const set_account_code = field_setter_fn(.code);
pub const set_account_flags = field_setter_fn(.flags);

fn field_setter_fn(comptime field: std.meta.FieldEnum(Account)) fn (
beam.env,
beam.Env,
c_int,
[*c]const beam.term,
) callconv(.C) beam.term {
[*c]const beam.Term,
) callconv(.C) beam.Term {
const field_name = @tagName(field);
const setter_name = "set_account_" ++ field_name;

return struct {
fn setter_fn(env: beam.env, argc: c_int, argv: [*c]const beam.term) callconv(.C) beam.term {
fn setter_fn(env: beam.Env, argc: c_int, argv: [*c]const beam.Term) callconv(.C) beam.Term {
assert(argc == 3);

const args = @ptrCast([*]const beam.term, argv)[0..@intCast(usize, argc)];
const args = @ptrCast([*]const beam.Term, argv)[0..@intCast(usize, argc)];

const batch_term = args[0];
const account_batch_resource = AccountBatchResource.from_term_handle(env, batch_term) catch |err|
Expand Down Expand Up @@ -88,7 +88,7 @@ fn field_setter_fn(comptime field: std.meta.FieldEnum(Account)) fn (

fn term_to_value_fn(
comptime field: std.meta.FieldEnum(Account),
) fn (beam.env, beam.term) beam.GetError!std.meta.fieldInfo(Account, field).field_type {
) fn (beam.Env, beam.Term) beam.GetError!std.meta.fieldInfo(Account, field).field_type {
return switch (field) {
.id, .user_data => beam.get_u128,
.ledger => beam.get_u32,
Expand All @@ -98,7 +98,7 @@ fn term_to_value_fn(
};
}

fn term_to_account_flags(env: beam.env, term: beam.term) beam.GetError!AccountFlags {
fn term_to_account_flags(env: beam.Env, term: beam.Term) beam.GetError!AccountFlags {
const flags_uint = beam.get_u16(env, term) catch
return beam.GetError.ArgumentError;

Expand Down
8 changes: 4 additions & 4 deletions src/batch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn Batch(comptime T: anytype) type {
};
}

pub fn create(comptime T: anytype, env: beam.env, capacity: u32) beam.term {
pub fn create(comptime T: anytype, env: beam.Env, capacity: u32) beam.Term {
const items = beam.general_purpose_allocator.alloc(T, capacity) catch |err|
switch (err) {
error.OutOfMemory => return beam.make_error_atom(env, "out_of_memory"),
Expand All @@ -37,7 +37,7 @@ pub fn create(comptime T: anytype, env: beam.env, capacity: u32) beam.term {
return beam.make_ok_term(env, term_handle);
}

pub fn add_item(comptime T: anytype, env: beam.env, batch_term: beam.term) !beam.term {
pub fn add_item(comptime T: anytype, env: beam.Env, batch_term: beam.Term) !beam.Term {
const batch_resource = BatchResource(T).from_term_handle(env, batch_term) catch |err|
switch (err) {
error.InvalidResourceTerm => return beam.make_error_atom(env, "invalid_batch"),
Expand Down Expand Up @@ -65,9 +65,9 @@ pub fn BatchResource(comptime T: anytype) type {

fn batch_resource_deinit_fn(
comptime T: anytype,
) fn (env: beam.env, ptr: ?*anyopaque) callconv(.C) void {
) fn (env: beam.Env, ptr: ?*anyopaque) callconv(.C) void {
return struct {
fn deinit_fn(_: beam.env, ptr: ?*anyopaque) callconv(.C) void {
fn deinit_fn(_: beam.Env, ptr: ?*anyopaque) callconv(.C) void {
if (ptr) |p| {
const batch: *T = @ptrCast(*T, @alignCast(@alignOf(*T), p));
beam.general_purpose_allocator.free(batch.items);
Expand Down
102 changes: 51 additions & 51 deletions src/beam.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub const resource = @import("beam/resource.zig");
pub const scheduler = @import("beam/scheduler.zig");

pub const Binary = @import("beam/Binary.zig");
pub const env = ?*e.ErlNifEnv;
pub const pid = e.ErlNifPid;
pub const resource_type = ?*e.ErlNifResourceType;
pub const term = e.ERL_NIF_TERM;
pub const Env = ?*e.ErlNifEnv;
pub const Pid = e.ErlNifPid;
pub const ResourceType = ?*e.ErlNifResourceType;
pub const Term = e.ERL_NIF_TERM;

/// The raw BEAM allocator with the standard Zig Allocator interface.
pub const raw_allocator = allocator.raw_allocator;
Expand All @@ -26,108 +26,108 @@ pub const large_allocator = allocator.large_allocator;
pub const general_purpose_allocator = allocator.general_purpose_allocator;

/// Raises a generic exception
pub fn raise(env_: env, reason: []const u8) term {
return e.enif_raise_exception(env_, make_atom(env_, reason));
pub fn raise(env: Env, reason: []const u8) Term {
return e.enif_raise_exception(env, make_atom(env, reason));
}

/// Raises a `:function_clause` exception
pub fn raise_function_clause_error(env_: env) term {
return raise(env_, "function_clause");
pub fn raise_function_clause_error(env: Env) Term {
return raise(env, "function_clause");
}

/// Creates a ref
pub fn make_ref(env_: env) term {
return e.enif_make_ref(env_);
pub fn make_ref(env: Env) Term {
return e.enif_make_ref(env);
}

/// Creates an atom from a Zig char slice
pub fn make_atom(env_: env, atom_str: []const u8) term {
return e.enif_make_atom_len(env_, atom_str.ptr, atom_str.len);
pub fn make_atom(env: Env, atom_str: []const u8) Term {
return e.enif_make_atom_len(env, atom_str.ptr, atom_str.len);
}

/// Creates a beam `nil` atom
pub fn make_nil(env_: env) term {
return e.enif_make_atom(env_, "nil");
pub fn make_nil(env: Env) Term {
return e.enif_make_atom(env, "nil");
}

/// Creates a beam `:ok` atom
pub fn make_ok(env_: env) term {
return e.enif_make_atom(env_, "ok");
pub fn make_ok(env: Env) Term {
return e.enif_make_atom(env, "ok");
}

/// Helper to create an `{:ok, term}` tuple
pub fn make_ok_term(env_: env, val: term) term {
return e.enif_make_tuple(env_, 2, make_ok(env_), val);
pub fn make_ok_term(env: Env, val: Term) Term {
return e.enif_make_tuple(env, 2, make_ok(env), val);
}

/// Helper to create an `{:ok, atom}` tuple, taking the atom value from a slice
pub fn make_ok_atom(env_: env, atom_str: []const u8) term {
return make_ok_term(env_, make_atom(env_, atom_str));
pub fn make_ok_atom(env: Env, atom_str: []const u8) Term {
return make_ok_term(env, make_atom(env, atom_str));
}

/// Creates a beam `:error` atom.
pub fn make_error(env_: env) term {
return e.enif_make_atom(env_, "error");
pub fn make_error(env: Env) Term {
return e.enif_make_atom(env, "error");
}

/// Helper to create an `{:error, term}` tuple
pub fn make_error_term(env_: env, val: term) term {
return e.enif_make_tuple(env_, 2, make_error(env_), val);
pub fn make_error_term(env: Env, val: Term) Term {
return e.enif_make_tuple(env, 2, make_error(env), val);
}

/// Helper to create an `{:error, atom}` tuple, taking the atom value from a slice
pub fn make_error_atom(env_: env, atom_str: []const u8) term {
return make_error_term(env_, make_atom(env_, atom_str));
pub fn make_error_atom(env: Env, atom_str: []const u8) Term {
return make_error_term(env, make_atom(env, atom_str));
}

/// Creates a binary term from a Zig slice
pub fn make_slice(environment: env, val: []const u8) term {
var result: term = undefined;
var bin: [*]u8 = @ptrCast([*]u8, e.enif_make_new_binary(environment, val.len, &result));
pub fn make_slice(env: Env, val: []const u8) Term {
var result: Term = undefined;
var bin: [*]u8 = @ptrCast([*]u8, e.enif_make_new_binary(env, val.len, &result));
std.mem.copy(u8, bin[0..val.len], val);

return result;
}

/// Creates a u8 value term.
pub fn make_u8(env_: env, val: u8) term {
return e.enif_make_uint(env_, val);
pub fn make_u8(env: Env, val: u8) Term {
return e.enif_make_uint(env, val);
}

/// Creates a u32 value term.
pub fn make_u32(env_: env, val: u32) term {
return e.enif_make_uint(env_, val);
pub fn make_u32(env: Env, val: u32) Term {
return e.enif_make_uint(env, val);
}

/// Creates an BEAM tuple from a Zig tuple of terms
pub fn make_tuple(env_: env, tuple: anytype) term {
pub fn make_tuple(env: Env, tuple: anytype) Term {
const type_info = @typeInfo(@TypeOf(tuple));
if (type_info != .Struct or !type_info.Struct.is_tuple)
@compileError("invalid argument to make_tuple: not a tuple");

var tuple_list: [tuple.len]term = undefined;
var tuple_list: [tuple.len]Term = undefined;
inline for (tuple_list) |*tuple_item, index| {
const tuple_term = tuple[index];
tuple_item.* = tuple_term;
}
return e.enif_make_tuple_from_array(env_, &tuple_list, tuple.len);
return e.enif_make_tuple_from_array(env, &tuple_list, tuple.len);
}

pub const GetError = error{ArgumentError};

/// Extract a binary from a term, returning it as a slice
pub fn get_char_slice(env_: env, src_term: term) GetError![]u8 {
pub fn get_char_slice(env: Env, src_term: Term) GetError![]u8 {
var bin: e.ErlNifBinary = undefined;
if (e.enif_inspect_binary(env_, src_term, &bin) == 0) {
if (e.enif_inspect_binary(env, src_term, &bin) == 0) {
return GetError.ArgumentError;
}

return bin.data[0..bin.size];
}

/// Extract a u128 from a binary (little endian) term
pub fn get_u128(env_: env, src_term: term) GetError!u128 {
const bin = try get_char_slice(env_, src_term);
pub fn get_u128(env: Env, src_term: Term) GetError!u128 {
const bin = try get_char_slice(env, src_term);
const required_length = @sizeOf(u128) / @sizeOf(u8);

// We represent the u128 as a 16 byte binary, little endian (required by TigerBeetle)
Expand All @@ -137,29 +137,29 @@ pub fn get_u128(env_: env, src_term: term) GetError!u128 {
}

/// Extract a u64 from a term
pub fn get_u64(env_: env, src_term: term) GetError!u64 {
pub fn get_u64(env: Env, src_term: Term) GetError!u64 {
var result: c_ulong = undefined;
if (e.enif_get_ulong(env_, src_term, &result) == 0) {
if (e.enif_get_ulong(env, src_term, &result) == 0) {
return GetError.ArgumentError;
}

return @intCast(u64, result);
}

/// Extract a u32 from a term
pub fn get_u32(env_: env, src_term: term) GetError!u32 {
pub fn get_u32(env: Env, src_term: Term) GetError!u32 {
var result: c_uint = undefined;
if (e.enif_get_uint(env_, src_term, &result) == 0) {
if (e.enif_get_uint(env, src_term, &result) == 0) {
return GetError.ArgumentError;
}

return @intCast(u32, result);
}

/// Extract a u16 from a term, checking it does not go outside the boundaries
pub fn get_u16(env_: env, src_term: term) GetError!u16 {
pub fn get_u16(env: Env, src_term: Term) GetError!u16 {
var result: c_uint = undefined;
if (e.enif_get_uint(env_, src_term, &result) == 0) {
if (e.enif_get_uint(env, src_term, &result) == 0) {
return GetError.ArgumentError;
}

Expand All @@ -173,21 +173,21 @@ pub fn get_u16(env_: env, src_term: term) GetError!u16 {
pub const TermToBinaryError = error{OutOfMemory};

/// Serializes a term to a beam.Binary
pub fn term_to_binary(env_: env, src_term: term) TermToBinaryError!Binary {
pub fn term_to_binary(env: Env, src_term: Term) TermToBinaryError!Binary {
var bin: e.ErlNifBinary = undefined;
if (e.enif_term_to_binary(env_, src_term, &bin) == 0) {
if (e.enif_term_to_binary(env, src_term, &bin) == 0) {
return error.OutOfMemory;
}

return Binary{ .binary = bin };
}

/// Allocates a process independent environment
pub fn alloc_env() env {
pub fn alloc_env() Env {
return e.enif_alloc_env();
}

/// Clears a process independent environment
pub fn clear_env(env_: env) void {
e.enif_clear_env(env_);
pub fn clear_env(env: Env) void {
e.enif_clear_env(env);
}
9 changes: 4 additions & 5 deletions src/beam/Binary.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const e = @import("../erl_nif.zig");
const env = @import("../beam.zig").env;
const term = @import("../beam.zig").term;
const beam = @import("../beam.zig");
const Self = @This();

binary: e.ErlNifBinary,
Expand All @@ -11,9 +10,9 @@ pub fn slice(self: Self) []const u8 {

pub const ToTermError = error{InvalidBinaryTerm};

pub fn to_term(self: Self, env_: env) ToTermError!term {
var result: term = undefined;
if (e.enif_binary_to_term(env_, self.binary.data, self.binary.size, &result, 0) == 0) {
pub fn to_term(self: Self, env: beam.Env) ToTermError!beam.Term {
var result: beam.Term = undefined;
if (e.enif_binary_to_term(env, self.binary.data, self.binary.size, &result, 0) == 0) {
return error.InvalidBinaryTerm;
}

Expand Down
14 changes: 7 additions & 7 deletions src/beam/resource.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ pub const Error = error{
OutOfMemory,
};

const DeinitFn = *const fn (beam.env, ?*anyopaque) callconv(.C) void;
const DeinitFn = *const fn (beam.Env, ?*anyopaque) callconv(.C) void;

pub fn Resource(comptime T: anytype, comptime deinit_fn: ?DeinitFn) type {
return struct {
const Self = @This();

const Type = struct {
beam_type: beam.resource_type,
beam_type: beam.ResourceType,

pub fn open(env: beam.env) Type {
pub fn open(env: beam.Env) Type {
const beam_type = e.enif_open_resource_type(
env,
null,
Expand All @@ -40,7 +40,7 @@ pub fn Resource(comptime T: anytype, comptime deinit_fn: ?DeinitFn) type {

/// Initializes the type of the resource. This must be called exactly once
/// in the load or upgrade callback of the NIF.
pub fn create_type(env: beam.env) void {
pub fn create_type(env: beam.Env) void {
// TODO: is this required or are we allowed to re-open a type?
assert(resource_type == null);

Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn Resource(comptime T: anytype, comptime deinit_fn: ?DeinitFn) type {
}

/// Recreates the resource from the term handle obtained with `term_handle`
pub fn from_term_handle(env: beam.env, term: beam.term) !Self {
pub fn from_term_handle(env: beam.Env, term: beam.Term) !Self {
var raw_ptr: ?*anyopaque = undefined;

if (0 == e.enif_get_resource(env, term, res_type(), &raw_ptr)) {
Expand All @@ -80,7 +80,7 @@ pub fn Resource(comptime T: anytype, comptime deinit_fn: ?DeinitFn) type {
}

/// Obtains a term handle to the resource
pub fn term_handle(self: Self, env: beam.env) beam.term {
pub fn term_handle(self: Self, env: beam.Env) beam.Term {
return e.enif_make_resource(env, self.raw_ptr);
}

Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn Resource(comptime T: anytype, comptime deinit_fn: ?DeinitFn) type {
return @ptrCast(*T, @alignCast(@alignOf(*T), self.raw_ptr));
}

fn res_type() beam.resource_type {
fn res_type() beam.ResourceType {
return resource_type.?.beam_type;
}
};
Expand Down
Loading

0 comments on commit c5da384

Please sign in to comment.