aboutsummaryrefslogtreecommitdiff
path: root/lib/std/io
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-06-24 16:58:19 -0700
committerGitHub <noreply@github.com>2023-06-24 16:58:19 -0700
commit146b79af153bbd5dafda0ba12a040385c7fc58f8 (patch)
tree67e3db8b444d65c667e314770fc983a7fc8ba293 /lib/std/io
parent13853bef0df3c90633021850cc6d6abaeea03282 (diff)
parent21ac0beb436f49fe49c6982a872f2dc48e4bea5e (diff)
downloadzig-146b79af153bbd5dafda0ba12a040385c7fc58f8.tar.gz
zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.zip
Merge pull request #16163 from mlugg/feat/builtins-infer-dest-ty
Infer destination type of cast builtins using result type
Diffstat (limited to 'lib/std/io')
-rw-r--r--lib/std/io/bit_reader.zig22
-rw-r--r--lib/std/io/bit_writer.zig28
-rw-r--r--lib/std/io/c_writer.zig2
-rw-r--r--lib/std/io/reader.zig2
4 files changed, 27 insertions, 27 deletions
diff --git a/lib/std/io/bit_reader.zig b/lib/std/io/bit_reader.zig
index 4bdb0b9194..7ea2ff5009 100644
--- a/lib/std/io/bit_reader.zig
+++ b/lib/std/io/bit_reader.zig
@@ -60,7 +60,7 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type)
var out_buffer = @as(Buf, 0);
if (self.bit_count > 0) {
- const n = if (self.bit_count >= bits) @intCast(u3, bits) else self.bit_count;
+ const n = if (self.bit_count >= bits) @as(u3, @intCast(bits)) else self.bit_count;
const shift = u7_bit_count - n;
switch (endian) {
.Big => {
@@ -88,45 +88,45 @@ pub fn BitReader(comptime endian: std.builtin.Endian, comptime ReaderType: type)
while (out_bits.* < bits) {
const n = bits - out_bits.*;
const next_byte = self.forward_reader.readByte() catch |err| switch (err) {
- error.EndOfStream => return @intCast(U, out_buffer),
+ error.EndOfStream => return @as(U, @intCast(out_buffer)),
else => |e| return e,
};
switch (endian) {
.Big => {
if (n >= u8_bit_count) {
- out_buffer <<= @intCast(u3, u8_bit_count - 1);
+ out_buffer <<= @as(u3, @intCast(u8_bit_count - 1));
out_buffer <<= 1;
out_buffer |= @as(Buf, next_byte);
out_bits.* += u8_bit_count;
continue;
}
- const shift = @intCast(u3, u8_bit_count - n);
- out_buffer <<= @intCast(BufShift, n);
+ const shift = @as(u3, @intCast(u8_bit_count - n));
+ out_buffer <<= @as(BufShift, @intCast(n));
out_buffer |= @as(Buf, next_byte >> shift);
out_bits.* += n;
- self.bit_buffer = @truncate(u7, next_byte << @intCast(u3, n - 1));
+ self.bit_buffer = @as(u7, @truncate(next_byte << @as(u3, @intCast(n - 1))));
self.bit_count = shift;
},
.Little => {
if (n >= u8_bit_count) {
- out_buffer |= @as(Buf, next_byte) << @intCast(BufShift, out_bits.*);
+ out_buffer |= @as(Buf, next_byte) << @as(BufShift, @intCast(out_bits.*));
out_bits.* += u8_bit_count;
continue;
}
- const shift = @intCast(u3, u8_bit_count - n);
+ const shift = @as(u3, @intCast(u8_bit_count - n));
const value = (next_byte << shift) >> shift;
- out_buffer |= @as(Buf, value) << @intCast(BufShift, out_bits.*);
+ out_buffer |= @as(Buf, value) << @as(BufShift, @intCast(out_bits.*));
out_bits.* += n;
- self.bit_buffer = @truncate(u7, next_byte >> @intCast(u3, n));
+ self.bit_buffer = @as(u7, @truncate(next_byte >> @as(u3, @intCast(n))));
self.bit_count = shift;
},
}
}
- return @intCast(U, out_buffer);
+ return @as(U, @intCast(out_buffer));
}
pub fn alignToByte(self: *Self) void {
diff --git a/lib/std/io/bit_writer.zig b/lib/std/io/bit_writer.zig
index 0be2e7ab08..ef8f007264 100644
--- a/lib/std/io/bit_writer.zig
+++ b/lib/std/io/bit_writer.zig
@@ -47,27 +47,27 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type)
const Buf = std.meta.Int(.unsigned, buf_bit_count);
const BufShift = math.Log2Int(Buf);
- const buf_value = @intCast(Buf, value);
+ const buf_value = @as(Buf, @intCast(value));
- const high_byte_shift = @intCast(BufShift, buf_bit_count - u8_bit_count);
+ const high_byte_shift = @as(BufShift, @intCast(buf_bit_count - u8_bit_count));
var in_buffer = switch (endian) {
- .Big => buf_value << @intCast(BufShift, buf_bit_count - bits),
+ .Big => buf_value << @as(BufShift, @intCast(buf_bit_count - bits)),
.Little => buf_value,
};
var in_bits = bits;
if (self.bit_count > 0) {
const bits_remaining = u8_bit_count - self.bit_count;
- const n = @intCast(u3, if (bits_remaining > bits) bits else bits_remaining);
+ const n = @as(u3, @intCast(if (bits_remaining > bits) bits else bits_remaining));
switch (endian) {
.Big => {
- const shift = @intCast(BufShift, high_byte_shift + self.bit_count);
- const v = @intCast(u8, in_buffer >> shift);
+ const shift = @as(BufShift, @intCast(high_byte_shift + self.bit_count));
+ const v = @as(u8, @intCast(in_buffer >> shift));
self.bit_buffer |= v;
in_buffer <<= n;
},
.Little => {
- const v = @truncate(u8, in_buffer) << @intCast(u3, self.bit_count);
+ const v = @as(u8, @truncate(in_buffer)) << @as(u3, @intCast(self.bit_count));
self.bit_buffer |= v;
in_buffer >>= n;
},
@@ -87,15 +87,15 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type)
while (in_bits >= u8_bit_count) {
switch (endian) {
.Big => {
- const v = @intCast(u8, in_buffer >> high_byte_shift);
+ const v = @as(u8, @intCast(in_buffer >> high_byte_shift));
try self.forward_writer.writeByte(v);
- in_buffer <<= @intCast(u3, u8_bit_count - 1);
+ in_buffer <<= @as(u3, @intCast(u8_bit_count - 1));
in_buffer <<= 1;
},
.Little => {
- const v = @truncate(u8, in_buffer);
+ const v = @as(u8, @truncate(in_buffer));
try self.forward_writer.writeByte(v);
- in_buffer >>= @intCast(u3, u8_bit_count - 1);
+ in_buffer >>= @as(u3, @intCast(u8_bit_count - 1));
in_buffer >>= 1;
},
}
@@ -103,10 +103,10 @@ pub fn BitWriter(comptime endian: std.builtin.Endian, comptime WriterType: type)
}
if (in_bits > 0) {
- self.bit_count = @intCast(u4, in_bits);
+ self.bit_count = @as(u4, @intCast(in_bits));
self.bit_buffer = switch (endian) {
- .Big => @truncate(u8, in_buffer >> high_byte_shift),
- .Little => @truncate(u8, in_buffer),
+ .Big => @as(u8, @truncate(in_buffer >> high_byte_shift)),
+ .Little => @as(u8, @truncate(in_buffer)),
};
}
}
diff --git a/lib/std/io/c_writer.zig b/lib/std/io/c_writer.zig
index 62c73d3714..ee87a28dc6 100644
--- a/lib/std/io/c_writer.zig
+++ b/lib/std/io/c_writer.zig
@@ -13,7 +13,7 @@ pub fn cWriter(c_file: *std.c.FILE) CWriter {
fn cWriterWrite(c_file: *std.c.FILE, bytes: []const u8) std.fs.File.WriteError!usize {
const amt_written = std.c.fwrite(bytes.ptr, 1, bytes.len, c_file);
if (amt_written >= 0) return amt_written;
- switch (@enumFromInt(os.E, std.c._errno().*)) {
+ switch (@as(os.E, @enumFromInt(std.c._errno().*))) {
.SUCCESS => unreachable,
.INVAL => unreachable,
.FAULT => unreachable,
diff --git a/lib/std/io/reader.zig b/lib/std/io/reader.zig
index abdca56d3c..4dde51838b 100644
--- a/lib/std/io/reader.zig
+++ b/lib/std/io/reader.zig
@@ -246,7 +246,7 @@ pub fn Reader(
/// Same as `readByte` except the returned byte is signed.
pub fn readByteSigned(self: Self) (Error || error{EndOfStream})!i8 {
- return @bitCast(i8, try self.readByte());
+ return @as(i8, @bitCast(try self.readByte()));
}
/// Reads exactly `num_bytes` bytes and returns as an array.