Skip to content

std interface reform (#1829): simple #2553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c365241
random interface changes.
tgschultz May 23, 2019
1e44015
tests passing
tgschultz May 23, 2019
ce427de
random interface complete.
tgschultz May 24, 2019
5b79723
initial allocator converion. ready to start sedding it up.
tgschultz May 24, 2019
5d92021
first sedding done.
tgschultz May 24, 2019
c07561a
updated failing_allocator
tgschultz May 24, 2019
dbf8964
second sedding done
tgschultz May 24, 2019
9971728
async sedding in progress
tgschultz May 24, 2019
e927b53
async sedding done
tgschultz May 24, 2019
4e06e7f
heap tests passing
tgschultz May 24, 2019
7d5e64f
about to perform questionable sed
tgschultz May 24, 2019
25dbd8a
std tests passing
tgschultz May 24, 2019
f98e7d7
integration tests passing.
tgschultz May 24, 2019
ea0c964
initial stream rework
tgschultz May 25, 2019
c0cab2b
minor changes to file
tgschultz May 25, 2019
f5375df
handle => adapter
tgschultz May 25, 2019
d0c17be
file adapters
tgschultz May 25, 2019
d065e3c
outStreamAdapter.stream fix
tgschultz May 25, 2019
20d6255
inStreamAdapter.stream fixes
tgschultz May 25, 2019
25cd7b8
sedded up "obvious" in and out streams
tgschultz May 25, 2019
7ce48a5
work on io.zig
tgschultz May 25, 2019
e00ce6d
hopefully most stream changes complete
tgschultz May 25, 2019
24fe1d6
file stream adapter types rename
tgschultz May 25, 2019
2825769
sed File.(In|Out)Stream => Adapter
tgschultz May 25, 2019
6acb5d3
stream pointers to non-pointers
tgschultz May 25, 2019
dc58515
*Stream => Stream
tgschultz May 25, 2019
b3917dd
pre-sedding for *_\._Stream
tgschultz May 25, 2019
fa8c1b3
io tests passing
tgschultz May 25, 2019
d43f613
std tests passing
tgschultz May 25, 2019
77c7b95
all integration tests passing (at least for Linux)
tgschultz May 25, 2019
2633f63
fixed windows child_process. missed a conversion.
tgschultz May 25, 2019
b03c4b2
OMG 1 letter typo
tgschultz May 25, 2019
3120112
std.Interface
May 31, 2019
2a4d2b5
Replace boilerplace with new Interface()
Jun 1, 2019
703d6a8
Embed optional into Interface.impl
Jun 3, 2019
872dfa8
Merge pull request #12 from fengb/stdlib-interface_reform_simple_extr…
tgschultz Jun 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ set(ZIG_STD_FILES
"hash/siphash.zig"
"hash_map.zig"
"heap.zig"
"interface.zig"
"io.zig"
"io/seekable_stream.zig"
"io/c_out_stream.zig"
Expand Down
36 changes: 18 additions & 18 deletions doc/docgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub fn main() !void {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();

var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator);
var arena = std.heap.ArenaAllocator.init(direct_allocator.allocator());
defer arena.deinit();

const allocator = &arena.allocator;
const allocator = arena.allocator();

var args_it = os.args();

Expand All @@ -41,20 +41,20 @@ pub fn main() !void {
var out_file = try os.File.openWrite(out_file_name);
defer out_file.close();

var file_in_stream = in_file.inStream();
var file_in_stream = in_file.inStreamAdapter();

const input_file_bytes = try file_in_stream.stream.readAllAlloc(allocator, max_doc_file_size);
const input_file_bytes = try file_in_stream.inStream().readAllAlloc(allocator, max_doc_file_size);

var file_out_stream = out_file.outStream();
var buffered_out_stream = io.BufferedOutStream(os.File.WriteError).init(&file_out_stream.stream);
var file_out_stream = out_file.outStreamAdapter();
var buffered_out_stream = io.BufferedOutStream(os.File.WriteError).init(file_out_stream.outStream());

var tokenizer = Tokenizer.init(in_file_name, input_file_bytes);
var toc = try genToc(allocator, &tokenizer);

try os.makePath(allocator, tmp_dir_name);
defer os.deleteTree(allocator, tmp_dir_name) catch {};

try genHtml(allocator, &tokenizer, &toc, &buffered_out_stream.stream, zig_exe);
try genHtml(allocator, &tokenizer, &toc, buffered_out_stream.outStream(), zig_exe);
try buffered_out_stream.flush();
}

Expand Down Expand Up @@ -316,7 +316,7 @@ const Action = enum {
Close,
};

fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
fn genToc(allocator: mem.Allocator, tokenizer: *Tokenizer) !Toc {
var urls = std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8).init(allocator);
errdefer urls.deinit();

Expand All @@ -327,7 +327,7 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
defer toc_buf.deinit();

var toc_buf_adapter = io.BufferOutStream.init(&toc_buf);
var toc = &toc_buf_adapter.stream;
var toc = toc_buf_adapter.outStream();

var nodes = std.ArrayList(Node).init(allocator);
defer nodes.deinit();
Expand Down Expand Up @@ -569,12 +569,12 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
};
}

fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {
fn urlize(allocator: mem.Allocator, input: []const u8) ![]u8 {
var buf = try std.Buffer.initSize(allocator, 0);
defer buf.deinit();

var buf_adapter = io.BufferOutStream.init(&buf);
var out = &buf_adapter.stream;
var out = buf_adapter.outStream();
for (input) |c| {
switch (c) {
'a'...'z', 'A'...'Z', '_', '-', '0'...'9' => {
Expand All @@ -589,12 +589,12 @@ fn urlize(allocator: *mem.Allocator, input: []const u8) ![]u8 {
return buf.toOwnedSlice();
}

fn escapeHtml(allocator: *mem.Allocator, input: []const u8) ![]u8 {
fn escapeHtml(allocator: mem.Allocator, input: []const u8) ![]u8 {
var buf = try std.Buffer.initSize(allocator, 0);
defer buf.deinit();

var buf_adapter = io.BufferOutStream.init(&buf);
var out = &buf_adapter.stream;
var out = buf_adapter.outStream();
try writeEscaped(out, input);
return buf.toOwnedSlice();
}
Expand Down Expand Up @@ -635,12 +635,12 @@ test "term color" {
testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result);
}

fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {
fn termColor(allocator: mem.Allocator, input: []const u8) ![]u8 {
var buf = try std.Buffer.initSize(allocator, 0);
defer buf.deinit();

var buf_adapter = io.BufferOutStream.init(&buf);
var out = &buf_adapter.stream;
var out = buf_adapter.outStream();
var number_start_index: usize = undefined;
var first_number: usize = undefined;
var second_number: usize = undefined;
Expand Down Expand Up @@ -947,7 +947,7 @@ fn tokenizeAndPrint(docgen_tokenizer: *Tokenizer, out: var, source_token: Token)
return tokenizeAndPrintRaw(docgen_tokenizer, out, source_token, raw_src);
}

fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void {
fn genHtml(allocator: mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void {
var code_progress_index: usize = 0;

var env_map = try os.getEnvMap(allocator);
Expand Down Expand Up @@ -1448,7 +1448,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
}
}

fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !os.ChildProcess.ExecResult {
fn exec(allocator: mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !os.ChildProcess.ExecResult {
const result = try os.ChildProcess.exec(allocator, args, null, env_map, max_doc_file_size);
switch (result.term) {
os.ChildProcess.Term.Exited => |exit_code| {
Expand All @@ -1473,7 +1473,7 @@ fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u
return result;
}

fn getBuiltinCode(allocator: *mem.Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 {
fn getBuiltinCode(allocator: mem.Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 {
const result = try exec(allocator, env_map, []const []const u8{
zig_exe,
"builtin",
Expand Down
34 changes: 17 additions & 17 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -5840,8 +5840,8 @@ test "global assembly" {
which has these fields:
</p>
<ul>
<li>{#syntax#}allocFn: fn (self: *Allocator, byte_count: usize, alignment: u29) Error![]u8{#endsyntax#} - where {#syntax#}Error{#endsyntax#} can be any error set.</li>
<li>{#syntax#}freeFn: fn (self: *Allocator, old_mem: []u8) void{#endsyntax#}</li>
<li>{#syntax#}allocFn: fn (self: *const Allocator, byte_count: usize, alignment: u29) Error![]u8{#endsyntax#} - where {#syntax#}Error{#endsyntax#} can be any error set.</li>
<li>{#syntax#}freeFn: fn (self: *const Allocator, old_mem: []u8) void{#endsyntax#}</li>
</ul>
<p>
You may notice that this corresponds to the {#syntax#}std.mem.Allocator{#endsyntax#} interface.
Expand Down Expand Up @@ -5873,12 +5873,12 @@ const assert = std.debug.assert;
var x: i32 = 1;

test "create a coroutine and cancel it" {
const p = try async<std.debug.global_allocator> simpleAsyncFn();
const p = try async<&std.debug.global_allocator> simpleAsyncFn();
comptime assert(@typeOf(p) == promise->void);
cancel p;
assert(x == 2);
}
async<*std.mem.Allocator> fn simpleAsyncFn() void {
async<*const std.mem.Allocator> fn simpleAsyncFn() void {
x += 1;
}
{#code_end#}
Expand All @@ -5895,7 +5895,7 @@ const assert = std.debug.assert;

test "coroutine suspend, resume, cancel" {
seq('a');
const p = try async<std.debug.global_allocator> testAsyncSeq();
const p = try async<&std.debug.global_allocator> testAsyncSeq();
seq('c');
resume p;
seq('f');
Expand Down Expand Up @@ -5930,7 +5930,7 @@ const std = @import("std");
const assert = std.debug.assert;

test "coroutine suspend with block" {
const p = try async<std.debug.global_allocator> testSuspendBlock();
const p = try async<&std.debug.global_allocator> testSuspendBlock();
std.debug.assert(!result);
resume a_promise;
std.debug.assert(result);
Expand Down Expand Up @@ -5973,9 +5973,9 @@ const assert = std.debug.assert;

test "resume from suspend" {
var buf: [500]u8 = undefined;
var a = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
var a = std.heap.FixedBufferAllocator.init(buf[0..]).allocator();
var my_result: i32 = 1;
const p = try async<a> testResumeFromSuspend(&my_result);
const p = try async<&a> testResumeFromSuspend(&my_result);
cancel p;
std.debug.assert(my_result == 2);
}
Expand Down Expand Up @@ -6025,7 +6025,7 @@ var final_result: i32 = 0;

test "coroutine await" {
seq('a');
const p = async<std.debug.global_allocator> amain() catch unreachable;
const p = async<&std.debug.global_allocator> amain() catch unreachable;
seq('f');
resume a_promise;
seq('i');
Expand Down Expand Up @@ -8385,8 +8385,8 @@ pub fn main() void {
C has a default allocator - <code>malloc</code>, <code>realloc</code>, and <code>free</code>.
When linking against libc, Zig exposes this allocator with {#syntax#}std.heap.c_allocator{#endsyntax#}.
However, by convention, there is no default allocator in Zig. Instead, functions which need to
allocate accept an {#syntax#}*Allocator{#endsyntax#} parameter. Likewise, data structures such as
{#syntax#}std.ArrayList{#endsyntax#} accept an {#syntax#}*Allocator{#endsyntax#} parameter in
allocate accept an {#syntax#}Allocator{#endsyntax#} parameter. Likewise, data structures such as
{#syntax#}std.ArrayList{#endsyntax#} accept an {#syntax#}Allocator{#endsyntax#} parameter in
their initialization functions:
</p>
{#code_begin|test|allocator#}
Expand All @@ -8396,12 +8396,12 @@ const assert = std.debug.assert;

test "using an allocator" {
var buffer: [100]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buffer).allocator;
const allocator = std.heap.FixedBufferAllocator.init(&buffer).allocator();
const result = try concat(allocator, "foo", "bar");
assert(std.mem.eql(u8, "foobar", result));
}

fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
fn concat(allocator: Allocator, a: []const u8, b: []const u8) ![]u8 {
const result = try allocator.alloc(u8, a.len + b.len);
std.mem.copy(u8, result, a);
std.mem.copy(u8, result[a.len..], b);
Expand Down Expand Up @@ -8429,7 +8429,7 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
</p>
<ol>
<li>
Are you making a library? In this case, best to accept an {#syntax#}*Allocator{#endsyntax#}
Are you making a library? In this case, best to accept an {#syntax#}Allocator{#endsyntax#}
as a parameter and allow your library's users to decide what allocator to use.
</li>
<li>Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely
Expand All @@ -8452,10 +8452,10 @@ pub fn main() !void {
var direct_allocator = std.heap.DirectAllocator.init();
defer direct_allocator.deinit();

var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator);
var arena = std.heap.ArenaAllocator.init(direct_allocator.allocator());
defer arena.deinit();

const allocator = &arena.allocator;
const allocator = arena.allocator();

const ptr = try allocator.create(i32);
std.debug.warn("ptr={*}\n", ptr);
Expand Down Expand Up @@ -8613,7 +8613,7 @@ test "string literal to constant slice" {
<p>
For example, the function's documentation may say "caller owns the returned memory", in which case
the code that calls the function must have a plan for when to free that memory. Probably in this situation,
the function will accept an {#syntax#}*Allocator{#endsyntax#} parameter.
the function will accept an {#syntax#}Allocator{#endsyntax#} parameter.
</p>
<p>
Sometimes the lifetime of a pointer may be more complicated. For example, when using
Expand Down
4 changes: 2 additions & 2 deletions example/guess_number/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const os = std.os;

pub fn main() !void {
var stdout_file = try io.getStdOut();
const stdout = &stdout_file.outStream().stream;
const stdout = stdout_file.outStreamAdapter().outStream();

try stdout.print("Welcome to the Guess Number Game in Zig.\n");

Expand All @@ -18,7 +18,7 @@ pub fn main() !void {
const seed = std.mem.readIntNative(u64, &seed_bytes);
var prng = std.rand.DefaultPrng.init(seed);

const answer = prng.random.range(u8, 0, 100) + 1;
const answer = prng.random().range(u8, 0, 100) + 1;

while (true) {
try stdout.print("\nGuess a number between 1 and 100: ");
Expand Down
4 changes: 2 additions & 2 deletions libc/process_headers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ const PathTable = std.AutoHashMap([]const u8, *TargetToHash);

pub fn main() !void {
var direct_allocator = std.heap.DirectAllocator.init();
var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator);
const allocator = &arena.allocator;
var arena = std.heap.ArenaAllocator.init(direct_allocator.allocator());
const allocator = arena.allocator();
const args = try std.os.argsAlloc(allocator);
var search_paths = std.ArrayList([]const u8).init(allocator);
var opt_out_dir: ?[]const u8 = null;
Expand Down
4 changes: 2 additions & 2 deletions src-self-hosted/arg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn argInAllowedSet(maybe_set: ?[]const []const u8, arg: []const u8) bool {
}

// Modifies the current argument index during iteration
fn readFlagArguments(allocator: *Allocator, args: []const []const u8, required: usize, allowed_set: ?[]const []const u8, index: *usize) !FlagArg {
fn readFlagArguments(allocator: Allocator, args: []const []const u8, required: usize, allowed_set: ?[]const []const u8, index: *usize) !FlagArg {
switch (required) {
0 => return FlagArg{ .None = undefined }, // TODO: Required to force non-tag but value?
1 => {
Expand Down Expand Up @@ -80,7 +80,7 @@ pub const Args = struct {
flags: HashMapFlags,
positionals: ArrayList([]const u8),

pub fn parse(allocator: *Allocator, comptime spec: []const Flag, args: []const []const u8) !Args {
pub fn parse(allocator: Allocator, comptime spec: []const Flag, args: []const []const u8) !Args {
var parsed = Args{
.flags = HashMapFlags.init(allocator),
.positionals = ArrayList([]const u8).init(allocator),
Expand Down
8 changes: 4 additions & 4 deletions src-self-hosted/codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
// Don't use ZIG_VERSION_STRING here. LLVM misparses it when it includes
// the git revision.
const producer = try std.Buffer.allocPrint(
&code.arena.allocator,
code.arena.allocator(),
"zig {}.{}.{}",
u32(c.ZIG_VERSION_MAJOR),
u32(c.ZIG_VERSION_MINOR),
Expand Down Expand Up @@ -80,7 +80,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
.dibuilder = dibuilder,
.context = context,
.lock = event.Lock.init(comp.loop),
.arena = &code.arena.allocator,
.arena = code.arena.allocator(),
};

try renderToLlvmModule(&ofile, fn_val, code);
Expand Down Expand Up @@ -142,9 +142,9 @@ pub const ObjectFile = struct {
dibuilder: *llvm.DIBuilder,
context: *llvm.Context,
lock: event.Lock,
arena: *std.mem.Allocator,
arena: std.mem.Allocator,

fn gpa(self: *ObjectFile) *std.mem.Allocator {
fn gpa(self: *ObjectFile) std.mem.Allocator {
return self.comp.gpa();
}
};
Expand Down
Loading