Skip to content

Cleanup net.zig - #226

Merged
lalinsky merged 3 commits into
mainfrom
cleanup-net
Jan 21, 2026
Merged

lalinsky merged 3 commits into
mainfrom
cleanup-net

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

No description provided.

@coderabbitai

coderabbitai Bot commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds Address.Family and Address.getFamily(); refactors address constructors/parsers to use shorthand initializers; consolidates socket boolean option handling into setBoolOption(); unifies receiveFrom/sendTo on a single ev-based path; re-exports POSIX constants and updates Windows setsockopt optname type.

Changes

Cohort / File(s) Change Summary
Address Type & Parsing
src/net.zig
Added pub const Address.Family = enum { ipv4, ipv6, unix } and pub fn getFamily(self: Address) Family; parsers/converters (parseIp, parseIpAndHost, fromStd, toStd, fromStorage) updated to use shorthand initializers and internal parsing delegates.
Socket Option & I/O API
src/net.zig
Centralized boolean option handling in setBoolOption() (platform branching moved inside); simplified setReuseAddress, setReusePort, setKeepAlive, setNoDelay; removed per-OS receive/send wrappers and unified to ev.NetRecvFrom / ev.NetSendTo.
POSIX Re-exports
src/os/posix.zig
Added public re-exports: pub const SO = system.SO, pub const SOL = system.SOL, pub const IPPROTO = system.IPPROTO, pub const TCP = system.TCP.
Windows API signature
src/os/windows.zig
Updated pub extern "ws2_32" fn setsockopt(...) signature: optname parameter changed from i32 to u32 (public API type change).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

