Skip to content

Commit e49b44e

Browse files
mitchellhnikneym
authored andcommitted
remove usingnamespace from everything but src/main.zig
This removes all our usingnamespace usage from our watchers and Windows lib code. There remains only one major usingnamespace usage left.
1 parent 5a799c1 commit e49b44e

File tree

5 files changed

+156
-35
lines changed

5 files changed

+156
-35
lines changed

src/watcher/file.zig

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ fn FileStream(comptime xev: type) type {
3838
/// The underlying file
3939
fd: FdType,
4040

41-
pub usingnamespace stream.Stream(xev, Self, .{
41+
const S = stream.Stream(xev, Self, .{
4242
.close = true,
4343
.poll = true,
4444
.read = .read,
4545
.write = .write,
4646
.threadpool = true,
4747
});
48+
pub const close = S.close;
49+
pub const poll = S.poll;
50+
pub const read = S.read;
51+
pub const write = S.write;
52+
pub const writeInit = S.writeInit;
53+
pub const queueWrite = S.queueWrite;
4854

4955
/// Initialize a File from a std.fs.File.
5056
pub fn init(file: std.fs.File) !Self {
@@ -318,14 +324,19 @@ fn FileDynamic(comptime xev: type) type {
318324

319325
pub const Union = xev.Union(&.{"File"});
320326

321-
pub usingnamespace stream.Stream(xev, Self, .{
327+
const S = stream.Stream(xev, Self, .{
322328
.close = true,
323329
.poll = true,
324330
.read = .read,
325331
.write = .write,
326332
.threadpool = true,
327333
.type = "File",
328334
});
335+
pub const close = S.close;
336+
pub const poll = S.poll;
337+
pub const read = S.read;
338+
pub const write = S.write;
339+
pub const queueWrite = S.queueWrite;
329340

330341
pub fn init(file: std.fs.File) !Self {
331342
return .{ .backend = switch (xev.backend) {
@@ -492,6 +503,18 @@ fn FileTests(
492503
comptime Impl: type,
493504
) type {
494505
return struct {
506+
test "File: Stream decls" {
507+
if (!@hasDecl(Impl, "S")) return;
508+
const Stream = Impl.S;
509+
inline for (@typeInfo(Stream).@"struct".decls) |decl| {
510+
const Decl = @TypeOf(@field(Stream, decl.name));
511+
if (Decl == void) continue;
512+
if (!@hasDecl(Impl, decl.name)) {
513+
@compileError("missing decl: " ++ decl.name);
514+
}
515+
}
516+
}
517+
495518
test "kqueue: zero-length read for readiness" {
496519
if (builtin.os.tag != .macos) return error.SkipZigTest;
497520

src/watcher/stream.zig

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const queue = @import("../queue.zig");
77
/// Options for creating a stream type. Each of the options makes the
88
/// functionality available for the stream.
99
pub const Options = struct {
10-
read: ReadMethod,
11-
write: WriteMethod,
12-
close: bool,
13-
poll: bool,
10+
read: ReadMethod = .none,
11+
write: WriteMethod = .none,
12+
close: bool = false,
13+
poll: bool = false,
1414

1515
/// True to schedule the read/write on the threadpool.
1616
threadpool: bool = false,
@@ -104,21 +104,35 @@ pub fn Shared(comptime xev: type) type {
104104
};
105105
}
106106

107-
/// Creates a stream type that is meant to be embedded within other
108-
/// types using "usingnamespace". A stream is something that supports read,
109-
/// write, close, etc. The exact operations supported are defined by the
110-
/// "options" struct.
107+
/// Creates a stream type that is meant to be embedded within other types.
108+
/// A stream is something that supports read, write, close, etc. The exact
109+
/// operations supported are defined by the "options" struct.
111110
///
112111
/// T requirements:
113112
/// - field named "fd" of type fd_t or socket_t
114113
/// - decl named "initFd" to initialize a new T from a fd
115114
///
116115
pub fn Stream(comptime xev: type, comptime T: type, comptime options: Options) type {
117116
return struct {
118-
pub usingnamespace if (options.close) Closeable(xev, T, options) else struct {};
119-
pub usingnamespace if (options.poll) Pollable(xev, T, options) else struct {};
120-
pub usingnamespace if (options.read != .none) Readable(xev, T, options) else struct {};
121-
pub usingnamespace if (options.write != .none) Writeable(xev, T, options) else struct {};
117+
const C_: ?type = if (options.close) Closeable(xev, T, options) else null;
118+
pub const close = if (C_) |C| C.close else {};
119+
120+
const P_: ?type = if (options.poll) Pollable(xev, T, options) else null;
121+
pub const poll = if (P_) |P| poll: {
122+
if (!@hasDecl(P, "poll")) break :poll {};
123+
break :poll P.poll;
124+
} else {};
125+
126+
const R_: ?type = if (options.read != .none) Readable(xev, T, options) else null;
127+
pub const read = if (R_) |R| R.read else {};
128+
129+
const W_: ?type = if (options.write != .none) Writeable(xev, T, options) else null;
130+
pub const writeInit = if (W_) |W| writeInit: {
131+
if (xev.dynamic) break :writeInit {};
132+
break :writeInit W.writeInit;
133+
} else {};
134+
pub const write = if (W_) |W| W.write else null;
135+
pub const queueWrite = if (W_) |W| W.queueWrite else {};
122136
};
123137
}
124138

@@ -1034,13 +1048,19 @@ pub fn GenericStream(comptime xev: type) type {
10341048

10351049
pub const Union = xev.Union(&.{"Stream"});
10361050

1037-
pub usingnamespace Stream(xev, Self, .{
1051+
const S = Stream(xev, Self, .{
10381052
.close = true,
10391053
.poll = true,
10401054
.read = .read,
10411055
.write = .write,
10421056
.type = "Stream",
10431057
});
1058+
pub const close = S.close;
1059+
pub const poll = S.poll;
1060+
pub const read = S.read;
1061+
pub const write = S.write;
1062+
pub const writeInit = S.writeInit;
1063+
pub const queueWrite = S.queueWrite;
10441064

10451065
pub fn initFd(fd: std.posix.pid_t) Self {
10461066
return .{ .backend = switch (xev.backend) {
@@ -1075,12 +1095,18 @@ pub fn GenericStream(comptime xev: type) type {
10751095
/// The underlying file
10761096
fd: std.posix.fd_t,
10771097

1078-
pub usingnamespace Stream(xev, Self, .{
1098+
const S = Stream(xev, Self, .{
10791099
.close = true,
10801100
.poll = true,
10811101
.read = .read,
10821102
.write = .write,
10831103
});
1104+
pub const close = S.close;
1105+
pub const poll = S.poll;
1106+
pub const read = S.read;
1107+
pub const write = S.write;
1108+
pub const writeInit = S.writeInit;
1109+
pub const queueWrite = S.queueWrite;
10841110

10851111
/// Initialize a generic stream from a file descriptor.
10861112
pub fn initFd(fd: std.posix.fd_t) Self {
@@ -1104,6 +1130,17 @@ pub fn GenericStream(comptime xev: type) type {
11041130

11051131
fn GenericStreamTests(comptime xev: type, comptime Impl: type) type {
11061132
return struct {
1133+
test "Stream decls" {
1134+
if (!@hasDecl(Impl, "S")) return;
1135+
inline for (@typeInfo(Impl.S).@"struct".decls) |decl| {
1136+
const Decl = @TypeOf(@field(Impl.S, decl.name));
1137+
if (Decl == void) continue;
1138+
if (!@hasDecl(Impl, decl.name)) {
1139+
@compileError("missing decl: " ++ decl.name);
1140+
}
1141+
}
1142+
}
1143+
11071144
test "pty: child to parent" {
11081145
const testing = std.testing;
11091146
switch (builtin.os.tag) {

src/watcher/tcp.zig

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ fn TCPStream(comptime xev: type) type {
2626

2727
fd: FdType,
2828

29-
pub usingnamespace stream.Stream(xev, Self, .{
29+
const S = stream.Stream(xev, Self, .{
3030
.close = true,
3131
.poll = true,
3232
.read = .recv,
3333
.write = .send,
3434
});
35+
pub const close = S.close;
36+
pub const poll = S.poll;
37+
pub const read = S.read;
38+
pub const write = S.write;
39+
pub const writeInit = S.writeInit;
40+
pub const queueWrite = S.queueWrite;
3541

3642
/// Initialize a new TCP with the family from the given address. Only
3743
/// the family is used, the actual address has no impact on the created
@@ -249,14 +255,19 @@ fn TCPDynamic(comptime xev: type) type {
249255

250256
pub const Union = xev.Union(&.{"TCP"});
251257

252-
pub usingnamespace stream.Stream(xev, Self, .{
258+
const S = stream.Stream(xev, Self, .{
253259
.close = true,
254260
.poll = true,
255261
.read = .read,
256262
.write = .write,
257263
.threadpool = true,
258264
.type = "TCP",
259265
});
266+
pub const close = S.close;
267+
pub const poll = S.poll;
268+
pub const read = S.read;
269+
pub const write = S.write;
270+
pub const queueWrite = S.queueWrite;
260271

261272
pub fn init(addr: std.net.Address) !Self {
262273
return .{ .backend = switch (xev.backend) {
@@ -492,6 +503,18 @@ fn TCPDynamic(comptime xev: type) type {
492503

493504
fn TCPTests(comptime xev: type, comptime Impl: type) type {
494505
return struct {
506+
test "TCP: Stream decls" {
507+
if (!@hasDecl(Impl, "S")) return;
508+
const Stream = Impl.S;
509+
inline for (@typeInfo(Stream).@"struct".decls) |decl| {
510+
const Decl = @TypeOf(@field(Stream, decl.name));
511+
if (Decl == void) continue;
512+
if (!@hasDecl(Impl, decl.name)) {
513+
@compileError("missing decl: " ++ decl.name);
514+
}
515+
}
516+
}
517+
495518
test "TCP: accept/connect/send/recv/close" {
496519
// We have no way to get a socket in WASI from a WASI context.
497520
if (builtin.os.tag == .wasi) return error.SkipZigTest;

src/watcher/udp.zig

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ fn UDPSendto(comptime xev: type) type {
4646
userdata: ?*anyopaque,
4747
};
4848

49-
pub usingnamespace stream.Stream(xev, Self, .{
49+
const S = stream.Stream(xev, Self, .{
5050
.close = true,
5151
.poll = true,
52-
.read = .none,
53-
.write = .none,
5452
});
53+
pub const close = S.close;
54+
pub const poll = S.poll;
5555

5656
/// Initialize a new UDP with the family from the given address. Only
5757
/// the family is used, the actual address has no impact on the created
@@ -226,12 +226,10 @@ fn UDPSendtoIOCP(comptime xev: type) type {
226226
userdata: ?*anyopaque,
227227
};
228228

229-
pub usingnamespace stream.Stream(xev, Self, .{
229+
const S = stream.Stream(xev, Self, .{
230230
.close = true,
231-
.poll = false,
232-
.read = .none,
233-
.write = .none,
234231
});
232+
pub const close = S.close;
235233

236234
/// Initialize a new UDP with the family from the given address. Only
237235
/// the family is used, the actual address has no impact on the created
@@ -423,12 +421,12 @@ fn UDPSendMsg(comptime xev: type) type {
423421
},
424422
};
425423

426-
pub usingnamespace stream.Stream(xev, Self, .{
424+
const S = stream.Stream(xev, Self, .{
427425
.close = true,
428426
.poll = true,
429-
.read = .none,
430-
.write = .none,
431427
});
428+
pub const close = S.close;
429+
pub const poll = S.poll;
432430

433431
/// Initialize a new UDP with the family from the given address. Only
434432
/// the family is used, the actual address has no impact on the created
@@ -694,13 +692,13 @@ fn UDPDynamic(comptime xev: type) type {
694692
pub const Union = xev.Union(&.{"UDP"});
695693
pub const State = xev.Union(&.{ "UDP", "State" });
696694

697-
pub usingnamespace stream.Stream(xev, Self, .{
695+
const S = stream.Stream(xev, Self, .{
698696
.close = true,
699697
.poll = true,
700-
.read = .none,
701-
.write = .none,
702698
.type = "UDP",
703699
});
700+
pub const close = S.close;
701+
pub const poll = S.poll;
704702

705703
pub fn init(addr: std.net.Address) !Self {
706704
return .{ .backend = switch (xev.backend) {
@@ -901,6 +899,18 @@ fn UDPDynamic(comptime xev: type) type {
901899

902900
fn UDPTests(comptime xev: type, comptime Impl: type) type {
903901
return struct {
902+
test "UDP: Stream decls" {
903+
if (!@hasDecl(Impl, "S")) return;
904+
const Stream = Impl.S;
905+
inline for (@typeInfo(Stream).@"struct".decls) |decl| {
906+
const Decl = @TypeOf(@field(Stream, decl.name));
907+
if (Decl == void) continue;
908+
if (!@hasDecl(Impl, decl.name)) {
909+
@compileError("missing decl: " ++ decl.name);
910+
}
911+
}
912+
}
913+
904914
test "UDP: read/write" {
905915
if (builtin.os.tag == .freebsd) return error.SkipZigTest;
906916
const testing = std.testing;

src/windows.zig

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,35 @@ const std = @import("std");
44
const windows = std.os.windows;
55
const posix = std.posix;
66

7-
pub usingnamespace std.os.windows;
7+
// Forwarded declarations of std.os.windows.
8+
pub const DWORD = windows.DWORD;
9+
pub const FALSE = windows.FALSE;
10+
pub const TRUE = windows.TRUE;
11+
pub const INFINITE = windows.INFINITE;
12+
pub const HANDLE = windows.HANDLE;
13+
pub const INVALID_HANDLE_VALUE = windows.INVALID_HANDLE_VALUE;
14+
pub const OVERLAPPED = windows.OVERLAPPED;
15+
pub const OVERLAPPED_ENTRY = windows.OVERLAPPED_ENTRY;
16+
pub const DUPLICATE_SAME_ACCESS = windows.DUPLICATE_SAME_ACCESS;
17+
pub const GENERIC_READ = windows.GENERIC_READ;
18+
pub const GENERIC_WRITE = windows.GENERIC_WRITE;
19+
pub const OPEN_ALWAYS = windows.OPEN_ALWAYS;
20+
pub const FILE_FLAG_OVERLAPPED = windows.FILE_FLAG_OVERLAPPED;
21+
pub const ReadFileError = windows.ReadFileError;
22+
pub const WriteFileError = windows.WriteFileError;
23+
pub const Win32Error = windows.Win32Error;
24+
pub const WSASocketW = windows.WSASocketW;
25+
pub const kernel32 = windows.kernel32;
26+
pub const ws2_32 = windows.ws2_32;
27+
pub const unexpectedWSAError = windows.unexpectedWSAError;
28+
pub const unexpectedError = windows.unexpectedError;
29+
pub const sliceToPrefixedFileW = windows.sliceToPrefixedFileW;
30+
pub const CloseHandle = windows.CloseHandle;
31+
pub const QueryPerformanceCounter = windows.QueryPerformanceCounter;
32+
pub const QueryPerformanceFrequency = windows.QueryPerformanceFrequency;
33+
pub const GetQueuedCompletionStatusEx = windows.GetQueuedCompletionStatusEx;
34+
pub const PostQueuedCompletionStatus = windows.PostQueuedCompletionStatus;
35+
pub const CreateIoCompletionPort = windows.CreateIoCompletionPort;
836

937
pub extern "kernel32" fn DeleteFileW(lpFileName: [*:0]const u16) callconv(windows.WINAPI) windows.BOOL;
1038

@@ -182,7 +210,7 @@ pub const exp = struct {
182210
lpSecurityAttributes: ?*windows.SECURITY_ATTRIBUTES,
183211
lpName: ?windows.LPCSTR,
184212
) !windows.HANDLE {
185-
const handle = kernel32.CreateJobObjectA(lpSecurityAttributes, lpName);
213+
const handle = exp.kernel32.CreateJobObjectA(lpSecurityAttributes, lpName);
186214
return switch (windows.kernel32.GetLastError()) {
187215
.SUCCESS => handle,
188216
.ALREADY_EXISTS => CreateJobObjectError.AlreadyExists,
@@ -191,7 +219,7 @@ pub const exp = struct {
191219
}
192220

193221
pub fn AssignProcessToJobObject(hJob: windows.HANDLE, hProcess: windows.HANDLE) posix.UnexpectedError!void {
194-
const result: windows.BOOL = kernel32.AssignProcessToJobObject(hJob, hProcess);
222+
const result: windows.BOOL = exp.kernel32.AssignProcessToJobObject(hJob, hProcess);
195223
if (result == windows.FALSE) {
196224
const err = windows.kernel32.GetLastError();
197225
return switch (err) {
@@ -206,7 +234,7 @@ pub const exp = struct {
206234
lpJobObjectInformation: windows.LPVOID,
207235
cbJobObjectInformationLength: windows.DWORD,
208236
) posix.UnexpectedError!void {
209-
const result: windows.BOOL = kernel32.SetInformationJobObject(
237+
const result: windows.BOOL = exp.kernel32.SetInformationJobObject(
210238
hJob,
211239
JobObjectInformationClass,
212240
lpJobObjectInformation,

0 commit comments

Comments
 (0)