1 module upromised.uv;
2 import deimos.libuv.uv;
3 import std.format : format;
4 import upromised.promise : DelegatePromise, DelegatePromiseIterator;
5 import upromised : fatal;
6 
7 class UvError : Exception {
8 	import std..string : fromStringz;
9 
10 	this(int code, string file = __FILE__, size_t line = __LINE__) nothrow {
11 		try {
12 			super("UV error (%s) %s".format(uv_strerror(code).fromStringz, code), file, line);
13 		} catch(Exception e) {
14 			fatal(e);
15 		}
16 	}
17 }
18 
19 void uvCheck(int r, string file = __FILE__, size_t line = __LINE__) {
20 	if (r < 0) throw new UvError(r, file, line);
21 }
22 
23 bool uvCheck(T)(int r, DelegatePromise!T t, string file = __FILE__, size_t line = __LINE__) nothrow {
24     if (r < 0) {
25         t.reject(new UvError(r, file, line));
26         return true;
27     }
28     return false;
29 }
30 
31 bool uvCheck(T)(int r, DelegatePromiseIterator!T t, string file = __FILE__, size_t line = __LINE__) nothrow {
32     if (r < 0) {
33         t.reject(new UvError(r, file, line));
34         return true;
35     }
36     return false;
37 }
38 
39 public uv_stream_t* stream(ref uv_tcp_t self) nothrow {
40 	return cast(uv_stream_t*)&self;
41 }
42 public uv_handle_t* handle(ref uv_tcp_t self) nothrow {
43 	return cast(uv_handle_t*)&self;
44 }
45 
46 public uv_stream_t* stream(ref uv_tty_t self) nothrow {
47 	return cast(uv_stream_t*)&self;
48 }
49 public uv_handle_t* handle(ref uv_tty_t self) nothrow {
50 	return cast(uv_handle_t*)&self;
51 }
52 
53 public uv_stream_t* stream(ref uv_pipe_t self) nothrow {
54 	return cast(uv_stream_t*)&self;
55 }
56 public uv_handle_t* handle(ref uv_pipe_t self) nothrow {
57 	return cast(uv_handle_t*)&self;
58 }
59 
60 public uv_handle_t* handle(ref uv_process_t self) nothrow {
61 	return cast(uv_handle_t*)&self;
62 }
63 public uv_handle_t* handle(ref uv_udp_t self) nothrow {
64 	return cast(uv_handle_t*)&self;
65 }