diff --git a/src/ThreadPool.zig b/src/ThreadPool.zig index ed796da..69fb964 100644 --- a/src/ThreadPool.zig +++ b/src/ThreadPool.zig @@ -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; diff --git a/src/backend/epoll.zig b/src/backend/epoll.zig index 6735bef..7516b4e 100644 --- a/src/backend/epoll.zig +++ b/src/backend/epoll.zig @@ -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 diff --git a/src/backend/io_uring.zig b/src/backend/io_uring.zig index cd649c2..7fb63fd 100644 --- a/src/backend/io_uring.zig +++ b/src/backend/io_uring.zig @@ -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 @@ -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, @@ -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()); @@ -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; diff --git a/src/backend/iocp.zig b/src/backend/iocp.zig index e986513..2636cc6 100644 --- a/src/backend/iocp.zig +++ b/src/backend/iocp.zig @@ -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); @@ -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); @@ -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 = {}, @@ -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)) |_| @@ -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 = {}, @@ -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)) |_| @@ -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), @@ -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; @@ -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), @@ -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; @@ -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. @@ -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); @@ -1954,7 +1954,7 @@ 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))); @@ -1962,7 +1962,7 @@ test "iocp: socket accept/connect/send/recv/close" { 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; @@ -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 = .{ @@ -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))); @@ -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))); diff --git a/src/backend/kqueue.zig b/src/backend/kqueue.zig index 2e534ff..7b0e265 100644 --- a/src/backend/kqueue.zig +++ b/src/backend/kqueue.zig @@ -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 diff --git a/src/backend/wasi_poll.zig b/src/backend/wasi_poll.zig index a53c35f..68bd8e2 100644 --- a/src/backend/wasi_poll.zig +++ b/src/backend/wasi_poll.zig @@ -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 @@ -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 | @@ -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 }; }; @@ -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 = .{ diff --git a/src/main.zig b/src/main.zig index 6b28e1c..0b04310 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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; } }; diff --git a/src/watcher/stream.zig b/src/watcher/stream.zig index 38f4218..2a20012 100644 --- a/src/watcher/stream.zig +++ b/src/watcher/stream.zig @@ -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, @@ -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( @@ -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( diff --git a/src/watcher/tcp.zig b/src/watcher/tcp.zig index 5969817..61e5404 100644 --- a/src/watcher/tcp.zig +++ b/src/watcher/tcp.zig @@ -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, @@ -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); @@ -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) { diff --git a/src/watcher/udp.zig b/src/watcher/udp.zig index 162a435..cfff622 100644 --- a/src/watcher/udp.zig +++ b/src/watcher/udp.zig @@ -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()); }