Addresses choose a family, tidy and clear,
Options find one path, no more split gear,
Sends and receives walk a single line,
POSIX names appear, Windows types realign,
Net code hums along — small fixes, big cheer 🎉

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Cleanup net.zig' is vague and generic, using 'Cleanup' without specifying what was actually cleaned up or refactored. Be more specific about the changes. Consider: 'Refactor net.zig socket API and add Address.Family enum' to reflect the actual work.
Description check ❓ Inconclusive No description was provided by the author, making it impossible to assess relevance to the changeset. Add a pull request description explaining the changes, such as refactored socket methods, new Address.Family enum, or consolidated cross-platform branching.
✅ Passed checks (1 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch cleanup-net

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@src/net.zig`:
- Around line 793-805: The function setBoolOption has a type mismatch: optname
is declared as u32 but both os.windows.setsockopt and std.posix.setsockopt
expect an i32; change the parameter signature in setBoolOption from optname: u32
to optname: i32 (in the fn setBoolOption declaration) and update any call sites
that pass a u32 constant to cast or use i32 values to match the new signature;
keep the rest of the implementation (value/memory handling and platform
branches) unchanged.
- Line 796: The Windows setsockopt call is passing optname as u32 which causes a
mismatch; cast optname to i32 when calling os.windows.setsockopt (or change the
optname parameter to i32) — use `@intCast`(i32, optname) at the call site where
const rc = os.windows.setsockopt(self.handle, level, optname,
std.mem.asBytes(&value).ptr, `@sizeOf`(c_int)); so the third argument is
explicitly an i32 and matches the Windows API expectation.

Comment thread src/net.zig
Comment on lines 793 to 805
fn setBoolOption(self: Socket, level: i32, optname: u32, enabled: bool) !void {
const value: c_int = if (enabled) 1 else 0;
const bytes = std.mem.asBytes(&value);
try std.posix.setsockopt(self.handle, level, optname, bytes);
}

/// Helper function to set a boolean socket option (Windows)
fn setBoolOptionWindows(self: Socket, level: i32, optname: i32, enabled: bool) !void {
const value: c_int = if (enabled) 1 else 0;
const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, @sizeOf(c_int));
if (rc == os.windows.SOCKET_ERROR) {
return error.Unexpected;
if (builtin.os.tag == .windows) {
const value: c_int = if (enabled) 1 else 0;
const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, @sizeOf(c_int));
if (rc == os.windows.SOCKET_ERROR) {
return error.Unexpected;
}
} else {
const value: c_int = if (enabled) 1 else 0;
const bytes = std.mem.asBytes(&value);
try std.posix.setsockopt(self.handle, level, optname, bytes);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Type mismatch causes build failure.

Pipeline error is clear: optname is declared u32 but os.windows.setsockopt expects i32. The POSIX std.posix.setsockopt also expects i32 for the option name parameter.

Change the parameter type to i32:

🐛 Proposed fix
-    fn setBoolOption(self: Socket, level: i32, optname: u32, enabled: bool) !void {
+    fn setBoolOption(self: Socket, level: i32, optname: i32, enabled: bool) !void {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn setBoolOption(self: Socket, level: i32, optname: u32, enabled: bool) !void {
const value: c_int = if (enabled) 1 else 0;
const bytes = std.mem.asBytes(&value);
try std.posix.setsockopt(self.handle, level, optname, bytes);
}
/// Helper function to set a boolean socket option (Windows)
fn setBoolOptionWindows(self: Socket, level: i32, optname: i32, enabled: bool) !void {
const value: c_int = if (enabled) 1 else 0;
const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, @sizeOf(c_int));
if (rc == os.windows.SOCKET_ERROR) {
return error.Unexpected;
if (builtin.os.tag == .windows) {
const value: c_int = if (enabled) 1 else 0;
const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, @sizeOf(c_int));
if (rc == os.windows.SOCKET_ERROR) {
return error.Unexpected;
}
} else {
const value: c_int = if (enabled) 1 else 0;
const bytes = std.mem.asBytes(&value);
try std.posix.setsockopt(self.handle, level, optname, bytes);
}
}
fn setBoolOption(self: Socket, level: i32, optname: i32, enabled: bool) !void {
if (builtin.os.tag == .windows) {
const value: c_int = if (enabled) 1 else 0;
const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, `@sizeOf`(c_int));
if (rc == os.windows.SOCKET_ERROR) {
return error.Unexpected;
}
} else {
const value: c_int = if (enabled) 1 else 0;
const bytes = std.mem.asBytes(&value);
try std.posix.setsockopt(self.handle, level, optname, bytes);
}
}
🧰 Tools
🪛 GitHub Actions: Test coro

[error] 796-796: Command failed: zig build test -Dtest-filter=coro. -Doptimize=ReleaseSafe --summary all | Type mismatch: expected type 'i32', found 'u32' in setsockopt call (src/net.zig:796). The function optname is declared as i32 in os/windows.zig:696. Consider casting or changing the type to resolve the mismatch.

🤖 Prompt for AI Agents
In `@src/net.zig` around lines 793 - 805, The function setBoolOption has a type
mismatch: optname is declared as u32 but both os.windows.setsockopt and
std.posix.setsockopt expect an i32; change the parameter signature in
setBoolOption from optname: u32 to optname: i32 (in the fn setBoolOption
declaration) and update any call sites that pass a u32 constant to cast or use
i32 values to match the new signature; keep the rest of the implementation
(value/memory handling and platform branches) unchanged.

Comment thread src/net.zig
return error.Unexpected;
if (builtin.os.tag == .windows) {
const value: c_int = if (enabled) 1 else 0;
const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, @sizeOf(c_int));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Cast optname to i32 for Windows setsockopt.

Looking at both call sites: keeping the parameter as u32 and casting specifically for Windows is probably cleaner since the POSIX constants are typically u32.

Alternative fix if you'd rather not change the signature:

🔧 Alternative fix - cast at call site
-            const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, `@sizeOf`(c_int));
+            const rc = os.windows.setsockopt(self.handle, level, `@intCast`(optname), std.mem.asBytes(&value).ptr, `@sizeOf`(c_int));

Either approach works. The @intCast is safe here since socket option constants are small positive values well within i32 range.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const rc = os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr, @sizeOf(c_int));
const rc = os.windows.setsockopt(self.handle, level, `@intCast`(optname), std.mem.asBytes(&value).ptr, `@sizeOf`(c_int));
🧰 Tools
🪛 GitHub Actions: Test coro

[error] 796-796: Command failed: zig build test -Dtest-filter=coro. -Doptimize=ReleaseSafe --summary all | Type mismatch: expected type 'i32', found 'u32' in setsockopt call (src/net.zig:796). The function optname is declared as i32 in os/windows.zig:696. Consider casting or changing the type to resolve the mismatch.

🤖 Prompt for AI Agents
In `@src/net.zig` at line 796, The Windows setsockopt call is passing optname as
u32 which causes a mismatch; cast optname to i32 when calling
os.windows.setsockopt (or change the optname parameter to i32) — use
`@intCast`(i32, optname) at the call site where const rc =
os.windows.setsockopt(self.handle, level, optname, std.mem.asBytes(&value).ptr,
`@sizeOf`(c_int)); so the third argument is explicitly an i32 and matches the
Windows API expectation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant