From 765c82671a733991c46f3d3e14297bc94d6b56c3 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Fri, 8 Jun 2018 10:41:37 +0200 Subject: Updated to newest pointer syntax --- build.zig | 2 +- examples/core.zig | 26 +++++++++++++------------- src/core.zig | 26 +++++++++++++------------- src/extended.zig | 37 +++++++++++++++++-------------------- tests/extended.zig | 4 ++-- 5 files changed, 46 insertions(+), 49 deletions(-) diff --git a/build.zig b/build.zig index 847a3a2..a32c0bd 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,6 @@ const Builder = @import("std").build.Builder; -pub fn build(b: &Builder) void { +pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); { diff --git a/examples/core.zig b/examples/core.zig index eb512ef..4335ff2 100644 --- a/examples/core.zig +++ b/examples/core.zig @@ -10,7 +10,7 @@ const Param = clap.Param; const ArgError = clap.OsArgIterator.Error; // TODO: More specific error in this func type. -const Command = fn(&mem.Allocator, &clap.ArgIterator(ArgError)) error!void; +const Command = fn(*mem.Allocator, *clap.ArgIterator(ArgError)) error!void; const params = []Param(Command){ Param(Command).init(help, false, Names.prefix("help")), @@ -74,7 +74,7 @@ pub fn main() !void { try arg.param.id(allocator, parser.iter); } -pub fn help(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +pub fn help(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { // debug.warn(usage); TODO: error: evaluation exceeded 1000 backwards branches } @@ -147,7 +147,7 @@ const missing_build_file = \\ ; -fn cmdBuild(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) !void { +fn cmdBuild(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) !void { var init = false; var build_file: []const u8 = "build.zig"; var cache_dir: []const u8 = "zig-cache"; @@ -323,17 +323,17 @@ const build_generic_usage = ; -fn cmdBuildExe(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdBuildExe(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:build-lib /////////////////////////////////////////////////////////////////////////////////// -fn cmdBuildLib(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdBuildLib(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:build-obj /////////////////////////////////////////////////////////////////////////////////// -fn cmdBuildObj(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdBuildObj(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:fmt ///////////////////////////////////////////////////////////////////////////////////////// @@ -350,17 +350,17 @@ const usage_fmt = \\ ; -fn cmdFmt(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdFmt(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:targets ///////////////////////////////////////////////////////////////////////////////////// -fn cmdTargets(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdTargets(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:version ///////////////////////////////////////////////////////////////////////////////////// -fn cmdVersion(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdVersion(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:test //////////////////////////////////////////////////////////////////////////////////////// @@ -374,7 +374,7 @@ const usage_test = \\ ; -fn cmdTest(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdTest(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:run ///////////////////////////////////////////////////////////////////////////////////////// @@ -390,7 +390,7 @@ const usage_run = \\ ; -fn cmdRun(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdRun(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:translate-c ///////////////////////////////////////////////////////////////////////////////// @@ -428,7 +428,7 @@ const info_zen = \\ ; -fn cmdZen(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdZen(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } // cmd:internal //////////////////////////////////////////////////////////////////////////////////// @@ -442,5 +442,5 @@ const usage_internal = \\ ; -fn cmdInternal(allocator: &mem.Allocator, args: &clap.ArgIterator(ArgError)) (error{}!void) { +fn cmdInternal(allocator: *mem.Allocator, args: *clap.ArgIterator(ArgError)) (error{}!void) { } diff --git a/src/core.zig b/src/core.zig index f2e1fe0..d870e8c 100644 --- a/src/core.zig +++ b/src/core.zig @@ -99,7 +99,7 @@ pub fn Param(comptime Id: type) type { takes_value: bool, names: Names, - pub fn init(id: Id, takes_value: bool, names: &const Names) Self { + pub fn init(id: Id, takes_value: bool, names: *const Names) Self { // Assert, that if the param have no name, then it has to take // a value. debug.assert( @@ -123,10 +123,10 @@ pub fn Arg(comptime Id: type) type { return struct { const Self = this; - param: &const Param(Id), + param: *const Param(Id), value: ?[]const u8, - pub fn init(param: &const Param(Id), value: ?[]const u8) Self { + pub fn init(param: *const Param(Id), value: ?[]const u8) Self { return Self { .param = param, .value = value, @@ -141,9 +141,9 @@ pub fn ArgIterator(comptime E: type) type { const Self = this; const Error = E; - nextFn: fn(iter: &Self) Error!?[]const u8, + nextFn: fn(iter: *Self) Error!?[]const u8, - pub fn next(iter: &Self) Error!?[]const u8 { + pub fn next(iter: *Self) Error!?[]const u8 { return iter.nextFn(iter); } }; @@ -168,7 +168,7 @@ pub const ArgSliceIterator = struct { }; } - fn nextFn(iter: &ArgIterator(Error)) Error!?[]const u8 { + fn nextFn(iter: *ArgIterator(Error)) Error!?[]const u8 { const self = @fieldParentPtr(ArgSliceIterator, "iter", iter); if (self.args.len <= self.index) return null; @@ -187,7 +187,7 @@ pub const OsArgIterator = struct { args: os.ArgIterator, iter: ArgIterator(Error), - pub fn init(allocator: &mem.Allocator) OsArgIterator { + pub fn init(allocator: *mem.Allocator) OsArgIterator { return OsArgIterator { .arena = heap.ArenaAllocator.init(allocator), .args = os.args(), @@ -197,11 +197,11 @@ pub const OsArgIterator = struct { }; } - pub fn deinit(iter: &OsArgIterator) void { + pub fn deinit(iter: *OsArgIterator) void { iter.arena.deinit(); } - fn nextFn(iter: &ArgIterator(Error)) Error!?[]const u8 { + fn nextFn(iter: *ArgIterator(Error)) Error!?[]const u8 { const self = @fieldParentPtr(OsArgIterator, "iter", iter); if (builtin.os == builtin.Os.windows) { return try self.args.next(self.allocator) ?? return null; @@ -229,10 +229,10 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { }; params: []const Param(Id), - iter: &ArgIterator(ArgError), + iter: *ArgIterator(ArgError), state: State, - pub fn init(params: []const Param(Id), iter: &ArgIterator(ArgError)) Self { + pub fn init(params: []const Param(Id), iter: *ArgIterator(ArgError)) Self { var res = Self { .params = params, .iter = iter, @@ -243,7 +243,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { } /// Get the next ::Arg that matches a ::Param. - pub fn next(clap: &Self) !?Arg(Id) { + pub fn next(clap: *Self) !?Arg(Id) { const ArgInfo = struct { const Kind = enum { Long, Short, Bare }; @@ -334,7 +334,7 @@ pub fn Clap(comptime Id: type, comptime ArgError: type) type { } } - fn chainging(clap: &Self, state: &const State.Chaining) !?Arg(Id) { + fn chainging(clap: *Self, state: *const State.Chaining) !?Arg(Id) { const arg = state.arg; const index = state.index; const next_index = index + 1; diff --git a/src/extended.zig b/src/extended.zig index ffcce5b..8fb017f 100644 --- a/src/extended.zig +++ b/src/extended.zig @@ -29,25 +29,25 @@ pub const Command = struct { params: []const Param, Result: type, - default: &const Opaque, + default: *const Opaque, - pub fn init(comptime Result: type, default: &const Result, params: []const Param) Command { + pub fn init(comptime Result: type, default: *const Result, params: []const Param) Command { return Command{ .params = params, .Result = Result, - .default = @ptrCast(&const Opaque, default), + .default = @ptrCast(*const Opaque, default), }; } }; pub const Parser = struct { - const UnsafeFunction = &const void; + const UnsafeFunction = *const void; FieldType: type, Errors: type, func: UnsafeFunction, - pub fn init(comptime FieldType: type, comptime Errors: type, func: parseFunc(FieldType, Errors)) Parser { + pub fn init(comptime FieldType: type, comptime Errors: type, func: ParseFunc(FieldType, Errors)) Parser { return Parser { .FieldType = FieldType, .Errors = Errors, @@ -55,20 +55,17 @@ pub const Parser = struct { }; } - fn parse(comptime parser: Parser, field_ptr: TakePtr(parser.FieldType), arg: []const u8) parser.Errors!void { - return @ptrCast(parseFunc(parser.FieldType, parser.Errors), parser.func)(field_ptr, arg); + fn parse(comptime parser: Parser, field_ptr: *parser.FieldType, arg: []const u8) parser.Errors!void { + return @ptrCast(ParseFunc(parser.FieldType, parser.Errors), parser.func)(field_ptr, arg); } - // TODO: This is a workaround, since we don't have pointer reform yet. - fn TakePtr(comptime T: type) type { return &T; } - - fn parseFunc(comptime FieldType: type, comptime Errors: type) type { - return fn(&FieldType, []const u8) Errors!void; + fn ParseFunc(comptime FieldType: type, comptime Errors: type) type { + return fn(*FieldType, []const u8) Errors!void; } pub fn int(comptime Int: type, comptime radix: u8) Parser { const func = struct { - fn i(field_ptr: &Int, arg: []const u8) !void { + fn i(field_ptr: *Int, arg: []const u8) !void { field_ptr.* = try fmt.parseInt(Int, arg, radix); } }.i; @@ -83,7 +80,7 @@ pub const Parser = struct { []const u8, error{}, struct { - fn s(field_ptr: &[]const u8, arg: []const u8) (error{}!void) { + fn s(field_ptr: *[]const u8, arg: []const u8) (error{}!void) { field_ptr.* = arg; } }.s @@ -98,23 +95,23 @@ pub fn Clap(comptime Result: type) type { params: []const Param, pub fn parse( - comptime clap: &const Self, + comptime clap: *const Self, comptime Error: type, - iter: &core.ArgIterator(Error), + iter: *core.ArgIterator(Error), ) !Result { // We initialize the core.Clap without any params, and fill them out in parseHelper. var c = core.Clap(usize, Error).init([]core.Param(usize){}, iter); const top_level_command = comptime Command.init(Result, &clap.default, clap.params); - return try parseHelper(top_level_command, Error, &c); + return try parseHelper(&top_level_command, Error, &c); } fn parseHelper( - comptime command: &const Command, + comptime command: *const Command, comptime Error: type, - clap: &core.Clap(usize, Error), + clap: *core.Clap(usize, Error), ) !command.Result { - var result = @ptrCast(&const command.Result, command.default).*; + var result = @ptrCast(*const command.Result, command.default).*; var handled = comptime blk: { var res: [command.params.len]bool = undefined; diff --git a/tests/extended.zig b/tests/extended.zig index 140c822..00cc84b 100644 --- a/tests/extended.zig +++ b/tests/extended.zig @@ -45,7 +45,7 @@ pub fn Test(comptime Expect: type) type { Fail: error, }; - pub fn success(args: []const []const u8, expected: &const Expect) Self { + pub fn success(args: []const []const u8, expected: *const Expect) Self { return Self{ .args = args, .kind = Kind{ @@ -63,7 +63,7 @@ pub fn Test(comptime Expect: type) type { }; } - pub fn run(t: &const Self, comptime parser: var) void { + pub fn run(t: *const Self, comptime parser: var) void { var iter = ArgSliceIterator.init(t.args); const actual = parser.parse(ArgSliceIterator.Error, &iter.iter); -- cgit v1.2.3