Skip to content

Commit

Permalink
add std.zip and support zip files in build.zig.zon
Browse files Browse the repository at this point in the history
fixes ziglang#17408

Co-authored-by: Joel Gustafson <joelg@mit.edu>
  • Loading branch information
marler8997 and joeltg committed Apr 22, 2024
1 parent c7ffdbc commit 1b9f725
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/std/io.zig
Expand Up @@ -344,6 +344,10 @@ pub fn GenericWriter(
return @errorCast(self.any().writeStruct(value));
}

pub inline fn writeStructEndian(self: Self, value: anytype, endian: std.builtin.Endian) Error!void {
return @errorCast(self.any().writeStructEndian(value, endian));
}

pub inline fn any(self: *const Self) AnyWriter {
return .{
.context = @ptrCast(&self.context),
Expand Down
12 changes: 12 additions & 0 deletions lib/std/io/Writer.zig
@@ -1,6 +1,7 @@
const std = @import("../std.zig");
const assert = std.debug.assert;
const mem = std.mem;
const native_endian = @import("builtin").target.cpu.arch.endian();

context: *const anyopaque,
writeFn: *const fn (context: *const anyopaque, bytes: []const u8) anyerror!usize,
Expand Down Expand Up @@ -59,6 +60,17 @@ pub fn writeStruct(self: Self, value: anytype) anyerror!void {
return self.writeAll(mem.asBytes(&value));
}

pub fn writeStructEndian(self: Self, value: anytype, endian: std.builtin.Endian) anyerror!void {
// TODO: make sure this value is not a reference type
if (native_endian == endian) {
return self.writeStruct(value);
} else {
var copy = value;
mem.byteSwapAllFields(@TypeOf(value), &copy);
return self.writeStruct(copy);
}
}

pub fn writeFile(self: Self, file: std.fs.File) anyerror!void {
// TODO: figure out how to adjust std lib abstractions so that this ends up
// doing sendfile or maybe even copy_file_range under the right conditions.
Expand Down
1 change: 1 addition & 0 deletions lib/std/std.zig
Expand Up @@ -104,6 +104,7 @@ pub const unicode = @import("unicode.zig");
pub const valgrind = @import("valgrind.zig");
pub const wasm = @import("wasm.zig");
pub const zig = @import("zig.zig");
pub const zip = @import("zip.zig");
pub const start = @import("start.zig");

const root = @import("root");
Expand Down

0 comments on commit 1b9f725

Please sign in to comment.