-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type-erased writer and GenericWriter
This is a companion to #17344 to apply the same change to the `std.io.Writer` interface.
- Loading branch information
1 parent
4d10607
commit f387f5a
Showing
4 changed files
with
154 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const std = @import("../std.zig"); | ||
const assert = std.debug.assert; | ||
const mem = std.mem; | ||
|
||
context: *const anyopaque, | ||
writeFn: *const fn (context: *const anyopaque, bytes: []const u8) anyerror!usize, | ||
|
||
const Self = @This(); | ||
pub const Error = anyerror; | ||
|
||
pub fn write(self: Self, bytes: []const u8) anyerror!usize { | ||
return self.writeFn(self.context, bytes); | ||
} | ||
|
||
pub fn writeAll(self: Self, bytes: []const u8) anyerror!void { | ||
var index: usize = 0; | ||
while (index != bytes.len) { | ||
index += try self.write(bytes[index..]); | ||
} | ||
} | ||
|
||
pub fn print(self: Self, comptime format: []const u8, args: anytype) anyerror!void { | ||
return std.fmt.format(self, format, args); | ||
} | ||
|
||
pub fn writeByte(self: Self, byte: u8) anyerror!void { | ||
const array = [1]u8{byte}; | ||
return self.writeAll(&array); | ||
} | ||
|
||
pub fn writeByteNTimes(self: Self, byte: u8, n: usize) anyerror!void { | ||
var bytes: [256]u8 = undefined; | ||
@memset(bytes[0..], byte); | ||
|
||
var remaining: usize = n; | ||
while (remaining > 0) { | ||
const to_write = @min(remaining, bytes.len); | ||
try self.writeAll(bytes[0..to_write]); | ||
remaining -= to_write; | ||
} | ||
} | ||
|
||
/// Write a native-endian integer. | ||
pub fn writeIntNative(self: Self, comptime T: type, value: T) anyerror!void { | ||
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
return self.writeAll(&bytes); | ||
} | ||
|
||
/// Write a foreign-endian integer. | ||
pub fn writeIntForeign(self: Self, comptime T: type, value: T) anyerror!void { | ||
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
return self.writeAll(&bytes); | ||
} | ||
|
||
pub fn writeIntLittle(self: Self, comptime T: type, value: T) anyerror!void { | ||
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
return self.writeAll(&bytes); | ||
} | ||
|
||
pub fn writeIntBig(self: Self, comptime T: type, value: T) anyerror!void { | ||
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); | ||
return self.writeAll(&bytes); | ||
} | ||
|
||
pub fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) anyerror!void { | ||
var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; | ||
mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian); | ||
return self.writeAll(&bytes); | ||
} | ||
|
||
pub fn writeStruct(self: Self, value: anytype) anyerror!void { | ||
// Only extern and packed structs have defined in-memory layout. | ||
comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto); | ||
return self.writeAll(mem.asBytes(&value)); | ||
} |
This file was deleted.
Oops, something went wrong.