1 module vibecompat.core.concurrency;
2 
3 public import vibe.core.concurrency;
4 import vibe.core.core : Task;
5 import core.time : Duration;
6 
7 /// Forwards to `std.concurrency.send`.
8 void sendCompat(ARGS...)(Task task, ARGS args)
9 @trusted {
10 	assert (task != Task(), "Invalid task handle");
11 	static assert(args.length > 0, "Need to send at least one value.");
12 	foreach (A; ARGS)
13 		static assert(isWeaklyIsolated!A, "Only objects with no unshared or unisolated aliasing may be sent, not "~A.stringof~".");
14 	send(task.tid, args);
15 }
16 
17 /// Forwards to `std.concurrency.prioritySend`.
18 void prioritySendCompat(ARGS...)(Task task, ARGS args)
19 @trusted {
20 	assert (task != Task(), "Invalid task handle");
21 	static assert(args.length > 0, "Need to send at least one value.");
22 	foreach (A; ARGS)
23 		static assert(isWeaklyIsolated!A, "Only objects with no unshared or unisolated aliasing may be sent, not "~A.stringof~".");
24 	prioritySend(task.tid, args);
25 }
26 
27 /// Forwards to `std.concurrency.receive`.
28 void receiveCompat(OPS...)(OPS ops)
29 @trusted {
30 	receive(ops);
31 }
32 
33 /// Forwards to `std.concurrency.receiveOnly`.
34 auto receiveOnlyCompat(ARGS...)()
35 @trusted {
36 	foreach (A; ARGS)
37 		static assert(isWeaklyIsolated!A, "Only objects with no unshared or unisolated aliasing may be sent, not "~A.stringof~".");
38 	return receiveOnly!ARGS();
39 }
40 
41 /// Forwards to `std.concurrency.receiveTimeout`.
42 bool receiveTimeoutCompat(OPS...)(Duration timeout, OPS ops)
43 @trusted {
44 	return receiveTimeout(timeout, ops);
45 }
46 
47 /// Forwards to `std.concurrency.setMaxMailboxSize`.
48 void setMaxMailboxSizeCompat(Task task, size_t messages, OnCrowding on_crowding)
49 @trusted {
50 	setMaxMailboxSize(task.tid, messages, on_crowding);
51 }
52 
53 
54 @safe unittest {
55 	import vibe.core.core : exitEventLoop, runEventLoop, runTask, sleep;
56 	import core.time : seconds;
57 
58 	auto t = runTask({
59 		assert(receiveOnlyCompat!int == 42);
60 		receiveCompat((string s) {
61 			assert(s == "foo");
62 		});
63 		receiveTimeoutCompat(10.seconds, (int i) {
64 			assert(i == 43);
65 		});
66 	});
67 	t.setMaxMailboxSizeCompat(10, OnCrowding.block);
68 	t.sendCompat(42);
69 	sleep(1.seconds);
70 	t.sendCompat(43);
71 	t.prioritySendCompat("foo");
72 	t.join();
73 }