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 #17408

Improved by helpful reviews from Josh Wolfe and Auguste Rame and Andrew
Kelley.

Co-authored-by: Joel Gustafson <joelg@mit.edu>
  • Loading branch information
marler8997 and joeltg committed Apr 28, 2024
1 parent c231d94 commit c9d8073
Show file tree
Hide file tree
Showing 7 changed files with 1,104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/std/io.zig
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,12 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
.Struct => {
inline for (std.meta.fields(S)) |f| {
switch (@typeInfo(f.type)) {
.Struct, .Array => byteSwapAllFields(f.type, &@field(ptr, f.name)),
.Struct => |struct_info| if (struct_info.backing_integer) |Int| {
@field(ptr, f.name) = @bitCast(@byteSwap(@as(Int, @bitCast(@field(ptr, f.name)))));
} else {
byteSwapAllFields(f.type, &@field(ptr, f.name));
},
.Array => byteSwapAllFields(f.type, &@field(ptr, f.name)),
.Enum => {
@field(ptr, f.name) = @enumFromInt(@byteSwap(@intFromEnum(@field(ptr, f.name))));
},
Expand Down
1 change: 1 addition & 0 deletions lib/std/std.zig
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit c9d8073

Please sign in to comment.