Skip to content

Commit

Permalink
Fix never mutated vars (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh authored Nov 20, 2023
2 parents 04f21b9 + d15ceff commit 00ed7d7
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/ThreadPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,11 @@ const Node = struct {

fn pop(self: *Buffer) ?*Node {
var head = self.head.load(.Monotonic);
var tail = self.tail.loadUnchecked(); // we're the only thread that can change this
const tail = self.tail.loadUnchecked(); // we're the only thread that can change this

while (true) {
// Quick sanity check and return null when not empty
var size = tail -% head;
const size = tail -% head;
assert(size <= capacity);
if (size == 0) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/backend/epoll.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ test "epoll: loop time" {
defer loop.deinit();

// should never init zero
var now = loop.now();
const now = loop.now();
try testing.expect(now > 0);

// should update on a loop tick
Expand Down
8 changes: 4 additions & 4 deletions src/backend/io_uring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ test "io_uring: loop time" {
defer loop.deinit();

// should never init zero
var now = loop.now();
const now = loop.now();
try testing.expect(now > 0);

// should update on a loop tick
Expand Down Expand Up @@ -1666,7 +1666,7 @@ test "io_uring: sendmsg/recvmsg" {
var iovecs_recv = [_]os.iovec{
os.iovec{ .iov_base = &buffer_recv, .iov_len = buffer_recv.len },
};
var addr = [_]u8{0} ** 4;
const addr = [_]u8{0} ** 4;
var address_recv = net.Address.initIp4(addr, 0);
var msg_recv: os.msghdr = os.msghdr{
.name = &address_recv.any,
Expand Down Expand Up @@ -1716,7 +1716,7 @@ test "io_uring: socket read cancellation" {

// Create a UDP server socket
const address = try net.Address.parseIp4("127.0.0.1", 3131);
var socket = try os.socket(address.any.family, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0);
const socket = try os.socket(address.any.family, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0);
errdefer os.closeSocket(socket);
try os.setsockopt(socket, os.SOL.SOCKET, os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try os.bind(socket, &address.any, address.getOsSockLen());
Expand All @@ -1735,7 +1735,7 @@ test "io_uring: socket read cancellation" {
.userdata = &read_result,
.callback = (struct {
fn callback(ud: ?*anyopaque, l: *xev.Loop, c: *xev.Completion, r: xev.Result) xev.CallbackAction {
var ptr = @as(*xev.Result, @ptrCast(@alignCast(ud)));
const ptr = @as(*xev.Result, @ptrCast(@alignCast(ud)));
ptr.* = r;
_ = c;
_ = l;
Expand Down
34 changes: 17 additions & 17 deletions src/backend/iocp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub const Loop = struct {
// Get the duration of the QueryPerformanceCounter.
// We should check if the division is lossless, but it returns 10_000_000 on my machine so
// we'll handle that later.
var qpc_duration = 1_000_000_000 / windows.QueryPerformanceFrequency();
const qpc_duration = 1_000_000_000 / windows.QueryPerformanceFrequency();

// This creates a new Completion Port
const handle = try windows.CreateIoCompletionPort(windows.INVALID_HANDLE_VALUE, null, 0, 1);
Expand Down Expand Up @@ -483,7 +483,7 @@ pub const Loop = struct {
std.debug.assert(windows.ws2_32.getsockname(asSocket(v.socket), @as(*std.os.sockaddr, @ptrCast(&addr)), &addr_len) == 0);

var socket_type: i32 = 0;
var socket_type_bytes = std.mem.asBytes(&socket_type);
const socket_type_bytes = std.mem.asBytes(&socket_type);
var opt_len: i32 = @as(i32, @intCast(socket_type_bytes.len));
std.debug.assert(windows.ws2_32.getsockopt(asSocket(v.socket), std.os.SOL.SOCKET, std.os.SO.TYPE, socket_type_bytes, &opt_len) == 0);

Expand Down Expand Up @@ -534,7 +534,7 @@ pub const Loop = struct {

.read => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
const buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
break :action if (windows.exp.ReadFile(v.fd, buffer, &completion.overlapped)) |_|
.{
.submitted = {},
Expand All @@ -547,7 +547,7 @@ pub const Loop = struct {

.pread => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
const buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
completion.overlapped.DUMMYUNIONNAME.DUMMYSTRUCTNAME.Offset = @intCast(v.offset & 0xFFFF_FFFF_FFFF_FFFF);
completion.overlapped.DUMMYUNIONNAME.DUMMYSTRUCTNAME.OffsetHigh = @intCast(v.offset >> 32);
break :action if (windows.exp.ReadFile(v.fd, buffer, &completion.overlapped)) |_|
Expand All @@ -564,7 +564,7 @@ pub const Loop = struct {

.write => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
const buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
break :action if (windows.exp.WriteFile(v.fd, buffer, &completion.overlapped)) |_|
.{
.submitted = {},
Expand All @@ -577,7 +577,7 @@ pub const Loop = struct {

.pwrite => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
const buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
completion.overlapped.DUMMYUNIONNAME.DUMMYSTRUCTNAME.Offset = @intCast(v.offset & 0xFFFF_FFFF_FFFF_FFFF);
completion.overlapped.DUMMYUNIONNAME.DUMMYSTRUCTNAME.OffsetHigh = @intCast(v.offset >> 32);
break :action if (windows.exp.WriteFile(v.fd, buffer, &completion.overlapped)) |_|
Expand All @@ -592,7 +592,7 @@ pub const Loop = struct {

.send => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
const buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
v.wsa_buffer = .{ .buf = @constCast(buffer.ptr), .len = @as(u32, @intCast(buffer.len)) };
const result = windows.ws2_32.WSASend(
asSocket(v.fd),
Expand All @@ -617,7 +617,7 @@ pub const Loop = struct {

.recv => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
const buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
v.wsa_buffer = .{ .buf = buffer.ptr, .len = @as(u32, @intCast(buffer.len)) };

var flags: u32 = 0;
Expand Down Expand Up @@ -645,7 +645,7 @@ pub const Loop = struct {

.sendto => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
const buffer: []const u8 = if (v.buffer == .slice) v.buffer.slice else v.buffer.array.array[0..v.buffer.array.len];
v.wsa_buffer = .{ .buf = @constCast(buffer.ptr), .len = @as(u32, @intCast(buffer.len)) };
const result = windows.ws2_32.WSASendTo(
asSocket(v.fd),
Expand All @@ -672,7 +672,7 @@ pub const Loop = struct {

.recvfrom => |*v| action: {
self.associate_fd(completion.handle().?) catch unreachable;
var buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
const buffer: []u8 = if (v.buffer == .slice) v.buffer.slice else &v.buffer.array;
v.wsa_buffer = .{ .buf = buffer.ptr, .len = @as(u32, @intCast(buffer.len)) };

var flags: u32 = 0;
Expand Down Expand Up @@ -1061,7 +1061,7 @@ pub const Completion = struct {
// NOTE(Corendos): according to Win32 documentation, EOF has to be detected using the socket type.
const socket_type = t: {
var socket_type: windows.DWORD = 0;
var socket_type_bytes = std.mem.asBytes(&socket_type);
const socket_type_bytes = std.mem.asBytes(&socket_type);
var opt_len: i32 = @as(i32, @intCast(socket_type_bytes.len));

// Here we assume the call will succeed because the socket should be valid.
Expand Down Expand Up @@ -1447,7 +1447,7 @@ test "iocp: loop time" {
defer loop.deinit();

// should never init zero
var now = loop.now();
const now = loop.now();
try testing.expect(now > 0);

while (now == loop.now()) try loop.run(.no_wait);
Expand Down Expand Up @@ -1954,15 +1954,15 @@ test "iocp: socket accept/connect/send/recv/close" {
// Create a TCP server socket
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
var ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
const ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(ln);

try std.os.setsockopt(ln, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
try std.os.bind(ln, &address.any, address.getOsSockLen());
try std.os.listen(ln, kernel_backlog);

// Create a TCP client socket
var client_conn = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
const client_conn = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(client_conn);

var server_conn_result: Result = undefined;
Expand Down Expand Up @@ -2024,7 +2024,7 @@ test "iocp: socket accept/connect/send/recv/close" {
try loop.run(.until_done);
//try testing.expect(server_conn > 0);
try testing.expect(connected);
var server_conn = try server_conn_result.accept;
const server_conn = try server_conn_result.accept;

// Send
var c_send: xev.Completion = .{
Expand Down Expand Up @@ -2220,7 +2220,7 @@ test "iocp: recv cancellation" {

// Create a TCP server socket
const address = try net.Address.parseIp4("127.0.0.1", 3131);
var socket = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.DGRAM, std.os.IPPROTO.UDP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
const socket = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.DGRAM, std.os.IPPROTO.UDP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(socket);

try std.os.setsockopt(socket, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
Expand Down Expand Up @@ -2293,7 +2293,7 @@ test "iocp: accept cancellation" {
// Create a TCP server socket
const address = try net.Address.parseIp4("127.0.0.1", 3131);
const kernel_backlog = 1;
var ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
const ln = try windows.WSASocketW(std.os.AF.INET, std.os.SOCK.STREAM, std.os.IPPROTO.TCP, null, 0, windows.ws2_32.WSA_FLAG_OVERLAPPED);
errdefer std.os.closeSocket(ln);

try std.os.setsockopt(ln, std.os.SOL.SOCKET, std.os.SO.REUSEADDR, &mem.toBytes(@as(c_int, 1)));
Expand Down
2 changes: 1 addition & 1 deletion src/backend/kqueue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ test "kqueue: loop time" {
defer loop.deinit();

// should never init zero
var now = loop.now();
const now = loop.now();
try testing.expect(now > 0);

// should update on a loop tick
Expand Down
10 changes: 5 additions & 5 deletions src/backend/wasi_poll.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ test "wasi: loop time" {
defer loop.deinit();

// should never init zero
var now = loop.now();
const now = loop.now();
try testing.expect(now > 0);

// should update on a loop tick
Expand Down Expand Up @@ -1512,8 +1512,8 @@ test "wasi: file" {
// We can't use dir.createFile yet: https://github.com/ziglang/zig/issues/14324
const f = f: {
const w = wasi;
var oflags = w.O.CREAT | w.O.TRUNC;
var base: w.rights_t = w.RIGHT.FD_WRITE |
const oflags = w.O.CREAT | w.O.TRUNC;
const base: w.rights_t = w.RIGHT.FD_WRITE |
w.RIGHT.FD_READ |
w.RIGHT.FD_DATASYNC |
w.RIGHT.FD_SEEK |
Expand All @@ -1526,7 +1526,7 @@ test "wasi: file" {
w.RIGHT.FD_FILESTAT_SET_SIZE |
w.RIGHT.FD_FILESTAT_GET |
w.RIGHT.POLL_FD_READWRITE;
var fdflags: w.fdflags_t = w.FDFLAG.SYNC | w.FDFLAG.RSYNC | w.FDFLAG.DSYNC;
const fdflags: w.fdflags_t = w.FDFLAG.SYNC | w.FDFLAG.RSYNC | w.FDFLAG.DSYNC;
const fd = try std.os.openatWasi(dir.fd, path, 0x0, oflags, 0x0, base, fdflags);
break :f std.fs.File{ .handle = fd };
};
Expand Down Expand Up @@ -1570,7 +1570,7 @@ test "wasi: file" {
try testing.expect(read_len.? == 0);

// Start a writer
var write_buf = "hello!";
const write_buf = "hello!";
var write_len: ?usize = null;
var c_write: xev.Completion = .{
.op = .{
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn Xev(comptime be: Backend, comptime T: type) type {
}

test "completion is zero-able" {
var c: Completion = .{};
const c: Completion = .{};
_ = c;
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/watcher/stream.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn Closeable(comptime xev: type, comptime T: type, comptime options: Options
c_inner: *xev.Completion,
r: xev.Result,
) xev.CallbackAction {
var fd = T.initFd(c_inner.op.close.fd);
const fd = T.initFd(c_inner.op.close.fd);
return @call(.always_inline, cb, .{
common.userdataValue(Userdata, ud),
l_inner,
Expand Down Expand Up @@ -568,7 +568,7 @@ pub fn GenericStream(comptime xev: type) type {
try testing.expect(read_len == null);

// Send
var send_buf = "hello, world!";
const send_buf = "hello, world!";
var c_write: xev.Completion = undefined;
child.write(&loop, &c_write, .{ .slice = send_buf }, void, null, (struct {
fn callback(
Expand Down Expand Up @@ -632,7 +632,7 @@ pub fn GenericStream(comptime xev: type) type {

// Send (note the newline at the end of the buf is important
// since we're in cooked mode)
var send_buf = "hello, world!\n";
const send_buf = "hello, world!\n";
var c_write: xev.Completion = undefined;
parent.write(&loop, &c_write, .{ .slice = send_buf }, void, null, (struct {
fn callback(
Expand Down
6 changes: 3 additions & 3 deletions src/watcher/tcp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ pub fn TCP(comptime xev: type) type {
var w_queue = Self.WriteQueue{};
var wr_send: xev.TCP.WriteRequest = undefined;
var sent_queued: usize = 0;
var queued_slice = send_buf[sent_unqueued..];
const queued_slice = send_buf[sent_unqueued..];
client.queueWrite(&loop, &w_queue, &wr_send, .{ .slice = queued_slice }, usize, &sent_queued, (struct {
fn callback(
sent_queued_inner: ?*usize,
Expand Down Expand Up @@ -551,7 +551,7 @@ pub fn TCP(comptime xev: type) type {
pub fn read(receiver: *@This()) void {
if (receiver.bytes_read == receiver.buf.len) return;

var read_buf = xev.ReadBuffer{
const read_buf = xev.ReadBuffer{
.slice = receiver.buf[receiver.bytes_read..],
};
receiver.conn.read(receiver.loop, &receiver.completion, read_buf, @This(), receiver, readCb);
Expand All @@ -566,7 +566,7 @@ pub fn TCP(comptime xev: type) type {
r: Self.ReadError!usize,
) xev.CallbackAction {
var receiver = receiver_opt.?;
var n_bytes = r catch unreachable;
const n_bytes = r catch unreachable;

receiver.bytes_read += n_bytes;
if (receiver.bytes_read < send_buf.len) {
Expand Down
2 changes: 1 addition & 1 deletion src/watcher/udp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn UDPSendtoIOCP(comptime xev: type) type {

/// Bind the address to the socket.
pub fn bind(self: Self, addr: std.net.Address) !void {
var socket = @as(windows.ws2_32.SOCKET, @ptrCast(self.fd));
const socket = @as(windows.ws2_32.SOCKET, @ptrCast(self.fd));
try os.setsockopt(socket, os.SOL.SOCKET, os.SO.REUSEADDR, &std.mem.toBytes(@as(c_int, 1)));
try os.bind(socket, &addr.any, addr.getOsSockLen());
}
Expand Down

0 comments on commit 00ed7d7

Please sign in to comment.