Skip to content

Commit

Permalink
std lib API deprecations for the upcoming 0.6.0 release
Browse files Browse the repository at this point in the history
See #3811
  • Loading branch information
andrewrk committed Mar 30, 2020
1 parent b980568 commit 9e7ae06
Show file tree
Hide file tree
Showing 70 changed files with 597 additions and 564 deletions.
69 changes: 44 additions & 25 deletions doc/docgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
allocator,
&[_][]const u8{ tmp_dir_name, name_plus_ext },
);
try io.writeFile(tmp_source_file_name, trimmed_raw_source);
try fs.cwd().writeFile(tmp_source_file_name, trimmed_raw_source);

switch (code.id) {
Code.Id.Exe => |expected_outcome| code_block: {
Expand Down Expand Up @@ -1106,18 +1106,17 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
}
}
if (expected_outcome == .BuildFail) {
const result = try ChildProcess.exec(
allocator,
build_args.toSliceConst(),
null,
&env_map,
max_doc_file_size,
);
const result = try ChildProcess.exec(.{
.allocator = allocator,
.argv = build_args.span(),
.env_map = &env_map,
.max_output_bytes = max_doc_file_size,
});
switch (result.term) {
.Exited => |exit_code| {
if (exit_code == 0) {
warn("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
for (build_args.toSliceConst()) |arg|
for (build_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand All @@ -1126,7 +1125,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
},
else => {
warn("{}\nThe following command crashed:\n", .{result.stderr});
for (build_args.toSliceConst()) |arg|
for (build_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand All @@ -1138,7 +1137,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
try out.print("\n{}</code></pre>\n", .{colored_stderr});
break :code_block;
}
const exec_result = exec(allocator, &env_map, build_args.toSliceConst()) catch
const exec_result = exec(allocator, &env_map, build_args.span()) catch
return parseError(tokenizer, code.source_token, "example failed to compile", .{});

if (code.target_str) |triple| {
Expand Down Expand Up @@ -1167,7 +1166,12 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
var exited_with_signal = false;

const result = if (expected_outcome == ExpectedOutcome.Fail) blk: {
const result = try ChildProcess.exec(allocator, run_args, null, &env_map, max_doc_file_size);
const result = try ChildProcess.exec(.{
.allocator = allocator,
.argv = run_args,
.env_map = &env_map,
.max_output_bytes = max_doc_file_size,
});
switch (result.term) {
.Exited => |exit_code| {
if (exit_code == 0) {
Expand Down Expand Up @@ -1234,7 +1238,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
try test_args.appendSlice(&[_][]const u8{ "-target", triple });
try out.print(" -target {}", .{triple});
}
const result = exec(allocator, &env_map, test_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "test failed", .{});
const result = exec(allocator, &env_map, test_args.span()) catch return parseError(tokenizer, code.source_token, "test failed", .{});
const escaped_stderr = try escapeHtml(allocator, result.stderr);
const escaped_stdout = try escapeHtml(allocator, result.stdout);
try out.print("\n{}{}</code></pre>\n", .{ escaped_stderr, escaped_stdout });
Expand Down Expand Up @@ -1268,12 +1272,17 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
try out.print(" --release-small", .{});
},
}
const result = try ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size);
const result = try ChildProcess.exec(.{
.allocator = allocator,
.argv = test_args.span(),
.env_map = &env_map,
.max_output_bytes = max_doc_file_size,
});
switch (result.term) {
.Exited => |exit_code| {
if (exit_code == 0) {
warn("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
for (test_args.toSliceConst()) |arg|
for (test_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand All @@ -1282,7 +1291,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
},
else => {
warn("{}\nThe following command crashed:\n", .{result.stderr});
for (test_args.toSliceConst()) |arg|
for (test_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand Down Expand Up @@ -1326,12 +1335,17 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
},
}

const result = try ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size);
const result = try ChildProcess.exec(.{
.allocator = allocator,
.argv = test_args.span(),
.env_map = &env_map,
.max_output_bytes = max_doc_file_size,
});
switch (result.term) {
.Exited => |exit_code| {
if (exit_code == 0) {
warn("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
for (test_args.toSliceConst()) |arg|
for (test_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand All @@ -1340,7 +1354,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
},
else => {
warn("{}\nThe following command crashed:\n", .{result.stderr});
for (test_args.toSliceConst()) |arg|
for (test_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand Down Expand Up @@ -1418,12 +1432,17 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
}

if (maybe_error_match) |error_match| {
const result = try ChildProcess.exec(allocator, build_args.toSliceConst(), null, &env_map, max_doc_file_size);
const result = try ChildProcess.exec(.{
.allocator = allocator,
.argv = build_args.span(),
.env_map = &env_map,
.max_output_bytes = max_doc_file_size,
});
switch (result.term) {
.Exited => |exit_code| {
if (exit_code == 0) {
warn("{}\nThe following command incorrectly succeeded:\n", .{result.stderr});
for (build_args.toSliceConst()) |arg|
for (build_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand All @@ -1432,7 +1451,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
},
else => {
warn("{}\nThe following command crashed:\n", .{result.stderr});
for (build_args.toSliceConst()) |arg|
for (build_args.span()) |arg|
warn("{} ", .{arg})
else
warn("\n", .{});
Expand All @@ -1447,7 +1466,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
const colored_stderr = try termColor(allocator, escaped_stderr);
try out.print("\n{}", .{colored_stderr});
} else {
_ = exec(allocator, &env_map, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile", .{});
_ = exec(allocator, &env_map, build_args.span()) catch return parseError(tokenizer, code.source_token, "example failed to compile", .{});
}
if (!code.is_inline) {
try out.print("</code></pre>\n", .{});
Expand Down Expand Up @@ -1484,7 +1503,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
try test_args.appendSlice(&[_][]const u8{ "-target", triple });
try out.print(" -target {}", .{triple});
}
const result = exec(allocator, &env_map, test_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "test failed", .{});
const result = exec(allocator, &env_map, test_args.span()) catch return parseError(tokenizer, code.source_token, "test failed", .{});
const escaped_stderr = try escapeHtml(allocator, result.stderr);
const escaped_stdout = try escapeHtml(allocator, result.stdout);
try out.print("\n{}{}</code></pre>\n", .{ escaped_stderr, escaped_stdout });
Expand All @@ -1497,7 +1516,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) !ChildProcess.ExecResult {
const result = try ChildProcess.exec2(.{
const result = try ChildProcess.exec(.{
.allocator = allocator,
.argv = args,
.env_map = env_map,
Expand Down
4 changes: 2 additions & 2 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -4953,7 +4953,7 @@ const mem = std.mem;
test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
const window_name = [1][*]const u8{"window name"};
const x: [*]const ?[*]const u8 = &window_name;
assert(mem.eql(u8, std.mem.toSliceConst(u8, @ptrCast([*:0]const u8, x[0].?)), "window name"));
assert(mem.eql(u8, std.mem.spanZ(@ptrCast([*:0]const u8, x[0].?)), "window name"));
}
{#code_end#}
{#header_close#}
Expand Down Expand Up @@ -9310,7 +9310,7 @@ test "string literal to constant slice" {
</p>
<p>
Sometimes the lifetime of a pointer may be more complicated. For example, when using
{#syntax#}std.ArrayList(T).toSlice(){#endsyntax#}, the returned slice has a lifetime that remains
{#syntax#}std.ArrayList(T).span(){#endsyntax#}, the returned slice has a lifetime that remains
valid until the next time the list is resized, such as by appending new elements.
</p>
<p>
Expand Down
2 changes: 1 addition & 1 deletion lib/std/atomic/queue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn startPuts(ctx: *Context) u8 {
var r = std.rand.DefaultPrng.init(0xdeadbeef);
while (put_count != 0) : (put_count -= 1) {
std.time.sleep(1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const x = @bitCast(i32, r.random.int(u32));
const node = ctx.allocator.create(Queue(i32).Node) catch unreachable;
node.* = .{
.prev = undefined,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/atomic/stack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn startPuts(ctx: *Context) u8 {
var r = std.rand.DefaultPrng.init(0xdeadbeef);
while (put_count != 0) : (put_count -= 1) {
std.time.sleep(1); // let the os scheduler be our fuzz
const x = @bitCast(i32, r.random.scalar(u32));
const x = @bitCast(i32, r.random.int(u32));
const node = ctx.allocator.create(Stack(i32).Node) catch unreachable;
node.* = Stack(i32).Node{
.next = undefined,
Expand Down
33 changes: 13 additions & 20 deletions lib/std/buffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub const Buffer = struct {

/// Must deinitialize with deinit.
pub fn initFromBuffer(buffer: Buffer) !Buffer {
return Buffer.init(buffer.list.allocator, buffer.toSliceConst());
return Buffer.init(buffer.list.allocator, buffer.span());
}

/// Buffer takes ownership of the passed in slice. The slice must have been
Expand Down Expand Up @@ -81,15 +81,8 @@ pub const Buffer = struct {
return self.list.span()[0..self.len() :0];
}

/// Deprecated: use `span`
pub fn toSlice(self: Buffer) [:0]u8 {
return self.span();
}

/// Deprecated: use `span`
pub fn toSliceConst(self: Buffer) [:0]const u8 {
return self.span();
}
pub const toSlice = @compileError("deprecated; use span()");
pub const toSliceConst = @compileError("deprecated; use span()");

pub fn shrink(self: *Buffer, new_len: usize) void {
assert(new_len <= self.len());
Expand Down Expand Up @@ -120,17 +113,17 @@ pub const Buffer = struct {
pub fn append(self: *Buffer, m: []const u8) !void {
const old_len = self.len();
try self.resize(old_len + m.len);
mem.copy(u8, self.list.toSlice()[old_len..], m);
mem.copy(u8, self.list.span()[old_len..], m);
}

pub fn appendByte(self: *Buffer, byte: u8) !void {
const old_len = self.len();
try self.resize(old_len + 1);
self.list.toSlice()[old_len] = byte;
self.list.span()[old_len] = byte;
}

pub fn eql(self: Buffer, m: []const u8) bool {
return mem.eql(u8, self.toSliceConst(), m);
return mem.eql(u8, self.span(), m);
}

pub fn startsWith(self: Buffer, m: []const u8) bool {
Expand All @@ -147,7 +140,7 @@ pub const Buffer = struct {

pub fn replaceContents(self: *Buffer, m: []const u8) !void {
try self.resize(m.len);
mem.copy(u8, self.list.toSlice(), m);
mem.copy(u8, self.list.span(), m);
}

pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) {
Expand All @@ -171,25 +164,25 @@ test "simple Buffer" {
try buf.append(" ");
try buf.append("world");
testing.expect(buf.eql("hello world"));
testing.expect(mem.eql(u8, mem.toSliceConst(u8, buf.toSliceConst().ptr), buf.toSliceConst()));
testing.expect(mem.eql(u8, mem.spanZ(buf.span().ptr), buf.span()));

var buf2 = try Buffer.initFromBuffer(buf);
defer buf2.deinit();
testing.expect(buf.eql(buf2.toSliceConst()));
testing.expect(buf.eql(buf2.span()));

testing.expect(buf.startsWith("hell"));
testing.expect(buf.endsWith("orld"));

try buf2.resize(4);
testing.expect(buf.startsWith(buf2.toSlice()));
testing.expect(buf.startsWith(buf2.span()));
}

test "Buffer.initSize" {
var buf = try Buffer.initSize(testing.allocator, 3);
defer buf.deinit();
testing.expect(buf.len() == 3);
try buf.append("hello");
testing.expect(mem.eql(u8, buf.toSliceConst()[3..], "hello"));
testing.expect(mem.eql(u8, buf.span()[3..], "hello"));
}

test "Buffer.initCapacity" {
Expand All @@ -201,7 +194,7 @@ test "Buffer.initCapacity" {
try buf.append("hello");
testing.expect(buf.len() == 5);
testing.expect(buf.capacity() == old_cap);
testing.expect(mem.eql(u8, buf.toSliceConst(), "hello"));
testing.expect(mem.eql(u8, buf.span(), "hello"));
}

test "Buffer.print" {
Expand All @@ -221,5 +214,5 @@ test "Buffer.outStream" {
const y: i32 = 1234;
try buf_stream.print("x: {}\ny: {}\n", .{ x, y });

testing.expect(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n"));
testing.expect(mem.eql(u8, buffer.span(), "x: 42\ny: 1234\n"));
}
Loading

0 comments on commit 9e7ae06

Please sign in to comment.