]> git.lizzy.rs Git - rust.git/blob - src/librustuv/uvll.rs
Statically link libuv to librustuv
[rust.git] / src / librustuv / uvll.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12  * Low-level bindings to the libuv library.
13  *
14  * This module contains a set of direct, 'bare-metal' wrappers around
15  * the libuv C-API.
16  *
17  * We're not bothering yet to redefine uv's structs as Rust structs
18  * because they are quite large and change often between versions.
19  * The maintenance burden is just too high. Instead we use the uv's
20  * `uv_handle_size` and `uv_req_size` to find the correct size of the
21  * structs and allocate them on the heap. This can be revisited later.
22  *
23  * There are also a collection of helper functions to ease interacting
24  * with the low-level API.
25  *
26  * As new functionality, existent in uv.h, is added to the rust stdlib,
27  * the mappings should be added in this module.
28  */
29
30 #[allow(non_camel_case_types)]; // C types
31
32 use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t};
33 use std::libc::ssize_t;
34 use std::libc::{malloc, free};
35 use std::libc;
36 use std::ptr;
37 use std::vec;
38
39 pub use self::errors::*;
40
41 pub static OK: c_int = 0;
42 pub static EOF: c_int = -4095;
43 pub static UNKNOWN: c_int = -4094;
44
45 // uv-errno.h redefines error codes for windows, but not for unix...
46
47 #[cfg(windows)]
48 pub mod errors {
49     use std::libc::c_int;
50
51     pub static EACCES: c_int = -4093;
52     pub static ECONNREFUSED: c_int = -4079;
53     pub static ECONNRESET: c_int = -4078;
54     pub static ENOTCONN: c_int = -4054;
55     pub static EPIPE: c_int = -4048;
56     pub static ECONNABORTED: c_int = -4080;
57 }
58 #[cfg(not(windows))]
59 pub mod errors {
60     use std::libc;
61     use std::libc::c_int;
62
63     pub static EACCES: c_int = -libc::EACCES;
64     pub static ECONNREFUSED: c_int = -libc::ECONNREFUSED;
65     pub static ECONNRESET: c_int = -libc::ECONNRESET;
66     pub static ENOTCONN: c_int = -libc::ENOTCONN;
67     pub static EPIPE: c_int = -libc::EPIPE;
68     pub static ECONNABORTED: c_int = -libc::ECONNABORTED;
69 }
70
71 pub static PROCESS_SETUID: c_int = 1 << 0;
72 pub static PROCESS_SETGID: c_int = 1 << 1;
73 pub static PROCESS_WINDOWS_VERBATIM_ARGUMENTS: c_int = 1 << 2;
74 pub static PROCESS_DETACHED: c_int = 1 << 3;
75 pub static PROCESS_WINDOWS_HIDE: c_int = 1 << 4;
76
77 pub static STDIO_IGNORE: c_int = 0x00;
78 pub static STDIO_CREATE_PIPE: c_int = 0x01;
79 pub static STDIO_INHERIT_FD: c_int = 0x02;
80 pub static STDIO_INHERIT_STREAM: c_int = 0x04;
81 pub static STDIO_READABLE_PIPE: c_int = 0x10;
82 pub static STDIO_WRITABLE_PIPE: c_int = 0x20;
83
84 // see libuv/include/uv-unix.h
85 #[cfg(unix)]
86 pub struct uv_buf_t {
87     base: *u8,
88     len: libc::size_t,
89 }
90
91 // see libuv/include/uv-win.h
92 #[cfg(windows)]
93 pub struct uv_buf_t {
94     len: u32,
95     base: *u8,
96 }
97
98 pub struct uv_process_options_t {
99     exit_cb: uv_exit_cb,
100     file: *libc::c_char,
101     args: **libc::c_char,
102     env: **libc::c_char,
103     cwd: *libc::c_char,
104     flags: libc::c_uint,
105     stdio_count: libc::c_int,
106     stdio: *uv_stdio_container_t,
107     uid: uv_uid_t,
108     gid: uv_gid_t,
109 }
110
111 // These fields are private because they must be interfaced with through the
112 // functions below.
113 pub struct uv_stdio_container_t {
114     priv flags: libc::c_int,
115     priv stream: *uv_stream_t,
116 }
117
118 pub type uv_handle_t = c_void;
119 pub type uv_loop_t = c_void;
120 pub type uv_idle_t = c_void;
121 pub type uv_tcp_t = c_void;
122 pub type uv_udp_t = c_void;
123 pub type uv_connect_t = c_void;
124 pub type uv_connection_t = c_void;
125 pub type uv_write_t = c_void;
126 pub type uv_async_t = c_void;
127 pub type uv_timer_t = c_void;
128 pub type uv_stream_t = c_void;
129 pub type uv_fs_t = c_void;
130 pub type uv_udp_send_t = c_void;
131 pub type uv_getaddrinfo_t = c_void;
132 pub type uv_process_t = c_void;
133 pub type uv_pipe_t = c_void;
134 pub type uv_tty_t = c_void;
135 pub type uv_signal_t = c_void;
136
137 pub struct uv_timespec_t {
138     tv_sec: libc::c_long,
139     tv_nsec: libc::c_long
140 }
141
142 pub struct uv_stat_t {
143     st_dev: libc::uint64_t,
144     st_mode: libc::uint64_t,
145     st_nlink: libc::uint64_t,
146     st_uid: libc::uint64_t,
147     st_gid: libc::uint64_t,
148     st_rdev: libc::uint64_t,
149     st_ino: libc::uint64_t,
150     st_size: libc::uint64_t,
151     st_blksize: libc::uint64_t,
152     st_blocks: libc::uint64_t,
153     st_flags: libc::uint64_t,
154     st_gen: libc::uint64_t,
155     st_atim: uv_timespec_t,
156     st_mtim: uv_timespec_t,
157     st_ctim: uv_timespec_t,
158     st_birthtim: uv_timespec_t
159 }
160
161 impl uv_stat_t {
162     pub fn new() -> uv_stat_t {
163         uv_stat_t {
164             st_dev: 0,
165             st_mode: 0,
166             st_nlink: 0,
167             st_uid: 0,
168             st_gid: 0,
169             st_rdev: 0,
170             st_ino: 0,
171             st_size: 0,
172             st_blksize: 0,
173             st_blocks: 0,
174             st_flags: 0,
175             st_gen: 0,
176             st_atim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
177             st_mtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
178             st_ctim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
179             st_birthtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 }
180         }
181     }
182     pub fn is_file(&self) -> bool {
183         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFREG as libc::uint64_t
184     }
185     pub fn is_dir(&self) -> bool {
186         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFDIR as libc::uint64_t
187     }
188 }
189
190 pub type uv_idle_cb = extern "C" fn(handle: *uv_idle_t,
191                                     status: c_int);
192 pub type uv_alloc_cb = extern "C" fn(stream: *uv_stream_t,
193                                      suggested_size: size_t) -> uv_buf_t;
194 pub type uv_read_cb = extern "C" fn(stream: *uv_stream_t,
195                                     nread: ssize_t,
196                                     buf: uv_buf_t);
197 pub type uv_udp_send_cb = extern "C" fn(req: *uv_udp_send_t,
198                                         status: c_int);
199 pub type uv_udp_recv_cb = extern "C" fn(handle: *uv_udp_t,
200                                         nread: ssize_t,
201                                         buf: uv_buf_t,
202                                         addr: *sockaddr,
203                                         flags: c_uint);
204 pub type uv_close_cb = extern "C" fn(handle: *uv_handle_t);
205 pub type uv_walk_cb = extern "C" fn(handle: *uv_handle_t,
206                                     arg: *c_void);
207 pub type uv_async_cb = extern "C" fn(handle: *uv_async_t,
208                                      status: c_int);
209 pub type uv_connect_cb = extern "C" fn(handle: *uv_connect_t,
210                                        status: c_int);
211 pub type uv_connection_cb = extern "C" fn(handle: *uv_connection_t,
212                                           status: c_int);
213 pub type uv_timer_cb = extern "C" fn(handle: *uv_timer_t,
214                                      status: c_int);
215 pub type uv_write_cb = extern "C" fn(handle: *uv_write_t,
216                                      status: c_int);
217 pub type uv_getaddrinfo_cb = extern "C" fn(req: *uv_getaddrinfo_t,
218                                            status: c_int,
219                                            res: *addrinfo);
220 pub type uv_exit_cb = extern "C" fn(handle: *uv_process_t,
221                                     exit_status: c_int,
222                                     term_signal: c_int);
223 pub type uv_signal_cb = extern "C" fn(handle: *uv_signal_t,
224                                       signum: c_int);
225
226 pub type sockaddr = c_void;
227 pub type sockaddr_in = c_void;
228 pub type sockaddr_in6 = c_void;
229 pub type sockaddr_storage = c_void;
230
231 #[cfg(unix)]
232 pub type socklen_t = c_int;
233
234 // XXX: This is a standard C type. Could probably be defined in libc
235 #[cfg(target_os = "android")]
236 #[cfg(target_os = "linux")]
237 pub struct addrinfo {
238     ai_flags: c_int,
239     ai_family: c_int,
240     ai_socktype: c_int,
241     ai_protocol: c_int,
242     ai_addrlen: socklen_t,
243     ai_addr: *sockaddr,
244     ai_canonname: *char,
245     ai_next: *addrinfo
246 }
247
248 #[cfg(target_os = "macos")]
249 #[cfg(target_os = "freebsd")]
250 pub struct addrinfo {
251     ai_flags: c_int,
252     ai_family: c_int,
253     ai_socktype: c_int,
254     ai_protocol: c_int,
255     ai_addrlen: socklen_t,
256     ai_canonname: *char,
257     ai_addr: *sockaddr,
258     ai_next: *addrinfo
259 }
260
261 #[cfg(windows)]
262 pub struct addrinfo {
263     ai_flags: c_int,
264     ai_family: c_int,
265     ai_socktype: c_int,
266     ai_protocol: c_int,
267     ai_addrlen: size_t,
268     ai_canonname: *char,
269     ai_addr: *sockaddr,
270     ai_next: *addrinfo
271 }
272
273 #[cfg(unix)] pub type uv_uid_t = libc::types::os::arch::posix88::uid_t;
274 #[cfg(unix)] pub type uv_gid_t = libc::types::os::arch::posix88::gid_t;
275 #[cfg(windows)] pub type uv_uid_t = libc::c_uchar;
276 #[cfg(windows)] pub type uv_gid_t = libc::c_uchar;
277
278 #[deriving(Eq)]
279 pub enum uv_handle_type {
280     UV_UNKNOWN_HANDLE,
281     UV_ASYNC,
282     UV_CHECK,
283     UV_FS_EVENT,
284     UV_FS_POLL,
285     UV_HANDLE,
286     UV_IDLE,
287     UV_NAMED_PIPE,
288     UV_POLL,
289     UV_PREPARE,
290     UV_PROCESS,
291     UV_STREAM,
292     UV_TCP,
293     UV_TIMER,
294     UV_TTY,
295     UV_UDP,
296     UV_SIGNAL,
297     UV_FILE,
298     UV_HANDLE_TYPE_MAX
299 }
300
301 #[cfg(unix)]
302 #[deriving(Eq)]
303 pub enum uv_req_type {
304     UV_UNKNOWN_REQ,
305     UV_REQ,
306     UV_CONNECT,
307     UV_WRITE,
308     UV_SHUTDOWN,
309     UV_UDP_SEND,
310     UV_FS,
311     UV_WORK,
312     UV_GETADDRINFO,
313     UV_REQ_TYPE_MAX
314 }
315
316 // uv_req_type may have additional fields defined by UV_REQ_TYPE_PRIVATE.
317 // See UV_REQ_TYPE_PRIVATE at libuv/include/uv-win.h
318 #[cfg(windows)]
319 #[deriving(Eq)]
320 pub enum uv_req_type {
321     UV_UNKNOWN_REQ,
322     UV_REQ,
323     UV_CONNECT,
324     UV_WRITE,
325     UV_SHUTDOWN,
326     UV_UDP_SEND,
327     UV_FS,
328     UV_WORK,
329     UV_GETADDRINFO,
330     UV_ACCEPT,
331     UV_FS_EVENT_REQ,
332     UV_POLL_REQ,
333     UV_PROCESS_EXIT,
334     UV_READ,
335     UV_UDP_RECV,
336     UV_WAKEUP,
337     UV_SIGNAL_REQ,
338     UV_REQ_TYPE_MAX
339 }
340
341 #[deriving(Eq)]
342 pub enum uv_membership {
343     UV_LEAVE_GROUP,
344     UV_JOIN_GROUP
345 }
346
347 pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
348     #[fixed_stack_segment]; #[inline(never)];
349
350     assert!(handle != UV_UNKNOWN_HANDLE && handle != UV_HANDLE_TYPE_MAX);
351     let size = rust_uv_handle_size(handle as uint);
352     let p = malloc(size);
353     assert!(p.is_not_null());
354     return p;
355 }
356
357 pub unsafe fn free_handle(v: *c_void) {
358     #[fixed_stack_segment]; #[inline(never)];
359
360     free(v)
361 }
362
363 pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
364     #[fixed_stack_segment]; #[inline(never)];
365
366     assert!(req != UV_UNKNOWN_REQ && req != UV_REQ_TYPE_MAX);
367     let size = rust_uv_req_size(req as uint);
368     let p = malloc(size);
369     assert!(p.is_not_null());
370     return p;
371 }
372
373 pub unsafe fn free_req(v: *c_void) {
374     #[fixed_stack_segment]; #[inline(never)];
375
376     free(v)
377 }
378
379 #[test]
380 fn handle_sanity_check() {
381     #[fixed_stack_segment]; #[inline(never)];
382     unsafe {
383         assert_eq!(UV_HANDLE_TYPE_MAX as uint, rust_uv_handle_type_max());
384     }
385 }
386
387 #[test]
388 fn request_sanity_check() {
389     #[fixed_stack_segment]; #[inline(never)];
390     unsafe {
391         assert_eq!(UV_REQ_TYPE_MAX as uint, rust_uv_req_type_max());
392     }
393 }
394
395 // XXX Event loops ignore SIGPIPE by default.
396 pub unsafe fn loop_new() -> *c_void {
397     #[fixed_stack_segment]; #[inline(never)];
398
399     return rust_uv_loop_new();
400 }
401
402 pub unsafe fn loop_delete(loop_handle: *c_void) {
403     #[fixed_stack_segment]; #[inline(never)];
404
405     rust_uv_loop_delete(loop_handle);
406 }
407
408 pub unsafe fn run(loop_handle: *c_void) {
409     #[fixed_stack_segment]; #[inline(never)];
410
411     rust_uv_run(loop_handle);
412 }
413
414 pub unsafe fn close<T>(handle: *T, cb: uv_close_cb) {
415     #[fixed_stack_segment]; #[inline(never)];
416
417     rust_uv_close(handle as *c_void, cb);
418 }
419
420 pub unsafe fn walk(loop_handle: *c_void, cb: uv_walk_cb, arg: *c_void) {
421     #[fixed_stack_segment]; #[inline(never)];
422
423     rust_uv_walk(loop_handle, cb, arg);
424 }
425
426 pub unsafe fn idle_init(loop_handle: *uv_loop_t, handle: *uv_idle_t) -> c_int {
427     #[fixed_stack_segment]; #[inline(never)];
428
429     rust_uv_idle_init(loop_handle, handle)
430 }
431
432 pub unsafe fn idle_start(handle: *uv_idle_t, cb: uv_idle_cb) -> c_int {
433     #[fixed_stack_segment]; #[inline(never)];
434
435     rust_uv_idle_start(handle, cb)
436 }
437
438 pub unsafe fn idle_stop(handle: *uv_idle_t) -> c_int {
439     #[fixed_stack_segment]; #[inline(never)];
440
441     rust_uv_idle_stop(handle)
442 }
443
444 pub unsafe fn udp_init(loop_handle: *uv_loop_t, handle: *uv_udp_t) -> c_int {
445     #[fixed_stack_segment]; #[inline(never)];
446
447     return rust_uv_udp_init(loop_handle, handle);
448 }
449
450 pub unsafe fn udp_bind(server: *uv_udp_t, addr: *sockaddr_in, flags: c_uint) -> c_int {
451     #[fixed_stack_segment]; #[inline(never)];
452
453     return rust_uv_udp_bind(server, addr, flags);
454 }
455
456 pub unsafe fn udp_bind6(server: *uv_udp_t, addr: *sockaddr_in6, flags: c_uint) -> c_int {
457     #[fixed_stack_segment]; #[inline(never)];
458
459     return rust_uv_udp_bind6(server, addr, flags);
460 }
461
462 pub unsafe fn udp_send<T>(req: *uv_udp_send_t, handle: *T, buf_in: &[uv_buf_t],
463                           addr: *sockaddr_in, cb: uv_udp_send_cb) -> c_int {
464     #[fixed_stack_segment]; #[inline(never)];
465
466     let buf_ptr = vec::raw::to_ptr(buf_in);
467     let buf_cnt = buf_in.len() as i32;
468     return rust_uv_udp_send(req, handle as *c_void, buf_ptr, buf_cnt, addr, cb);
469 }
470
471 pub unsafe fn udp_send6<T>(req: *uv_udp_send_t, handle: *T, buf_in: &[uv_buf_t],
472                           addr: *sockaddr_in6, cb: uv_udp_send_cb) -> c_int {
473     #[fixed_stack_segment]; #[inline(never)];
474
475     let buf_ptr = vec::raw::to_ptr(buf_in);
476     let buf_cnt = buf_in.len() as i32;
477     return rust_uv_udp_send6(req, handle as *c_void, buf_ptr, buf_cnt, addr, cb);
478 }
479
480 pub unsafe fn udp_recv_start(server: *uv_udp_t, on_alloc: uv_alloc_cb,
481                              on_recv: uv_udp_recv_cb) -> c_int {
482     #[fixed_stack_segment]; #[inline(never)];
483
484     return rust_uv_udp_recv_start(server, on_alloc, on_recv);
485 }
486
487 pub unsafe fn udp_recv_stop(server: *uv_udp_t) -> c_int {
488     #[fixed_stack_segment]; #[inline(never)];
489
490     return rust_uv_udp_recv_stop(server);
491 }
492
493 pub unsafe fn get_udp_handle_from_send_req(send_req: *uv_udp_send_t) -> *uv_udp_t {
494     #[fixed_stack_segment]; #[inline(never)];
495
496     return rust_uv_get_udp_handle_from_send_req(send_req);
497 }
498
499 pub unsafe fn udp_getsockname(handle: *uv_udp_t, name: *sockaddr_storage) -> c_int {
500     #[fixed_stack_segment]; #[inline(never)];
501
502     return rust_uv_udp_getsockname(handle, name);
503 }
504
505 pub unsafe fn udp_set_membership(handle: *uv_udp_t, multicast_addr: *c_char,
506                                  interface_addr: *c_char, membership: uv_membership) -> c_int {
507     #[fixed_stack_segment]; #[inline(never)];
508
509     return rust_uv_udp_set_membership(handle, multicast_addr, interface_addr, membership as c_int);
510 }
511
512 pub unsafe fn udp_set_multicast_loop(handle: *uv_udp_t, on: c_int) -> c_int {
513     #[fixed_stack_segment]; #[inline(never)];
514
515     return rust_uv_udp_set_multicast_loop(handle, on);
516 }
517
518 pub unsafe fn udp_set_multicast_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int {
519     #[fixed_stack_segment]; #[inline(never)];
520
521     return rust_uv_udp_set_multicast_ttl(handle, ttl);
522 }
523
524 pub unsafe fn udp_set_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int {
525     #[fixed_stack_segment]; #[inline(never)];
526
527     return rust_uv_udp_set_ttl(handle, ttl);
528 }
529
530 pub unsafe fn udp_set_broadcast(handle: *uv_udp_t, on: c_int) -> c_int {
531     #[fixed_stack_segment]; #[inline(never)];
532
533     return rust_uv_udp_set_broadcast(handle, on);
534 }
535
536 pub unsafe fn tcp_init(loop_handle: *c_void, handle: *uv_tcp_t) -> c_int {
537     #[fixed_stack_segment]; #[inline(never)];
538
539     return rust_uv_tcp_init(loop_handle, handle);
540 }
541
542 pub unsafe fn tcp_connect(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
543                           addr_ptr: *sockaddr_in, after_connect_cb: uv_connect_cb) -> c_int {
544     #[fixed_stack_segment]; #[inline(never)];
545
546     return rust_uv_tcp_connect(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
547 }
548
549 pub unsafe fn tcp_connect6(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
550                            addr_ptr: *sockaddr_in6, after_connect_cb: uv_connect_cb) -> c_int {
551     #[fixed_stack_segment]; #[inline(never)];
552
553     return rust_uv_tcp_connect6(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
554 }
555
556 pub unsafe fn tcp_bind(tcp_server_ptr: *uv_tcp_t, addr_ptr: *sockaddr_in) -> c_int {
557     #[fixed_stack_segment]; #[inline(never)];
558
559     return rust_uv_tcp_bind(tcp_server_ptr, addr_ptr);
560 }
561
562 pub unsafe fn tcp_bind6(tcp_server_ptr: *uv_tcp_t, addr_ptr: *sockaddr_in6) -> c_int {
563     #[fixed_stack_segment]; #[inline(never)];
564
565     return rust_uv_tcp_bind6(tcp_server_ptr, addr_ptr);
566 }
567
568 pub unsafe fn tcp_getpeername(tcp_handle_ptr: *uv_tcp_t, name: *sockaddr_storage) -> c_int {
569     #[fixed_stack_segment]; #[inline(never)];
570
571     return rust_uv_tcp_getpeername(tcp_handle_ptr, name);
572 }
573
574 pub unsafe fn tcp_getsockname(handle: *uv_tcp_t, name: *sockaddr_storage) -> c_int {
575     #[fixed_stack_segment]; #[inline(never)];
576
577     return rust_uv_tcp_getsockname(handle, name);
578 }
579
580 pub unsafe fn tcp_nodelay(handle: *uv_tcp_t, enable: c_int) -> c_int {
581     #[fixed_stack_segment]; #[inline(never)];
582
583     return rust_uv_tcp_nodelay(handle, enable);
584 }
585
586 pub unsafe fn tcp_keepalive(handle: *uv_tcp_t, enable: c_int, delay: c_uint) -> c_int {
587     #[fixed_stack_segment]; #[inline(never)];
588
589     return rust_uv_tcp_keepalive(handle, enable, delay);
590 }
591
592 pub unsafe fn tcp_simultaneous_accepts(handle: *uv_tcp_t, enable: c_int) -> c_int {
593     #[fixed_stack_segment]; #[inline(never)];
594
595     return rust_uv_tcp_simultaneous_accepts(handle, enable);
596 }
597
598 pub unsafe fn listen<T>(stream: *T, backlog: c_int,
599                         cb: uv_connection_cb) -> c_int {
600     #[fixed_stack_segment]; #[inline(never)];
601
602     return rust_uv_listen(stream as *c_void, backlog, cb);
603 }
604
605 pub unsafe fn accept(server: *c_void, client: *c_void) -> c_int {
606     #[fixed_stack_segment]; #[inline(never)];
607
608     return rust_uv_accept(server as *c_void, client as *c_void);
609 }
610
611 pub unsafe fn write<T>(req: *uv_write_t,
612                        stream: *T,
613                        buf_in: &[uv_buf_t],
614                        cb: uv_write_cb) -> c_int {
615     #[fixed_stack_segment]; #[inline(never)];
616
617     let buf_ptr = vec::raw::to_ptr(buf_in);
618     let buf_cnt = buf_in.len() as i32;
619     return rust_uv_write(req as *c_void, stream as *c_void, buf_ptr, buf_cnt, cb);
620 }
621 pub unsafe fn read_start(stream: *uv_stream_t,
622                          on_alloc: uv_alloc_cb,
623                          on_read: uv_read_cb) -> c_int {
624     #[fixed_stack_segment]; #[inline(never)];
625
626     return rust_uv_read_start(stream as *c_void, on_alloc, on_read);
627 }
628
629 pub unsafe fn read_stop(stream: *uv_stream_t) -> c_int {
630     #[fixed_stack_segment]; #[inline(never)];
631
632     return rust_uv_read_stop(stream as *c_void);
633 }
634
635 pub unsafe fn strerror(err: c_int) -> *c_char {
636     #[fixed_stack_segment]; #[inline(never)];
637     return rust_uv_strerror(err);
638 }
639 pub unsafe fn err_name(err: c_int) -> *c_char {
640     #[fixed_stack_segment]; #[inline(never)];
641     return rust_uv_err_name(err);
642 }
643
644 pub unsafe fn async_init(loop_handle: *c_void,
645                          async_handle: *uv_async_t,
646                          cb: uv_async_cb) -> c_int {
647     #[fixed_stack_segment]; #[inline(never)];
648
649     return rust_uv_async_init(loop_handle, async_handle, cb);
650 }
651
652 pub unsafe fn async_send(async_handle: *uv_async_t) {
653     #[fixed_stack_segment]; #[inline(never)];
654
655     return rust_uv_async_send(async_handle);
656 }
657 pub unsafe fn buf_init(input: *u8, len: uint) -> uv_buf_t {
658     #[fixed_stack_segment]; #[inline(never)];
659
660     let out_buf = uv_buf_t { base: ptr::null(), len: 0 as size_t };
661     let out_buf_ptr = ptr::to_unsafe_ptr(&out_buf);
662     rust_uv_buf_init(out_buf_ptr, input, len as size_t);
663     return out_buf;
664 }
665
666 pub unsafe fn timer_init(loop_ptr: *c_void, timer_ptr: *uv_timer_t) -> c_int {
667     #[fixed_stack_segment]; #[inline(never)];
668
669     return rust_uv_timer_init(loop_ptr, timer_ptr);
670 }
671 pub unsafe fn timer_start(timer_ptr: *uv_timer_t,
672                           cb: uv_timer_cb, timeout: u64,
673                           repeat: u64) -> c_int {
674     #[fixed_stack_segment]; #[inline(never)];
675
676     return rust_uv_timer_start(timer_ptr, cb, timeout, repeat);
677 }
678 pub unsafe fn timer_stop(timer_ptr: *uv_timer_t) -> c_int {
679     #[fixed_stack_segment]; #[inline(never)];
680
681     return rust_uv_timer_stop(timer_ptr);
682 }
683
684 pub unsafe fn is_ip4_addr(addr: *sockaddr) -> bool {
685     #[fixed_stack_segment]; #[inline(never)];
686
687     match rust_uv_is_ipv4_sockaddr(addr) { 0 => false, _ => true }
688 }
689
690 pub unsafe fn is_ip6_addr(addr: *sockaddr) -> bool {
691     #[fixed_stack_segment]; #[inline(never)];
692
693     match rust_uv_is_ipv6_sockaddr(addr) { 0 => false, _ => true }
694 }
695
696 pub unsafe fn malloc_ip4_addr(ip: &str, port: int) -> *sockaddr_in {
697     #[fixed_stack_segment]; #[inline(never)];
698     do ip.with_c_str |ip_buf| {
699         rust_uv_ip4_addrp(ip_buf as *u8, port as libc::c_int)
700     }
701 }
702 pub unsafe fn malloc_ip6_addr(ip: &str, port: int) -> *sockaddr_in6 {
703     #[fixed_stack_segment]; #[inline(never)];
704     do ip.with_c_str |ip_buf| {
705         rust_uv_ip6_addrp(ip_buf as *u8, port as libc::c_int)
706     }
707 }
708
709 pub unsafe fn malloc_sockaddr_storage() -> *sockaddr_storage {
710     #[fixed_stack_segment]; #[inline(never)];
711
712     rust_uv_malloc_sockaddr_storage()
713 }
714
715 pub unsafe fn free_sockaddr_storage(ss: *sockaddr_storage) {
716     #[fixed_stack_segment]; #[inline(never)];
717
718     rust_uv_free_sockaddr_storage(ss);
719 }
720
721 pub unsafe fn free_ip4_addr(addr: *sockaddr_in) {
722     #[fixed_stack_segment]; #[inline(never)];
723
724     rust_uv_free_ip4_addr(addr);
725 }
726
727 pub unsafe fn free_ip6_addr(addr: *sockaddr_in6) {
728     #[fixed_stack_segment]; #[inline(never)];
729
730     rust_uv_free_ip6_addr(addr);
731 }
732
733 pub unsafe fn ip4_name(addr: *sockaddr_in, dst: *u8, size: size_t) -> c_int {
734     #[fixed_stack_segment]; #[inline(never)];
735
736     return rust_uv_ip4_name(addr, dst, size);
737 }
738
739 pub unsafe fn ip6_name(addr: *sockaddr_in6, dst: *u8, size: size_t) -> c_int {
740     #[fixed_stack_segment]; #[inline(never)];
741
742     return rust_uv_ip6_name(addr, dst, size);
743 }
744
745 pub unsafe fn ip4_port(addr: *sockaddr_in) -> c_uint {
746     #[fixed_stack_segment]; #[inline(never)];
747
748    return rust_uv_ip4_port(addr);
749 }
750
751 pub unsafe fn ip6_port(addr: *sockaddr_in6) -> c_uint {
752     #[fixed_stack_segment]; #[inline(never)];
753
754     return rust_uv_ip6_port(addr);
755 }
756
757 pub unsafe fn fs_open(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, flags: int, mode: int,
758                 cb: *u8) -> c_int {
759     #[fixed_stack_segment]; #[inline(never)];
760
761     rust_uv_fs_open(loop_ptr, req, path, flags as c_int, mode as c_int, cb)
762 }
763
764 pub unsafe fn fs_unlink(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
765                 cb: *u8) -> c_int {
766     #[fixed_stack_segment]; #[inline(never)];
767
768     rust_uv_fs_unlink(loop_ptr, req, path, cb)
769 }
770 pub unsafe fn fs_write(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
771                        len: uint, offset: i64, cb: *u8) -> c_int {
772     #[fixed_stack_segment]; #[inline(never)];
773
774     rust_uv_fs_write(loop_ptr, req, fd, buf, len as c_uint, offset, cb)
775 }
776 pub unsafe fn fs_read(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
777                        len: uint, offset: i64, cb: *u8) -> c_int {
778     #[fixed_stack_segment]; #[inline(never)];
779
780     rust_uv_fs_read(loop_ptr, req, fd, buf, len as c_uint, offset, cb)
781 }
782 pub unsafe fn fs_close(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int,
783                 cb: *u8) -> c_int {
784     #[fixed_stack_segment]; #[inline(never)];
785
786     rust_uv_fs_close(loop_ptr, req, fd, cb)
787 }
788 pub unsafe fn fs_stat(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, cb: *u8) -> c_int {
789     #[fixed_stack_segment]; #[inline(never)];
790
791     rust_uv_fs_stat(loop_ptr, req, path, cb)
792 }
793 pub unsafe fn fs_fstat(loop_ptr: *uv_loop_t, req: *uv_fs_t, fd: c_int, cb: *u8) -> c_int {
794     #[fixed_stack_segment]; #[inline(never)];
795
796     rust_uv_fs_fstat(loop_ptr, req, fd, cb)
797 }
798 pub unsafe fn fs_mkdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char, mode: int,
799                 cb: *u8) -> c_int {
800     #[fixed_stack_segment]; #[inline(never)];
801
802     rust_uv_fs_mkdir(loop_ptr, req, path, mode as c_int, cb)
803 }
804 pub unsafe fn fs_rmdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
805                 cb: *u8) -> c_int {
806     #[fixed_stack_segment]; #[inline(never)];
807
808     rust_uv_fs_rmdir(loop_ptr, req, path, cb)
809 }
810 pub unsafe fn fs_readdir(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
811                 flags: c_int, cb: *u8) -> c_int {
812     #[fixed_stack_segment]; #[inline(never)];
813
814     rust_uv_fs_readdir(loop_ptr, req, path, flags, cb)
815 }
816 pub unsafe fn populate_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t) {
817     #[fixed_stack_segment]; #[inline(never)];
818
819     rust_uv_populate_uv_stat(req_in, stat_out)
820 }
821 pub unsafe fn fs_req_cleanup(req: *uv_fs_t) {
822     #[fixed_stack_segment]; #[inline(never)];
823
824     rust_uv_fs_req_cleanup(req);
825 }
826
827 pub unsafe fn spawn(loop_ptr: *c_void, result: *uv_process_t,
828                     options: uv_process_options_t) -> c_int {
829     #[fixed_stack_segment]; #[inline(never)];
830     return rust_uv_spawn(loop_ptr, result, options);
831 }
832
833 pub unsafe fn process_kill(p: *uv_process_t, signum: c_int) -> c_int {
834     #[fixed_stack_segment]; #[inline(never)];
835     return rust_uv_process_kill(p, signum);
836 }
837
838 pub unsafe fn process_pid(p: *uv_process_t) -> c_int {
839     #[fixed_stack_segment]; #[inline(never)];
840     return rust_uv_process_pid(p);
841 }
842
843 pub unsafe fn set_stdio_container_flags(c: *uv_stdio_container_t,
844                                         flags: libc::c_int) {
845     #[fixed_stack_segment]; #[inline(never)];
846     rust_set_stdio_container_flags(c, flags);
847 }
848
849 pub unsafe fn set_stdio_container_fd(c: *uv_stdio_container_t,
850                                      fd: libc::c_int) {
851     #[fixed_stack_segment]; #[inline(never)];
852     rust_set_stdio_container_fd(c, fd);
853 }
854
855 pub unsafe fn set_stdio_container_stream(c: *uv_stdio_container_t,
856                                          stream: *uv_stream_t) {
857     #[fixed_stack_segment]; #[inline(never)];
858     rust_set_stdio_container_stream(c, stream);
859 }
860
861 pub unsafe fn pipe_init(loop_ptr: *c_void, p: *uv_pipe_t, ipc: c_int) -> c_int {
862     #[fixed_stack_segment]; #[inline(never)];
863     rust_uv_pipe_init(loop_ptr, p, ipc)
864 }
865
866 // data access helpers
867 pub unsafe fn get_result_from_fs_req(req: *uv_fs_t) -> c_int {
868     #[fixed_stack_segment]; #[inline(never)];
869
870     rust_uv_get_result_from_fs_req(req)
871 }
872 pub unsafe fn get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void {
873     #[fixed_stack_segment]; #[inline(never)];
874
875     rust_uv_get_ptr_from_fs_req(req)
876 }
877 pub unsafe fn get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t {
878     #[fixed_stack_segment]; #[inline(never)];
879
880     rust_uv_get_loop_from_fs_req(req)
881 }
882 pub unsafe fn get_loop_from_getaddrinfo_req(req: *uv_getaddrinfo_t) -> *uv_loop_t {
883     #[fixed_stack_segment]; #[inline(never)];
884
885     rust_uv_get_loop_from_getaddrinfo_req(req)
886 }
887 pub unsafe fn get_loop_for_uv_handle<T>(handle: *T) -> *c_void {
888     #[fixed_stack_segment]; #[inline(never)];
889
890     return rust_uv_get_loop_for_uv_handle(handle as *c_void);
891 }
892 pub unsafe fn get_stream_handle_from_connect_req(connect: *uv_connect_t) -> *uv_stream_t {
893     #[fixed_stack_segment]; #[inline(never)];
894
895     return rust_uv_get_stream_handle_from_connect_req(connect);
896 }
897 pub unsafe fn get_stream_handle_from_write_req(write_req: *uv_write_t) -> *uv_stream_t {
898     #[fixed_stack_segment]; #[inline(never)];
899
900     return rust_uv_get_stream_handle_from_write_req(write_req);
901 }
902 pub unsafe fn get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void {
903     #[fixed_stack_segment]; #[inline(never)];
904
905     rust_uv_get_data_for_uv_loop(loop_ptr)
906 }
907 pub unsafe fn set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void) {
908     #[fixed_stack_segment]; #[inline(never)];
909
910     rust_uv_set_data_for_uv_loop(loop_ptr, data);
911 }
912 pub unsafe fn get_data_for_uv_handle<T>(handle: *T) -> *c_void {
913     #[fixed_stack_segment]; #[inline(never)];
914
915     return rust_uv_get_data_for_uv_handle(handle as *c_void);
916 }
917 pub unsafe fn set_data_for_uv_handle<T, U>(handle: *T, data: *U) {
918     #[fixed_stack_segment]; #[inline(never)];
919
920     rust_uv_set_data_for_uv_handle(handle as *c_void, data as *c_void);
921 }
922 pub unsafe fn get_data_for_req<T>(req: *T) -> *c_void {
923     #[fixed_stack_segment]; #[inline(never)];
924
925     return rust_uv_get_data_for_req(req as *c_void);
926 }
927 pub unsafe fn set_data_for_req<T, U>(req: *T, data: *U) {
928     #[fixed_stack_segment]; #[inline(never)];
929
930     rust_uv_set_data_for_req(req as *c_void, data as *c_void);
931 }
932 pub unsafe fn get_base_from_buf(buf: uv_buf_t) -> *u8 {
933     #[fixed_stack_segment]; #[inline(never)];
934
935     return rust_uv_get_base_from_buf(buf);
936 }
937 pub unsafe fn get_len_from_buf(buf: uv_buf_t) -> size_t {
938     #[fixed_stack_segment]; #[inline(never)];
939
940     return rust_uv_get_len_from_buf(buf);
941 }
942 pub unsafe fn getaddrinfo(loop_: *uv_loop_t, req: *uv_getaddrinfo_t,
943                getaddrinfo_cb: uv_getaddrinfo_cb,
944                node: *c_char, service: *c_char,
945                hints: *addrinfo) -> c_int {
946     #[fixed_stack_segment]; #[inline(never)];
947     return rust_uv_getaddrinfo(loop_, req, getaddrinfo_cb, node, service, hints);
948 }
949 pub unsafe fn freeaddrinfo(ai: *addrinfo) {
950     #[fixed_stack_segment]; #[inline(never)];
951     rust_uv_freeaddrinfo(ai);
952 }
953 pub unsafe fn pipe_open(pipe: *uv_pipe_t, file: c_int) -> c_int {
954     #[fixed_stack_segment]; #[inline(never)];
955     rust_uv_pipe_open(pipe, file)
956 }
957 pub unsafe fn pipe_bind(pipe: *uv_pipe_t, name: *c_char) -> c_int {
958     #[fixed_stack_segment]; #[inline(never)];
959     rust_uv_pipe_bind(pipe, name)
960 }
961 pub unsafe fn pipe_connect(req: *uv_connect_t, handle: *uv_pipe_t,
962                            name: *c_char, cb: uv_connect_cb) {
963     #[fixed_stack_segment]; #[inline(never)];
964     rust_uv_pipe_connect(req, handle, name, cb)
965 }
966 pub unsafe fn tty_init(loop_ptr: *uv_loop_t, tty: *uv_tty_t, fd: c_int,
967                        readable: c_int) -> c_int {
968     #[fixed_stack_segment]; #[inline(never)];
969     rust_uv_tty_init(loop_ptr, tty, fd, readable)
970 }
971 pub unsafe fn tty_set_mode(tty: *uv_tty_t, mode: c_int) -> c_int {
972     #[fixed_stack_segment]; #[inline(never)];
973     rust_uv_tty_set_mode(tty, mode)
974 }
975 pub unsafe fn tty_get_winsize(tty: *uv_tty_t, width: *c_int,
976                               height: *c_int) -> c_int {
977     #[fixed_stack_segment]; #[inline(never)];
978     rust_uv_tty_get_winsize(tty, width, height)
979 }
980 // FIXME(#9613) this should return uv_handle_type, not a c_int
981 pub unsafe fn guess_handle(fd: c_int) -> c_int {
982     #[fixed_stack_segment]; #[inline(never)];
983     rust_uv_guess_handle(fd)
984 }
985
986 pub unsafe fn signal_init(loop_: *uv_loop_t, handle: *uv_signal_t) -> c_int {
987     #[fixed_stack_segment]; #[inline(never)];
988     return rust_uv_signal_init(loop_, handle);
989 }
990 pub unsafe fn signal_start(handle: *uv_signal_t,
991                            signal_cb: uv_signal_cb,
992                            signum: c_int) -> c_int {
993     #[fixed_stack_segment]; #[inline(never)];
994     return rust_uv_signal_start(handle, signal_cb, signum);
995 }
996 pub unsafe fn signal_stop(handle: *uv_signal_t) -> c_int {
997     #[fixed_stack_segment]; #[inline(never)];
998     return rust_uv_signal_stop(handle);
999 }
1000
1001 pub struct uv_err_data {
1002     err_name: ~str,
1003     err_msg: ~str,
1004 }
1005
1006 // uv_support is the result of compiling rust_uv.cpp
1007 #[link_args = "-luv_support -luv"]
1008 extern {
1009
1010     fn rust_uv_handle_size(type_: uintptr_t) -> size_t;
1011     fn rust_uv_req_size(type_: uintptr_t) -> size_t;
1012     fn rust_uv_handle_type_max() -> uintptr_t;
1013     fn rust_uv_req_type_max() -> uintptr_t;
1014
1015     // libuv public API
1016     fn rust_uv_loop_new() -> *c_void;
1017     fn rust_uv_loop_delete(lp: *c_void);
1018     fn rust_uv_run(loop_handle: *c_void);
1019     fn rust_uv_close(handle: *c_void, cb: uv_close_cb);
1020     fn rust_uv_walk(loop_handle: *c_void, cb: uv_walk_cb, arg: *c_void);
1021
1022     fn rust_uv_idle_init(loop_handle: *uv_loop_t, handle: *uv_idle_t) -> c_int;
1023     fn rust_uv_idle_start(handle: *uv_idle_t, cb: uv_idle_cb) -> c_int;
1024     fn rust_uv_idle_stop(handle: *uv_idle_t) -> c_int;
1025
1026     fn rust_uv_async_send(handle: *uv_async_t);
1027     fn rust_uv_async_init(loop_handle: *c_void,
1028                           async_handle: *uv_async_t,
1029                           cb: uv_async_cb) -> c_int;
1030     fn rust_uv_tcp_init(loop_handle: *c_void, handle_ptr: *uv_tcp_t) -> c_int;
1031     fn rust_uv_buf_init(out_buf: *uv_buf_t, base: *u8, len: size_t);
1032     fn rust_uv_strerror(err: c_int) -> *c_char;
1033     fn rust_uv_err_name(err: c_int) -> *c_char;
1034     fn rust_uv_ip4_addrp(ip: *u8, port: c_int) -> *sockaddr_in;
1035     fn rust_uv_ip6_addrp(ip: *u8, port: c_int) -> *sockaddr_in6;
1036     fn rust_uv_free_ip4_addr(addr: *sockaddr_in);
1037     fn rust_uv_free_ip6_addr(addr: *sockaddr_in6);
1038     fn rust_uv_ip4_name(src: *sockaddr_in, dst: *u8, size: size_t) -> c_int;
1039     fn rust_uv_ip6_name(src: *sockaddr_in6, dst: *u8, size: size_t) -> c_int;
1040     fn rust_uv_ip4_port(src: *sockaddr_in) -> c_uint;
1041     fn rust_uv_ip6_port(src: *sockaddr_in6) -> c_uint;
1042     fn rust_uv_tcp_connect(req: *uv_connect_t, handle: *uv_tcp_t,
1043                            cb: uv_connect_cb,
1044                            addr: *sockaddr_in) -> c_int;
1045     fn rust_uv_tcp_bind(tcp_server: *uv_tcp_t, addr: *sockaddr_in) -> c_int;
1046     fn rust_uv_tcp_connect6(req: *uv_connect_t, handle: *uv_tcp_t,
1047                             cb: uv_connect_cb,
1048                             addr: *sockaddr_in6) -> c_int;
1049     fn rust_uv_tcp_bind6(tcp_server: *uv_tcp_t, addr: *sockaddr_in6) -> c_int;
1050     fn rust_uv_tcp_getpeername(tcp_handle_ptr: *uv_tcp_t, name: *sockaddr_storage) -> c_int;
1051     fn rust_uv_tcp_getsockname(handle: *uv_tcp_t, name: *sockaddr_storage) -> c_int;
1052     fn rust_uv_tcp_nodelay(handle: *uv_tcp_t, enable: c_int) -> c_int;
1053     fn rust_uv_tcp_keepalive(handle: *uv_tcp_t, enable: c_int, delay: c_uint) -> c_int;
1054     fn rust_uv_tcp_simultaneous_accepts(handle: *uv_tcp_t, enable: c_int) -> c_int;
1055
1056     fn rust_uv_udp_init(loop_handle: *uv_loop_t, handle_ptr: *uv_udp_t) -> c_int;
1057     fn rust_uv_udp_bind(server: *uv_udp_t, addr: *sockaddr_in, flags: c_uint) -> c_int;
1058     fn rust_uv_udp_bind6(server: *uv_udp_t, addr: *sockaddr_in6, flags: c_uint) -> c_int;
1059     fn rust_uv_udp_send(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
1060                         buf_cnt: c_int, addr: *sockaddr_in, cb: uv_udp_send_cb) -> c_int;
1061     fn rust_uv_udp_send6(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
1062                          buf_cnt: c_int, addr: *sockaddr_in6, cb: uv_udp_send_cb) -> c_int;
1063     fn rust_uv_udp_recv_start(server: *uv_udp_t,
1064                               on_alloc: uv_alloc_cb,
1065                               on_recv: uv_udp_recv_cb) -> c_int;
1066     fn rust_uv_udp_recv_stop(server: *uv_udp_t) -> c_int;
1067     fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
1068     fn rust_uv_udp_getsockname(handle: *uv_udp_t, name: *sockaddr_storage) -> c_int;
1069     fn rust_uv_udp_set_membership(handle: *uv_udp_t, multicast_addr: *c_char,
1070                                   interface_addr: *c_char, membership: c_int) -> c_int;
1071     fn rust_uv_udp_set_multicast_loop(handle: *uv_udp_t, on: c_int) -> c_int;
1072     fn rust_uv_udp_set_multicast_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int;
1073     fn rust_uv_udp_set_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int;
1074     fn rust_uv_udp_set_broadcast(handle: *uv_udp_t, on: c_int) -> c_int;
1075
1076     fn rust_uv_is_ipv4_sockaddr(addr: *sockaddr) -> c_int;
1077     fn rust_uv_is_ipv6_sockaddr(addr: *sockaddr) -> c_int;
1078     fn rust_uv_malloc_sockaddr_storage() -> *sockaddr_storage;
1079     fn rust_uv_free_sockaddr_storage(ss: *sockaddr_storage);
1080
1081     fn rust_uv_listen(stream: *c_void, backlog: c_int,
1082                       cb: uv_connection_cb) -> c_int;
1083     fn rust_uv_accept(server: *c_void, client: *c_void) -> c_int;
1084     fn rust_uv_write(req: *c_void, stream: *c_void, buf_in: *uv_buf_t, buf_cnt: c_int,
1085                      cb: uv_write_cb) -> c_int;
1086     fn rust_uv_read_start(stream: *c_void,
1087                           on_alloc: uv_alloc_cb,
1088                           on_read: uv_read_cb) -> c_int;
1089     fn rust_uv_read_stop(stream: *c_void) -> c_int;
1090     fn rust_uv_timer_init(loop_handle: *c_void, timer_handle: *uv_timer_t) -> c_int;
1091     fn rust_uv_timer_start(timer_handle: *uv_timer_t, cb: uv_timer_cb, timeout: libc::uint64_t,
1092                            repeat: libc::uint64_t) -> c_int;
1093     fn rust_uv_timer_stop(handle: *uv_timer_t) -> c_int;
1094     fn rust_uv_fs_open(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1095                        flags: c_int, mode: c_int, cb: *u8) -> c_int;
1096     fn rust_uv_fs_unlink(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1097                        cb: *u8) -> c_int;
1098     fn rust_uv_fs_write(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int,
1099                        buf: *c_void, len: c_uint, offset: i64, cb: *u8) -> c_int;
1100     fn rust_uv_fs_read(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int,
1101                        buf: *c_void, len: c_uint, offset: i64, cb: *u8) -> c_int;
1102     fn rust_uv_fs_close(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int,
1103                         cb: *u8) -> c_int;
1104     fn rust_uv_fs_stat(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char, cb: *u8) -> c_int;
1105     fn rust_uv_fs_fstat(loop_ptr: *c_void, req: *uv_fs_t, fd: c_int, cb: *u8) -> c_int;
1106     fn rust_uv_fs_mkdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1107                         mode: c_int, cb: *u8) -> c_int;
1108     fn rust_uv_fs_rmdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1109                         cb: *u8) -> c_int;
1110     fn rust_uv_fs_readdir(loop_ptr: *c_void, req: *uv_fs_t, path: *c_char,
1111                         flags: c_int, cb: *u8) -> c_int;
1112     fn rust_uv_fs_req_cleanup(req: *uv_fs_t);
1113     fn rust_uv_populate_uv_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t);
1114     fn rust_uv_get_result_from_fs_req(req: *uv_fs_t) -> c_int;
1115     fn rust_uv_get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void;
1116     fn rust_uv_get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t;
1117     fn rust_uv_get_loop_from_getaddrinfo_req(req: *uv_fs_t) -> *uv_loop_t;
1118
1119     fn rust_uv_get_stream_handle_from_connect_req(connect_req: *uv_connect_t) -> *uv_stream_t;
1120     fn rust_uv_get_stream_handle_from_write_req(write_req: *uv_write_t) -> *uv_stream_t;
1121     fn rust_uv_get_loop_for_uv_handle(handle: *c_void) -> *c_void;
1122     fn rust_uv_get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void;
1123     fn rust_uv_set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void);
1124     fn rust_uv_get_data_for_uv_handle(handle: *c_void) -> *c_void;
1125     fn rust_uv_set_data_for_uv_handle(handle: *c_void, data: *c_void);
1126     fn rust_uv_get_data_for_req(req: *c_void) -> *c_void;
1127     fn rust_uv_set_data_for_req(req: *c_void, data: *c_void);
1128     fn rust_uv_get_base_from_buf(buf: uv_buf_t) -> *u8;
1129     fn rust_uv_get_len_from_buf(buf: uv_buf_t) -> size_t;
1130     fn rust_uv_getaddrinfo(loop_: *uv_loop_t, req: *uv_getaddrinfo_t,
1131                            getaddrinfo_cb: uv_getaddrinfo_cb,
1132                            node: *c_char, service: *c_char,
1133                            hints: *addrinfo) -> c_int;
1134     fn rust_uv_freeaddrinfo(ai: *addrinfo);
1135     fn rust_uv_spawn(loop_ptr: *c_void, outptr: *uv_process_t,
1136                      options: uv_process_options_t) -> c_int;
1137     fn rust_uv_process_kill(p: *uv_process_t, signum: c_int) -> c_int;
1138     fn rust_uv_process_pid(p: *uv_process_t) -> c_int;
1139     fn rust_set_stdio_container_flags(c: *uv_stdio_container_t, flags: c_int);
1140     fn rust_set_stdio_container_fd(c: *uv_stdio_container_t, fd: c_int);
1141     fn rust_set_stdio_container_stream(c: *uv_stdio_container_t,
1142                                        stream: *uv_stream_t);
1143     fn rust_uv_pipe_init(loop_ptr: *c_void, p: *uv_pipe_t, ipc: c_int) -> c_int;
1144
1145     fn rust_uv_pipe_open(pipe: *uv_pipe_t, file: c_int) -> c_int;
1146     fn rust_uv_pipe_bind(pipe: *uv_pipe_t, name: *c_char) -> c_int;
1147     fn rust_uv_pipe_connect(req: *uv_connect_t, handle: *uv_pipe_t,
1148                             name: *c_char, cb: uv_connect_cb);
1149     fn rust_uv_tty_init(loop_ptr: *uv_loop_t, tty: *uv_tty_t, fd: c_int,
1150                         readable: c_int) -> c_int;
1151     fn rust_uv_tty_set_mode(tty: *uv_tty_t, mode: c_int) -> c_int;
1152     fn rust_uv_tty_get_winsize(tty: *uv_tty_t, width: *c_int,
1153                                height: *c_int) -> c_int;
1154     fn rust_uv_guess_handle(fd: c_int) -> c_int;
1155
1156     // XXX: see comments in addrinfo.rs
1157     // These should all really be constants...
1158     //#[rust_stack] pub fn rust_SOCK_STREAM() -> c_int;
1159     //#[rust_stack] pub fn rust_SOCK_DGRAM() -> c_int;
1160     //#[rust_stack] pub fn rust_SOCK_RAW() -> c_int;
1161     //#[rust_stack] pub fn rust_IPPROTO_UDP() -> c_int;
1162     //#[rust_stack] pub fn rust_IPPROTO_TCP() -> c_int;
1163     //#[rust_stack] pub fn rust_AI_ADDRCONFIG() -> c_int;
1164     //#[rust_stack] pub fn rust_AI_ALL() -> c_int;
1165     //#[rust_stack] pub fn rust_AI_CANONNAME() -> c_int;
1166     //#[rust_stack] pub fn rust_AI_NUMERICHOST() -> c_int;
1167     //#[rust_stack] pub fn rust_AI_NUMERICSERV() -> c_int;
1168     //#[rust_stack] pub fn rust_AI_PASSIVE() -> c_int;
1169     //#[rust_stack] pub fn rust_AI_V4MAPPED() -> c_int;
1170
1171     fn rust_uv_signal_init(loop_: *uv_loop_t, handle: *uv_signal_t) -> c_int;
1172     fn rust_uv_signal_start(handle: *uv_signal_t,
1173                             signal_cb: uv_signal_cb,
1174                             signum: c_int) -> c_int;
1175     fn rust_uv_signal_stop(handle: *uv_signal_t) -> c_int;
1176 }
1177
1178 // libuv requires various system libraries to successfully link on some
1179 // platforms
1180 #[cfg(target_os = "linux")]
1181 #[link_args = "-lpthread"]
1182 extern {}
1183
1184 #[cfg(target_os = "win32")]
1185 #[link_args = "-lWs2_32 -lpsapi -liphlpapi"]
1186 extern {}