Skip to content

Commit ffb6404

Browse files
committed
Adjust callbacks in the libraries for the new type of extern fns
cc #3678
1 parent 82a9abb commit ffb6404

File tree

4 files changed

+168
-33
lines changed

4 files changed

+168
-33
lines changed

src/libextra/rl.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@ use std::libc::{c_char, c_int};
1616
use std::local_data;
1717
use std::str;
1818

19+
#[cfg(stage0)]
1920
pub mod rustrt {
2021
use std::libc::{c_char, c_int};
2122

22-
#[cfg(stage0)]
23-
mod macro_hack {
24-
#[macro_escape];
25-
macro_rules! externfn(
26-
(fn $name:ident ($($arg_name:ident : $arg_ty:ty),*) $(-> $ret_ty:ty),*) => (
27-
extern {
28-
fn $name($($arg_name : $arg_ty),*) $(-> $ret_ty),*;
29-
}
30-
)
31-
)
23+
extern {
24+
fn linenoise(prompt: *c_char) -> *c_char;
25+
fn linenoiseHistoryAdd(line: *c_char) -> c_int;
26+
fn linenoiseHistorySetMaxLen(len: c_int) -> c_int;
27+
fn linenoiseHistorySave(file: *c_char) -> c_int;
28+
fn linenoiseHistoryLoad(file: *c_char) -> c_int;
29+
fn linenoiseSetCompletionCallback(callback: *u8);
30+
fn linenoiseAddCompletion(completions: *(), line: *c_char);
3231
}
32+
}
33+
34+
#[cfg(not(stage0))]
35+
pub mod rustrt {
36+
use std::libc::{c_char, c_int};
3337

3438
externfn!(fn linenoise(prompt: *c_char) -> *c_char)
3539
externfn!(fn linenoiseHistoryAdd(line: *c_char) -> c_int)
3640
externfn!(fn linenoiseHistorySetMaxLen(len: c_int) -> c_int)
3741
externfn!(fn linenoiseHistorySave(file: *c_char) -> c_int)
3842
externfn!(fn linenoiseHistoryLoad(file: *c_char) -> c_int)
39-
externfn!(fn linenoiseSetCompletionCallback(callback: *u8))
43+
externfn!(fn linenoiseSetCompletionCallback(callback: extern "C" fn(*i8, *())))
4044
externfn!(fn linenoiseAddCompletion(completions: *(), line: *c_char))
4145
}
4246

src/libstd/ptr.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,47 @@ impl<T> Eq for *const T {
369369
fn ne(&self, other: &*const T) -> bool { !self.eq(other) }
370370
}
371371

372+
// Equality for extern "C" fn pointers
373+
#[cfg(not(test))]
374+
mod externfnpointers {
375+
use cast;
376+
use cmp::Eq;
377+
378+
impl<_R> Eq for extern "C" fn() -> _R {
379+
#[inline]
380+
fn eq(&self, other: &extern "C" fn() -> _R) -> bool {
381+
let self_: *() = unsafe { cast::transmute(*self) };
382+
let other_: *() = unsafe { cast::transmute(*other) };
383+
self_ == other_
384+
}
385+
#[inline]
386+
fn ne(&self, other: &extern "C" fn() -> _R) -> bool {
387+
!self.eq(other)
388+
}
389+
}
390+
macro_rules! fnptreq(
391+
($($p:ident),*) => {
392+
impl<_R,$($p),*> Eq for extern "C" fn($($p),*) -> _R {
393+
#[inline]
394+
fn eq(&self, other: &extern "C" fn($($p),*) -> _R) -> bool {
395+
let self_: *() = unsafe { cast::transmute(*self) };
396+
let other_: *() = unsafe { cast::transmute(*other) };
397+
self_ == other_
398+
}
399+
#[inline]
400+
fn ne(&self, other: &extern "C" fn($($p),*) -> _R) -> bool {
401+
!self.eq(other)
402+
}
403+
}
404+
}
405+
)
406+
fnptreq!(A)
407+
fnptreq!(A,B)
408+
fnptreq!(A,B,C)
409+
fnptreq!(A,B,C,D)
410+
fnptreq!(A,B,C,D,E)
411+
}
412+
372413
// Comparison for pointers
373414
#[cfg(not(test))]
374415
impl<T> Ord for *const T {

src/libstd/rt/task.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,17 @@ impl Unwinder {
445445
}
446446

447447
extern {
448+
#[cfg(not(stage0))]
448449
#[rust_stack]
449-
fn rust_try(f: *u8, code: *c_void, data: *c_void) -> uintptr_t;
450+
fn rust_try(f: extern "C" fn(*c_void, *c_void),
451+
code: *c_void,
452+
data: *c_void) -> uintptr_t;
453+
454+
#[cfg(stage0)]
455+
#[rust_stack]
456+
fn rust_try(f: *u8,
457+
code: *c_void,
458+
data: *c_void) -> uintptr_t;
450459
}
451460
}
452461

src/libstd/rt/uv/uvll.rs

Lines changed: 102 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
use c_str::ToCStr;
3333
use libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t};
34+
#[cfg(not(stage0))]
35+
use libc::ssize_t;
3436
use libc::{malloc, free};
3537
use libc;
3638
use prelude::*;
@@ -63,17 +65,78 @@ pub type uv_idle_t = c_void;
6365
pub type uv_tcp_t = c_void;
6466
pub type uv_udp_t = c_void;
6567
pub type uv_connect_t = c_void;
68+
pub type uv_connection_t = c_void;
6669
pub type uv_write_t = c_void;
6770
pub type uv_async_t = c_void;
6871
pub type uv_timer_t = c_void;
6972
pub type uv_stream_t = c_void;
7073
pub type uv_fs_t = c_void;
7174
pub type uv_udp_send_t = c_void;
7275

76+
#[cfg(stage0)]
7377
pub type uv_idle_cb = *u8;
78+
#[cfg(stage0)]
7479
pub type uv_alloc_cb = *u8;
80+
#[cfg(stage0)]
81+
pub type uv_read_cb = *u8;
82+
#[cfg(stage0)]
7583
pub type uv_udp_send_cb = *u8;
84+
#[cfg(stage0)]
7685
pub type uv_udp_recv_cb = *u8;
86+
#[cfg(stage0)]
87+
pub type uv_close_cb = *u8;
88+
#[cfg(stage0)]
89+
pub type uv_walk_cb = *u8;
90+
#[cfg(stage0)]
91+
pub type uv_async_cb = *u8;
92+
#[cfg(stage0)]
93+
pub type uv_connect_cb = *u8;
94+
#[cfg(stage0)]
95+
pub type uv_connection_cb = *u8;
96+
#[cfg(stage0)]
97+
pub type uv_timer_cb = *u8;
98+
#[cfg(stage0)]
99+
pub type uv_write_cb = *u8;
100+
101+
#[cfg(not(stage0))]
102+
pub type uv_idle_cb = extern "C" fn(handle: *uv_idle_t,
103+
status: c_int);
104+
#[cfg(not(stage0))]
105+
pub type uv_alloc_cb = extern "C" fn(stream: *uv_stream_t,
106+
suggested_size: size_t) -> uv_buf_t;
107+
#[cfg(not(stage0))]
108+
pub type uv_read_cb = extern "C" fn(stream: *uv_stream_t,
109+
nread: ssize_t,
110+
buf: uv_buf_t);
111+
#[cfg(not(stage0))]
112+
pub type uv_udp_send_cb = extern "C" fn(req: *uv_udp_send_t,
113+
status: c_int);
114+
#[cfg(not(stage0))]
115+
pub type uv_udp_recv_cb = extern "C" fn(handle: *uv_udp_t,
116+
nread: ssize_t,
117+
buf: uv_buf_t,
118+
addr: *sockaddr,
119+
flags: c_uint);
120+
#[cfg(not(stage0))]
121+
pub type uv_close_cb = extern "C" fn(handle: *uv_handle_t);
122+
#[cfg(not(stage0))]
123+
pub type uv_walk_cb = extern "C" fn(handle: *uv_handle_t,
124+
arg: *c_void);
125+
#[cfg(not(stage0))]
126+
pub type uv_async_cb = extern "C" fn(handle: *uv_async_t,
127+
status: c_int);
128+
#[cfg(not(stage0))]
129+
pub type uv_connect_cb = extern "C" fn(handle: *uv_connect_t,
130+
status: c_int);
131+
#[cfg(not(stage0))]
132+
pub type uv_connection_cb = extern "C" fn(handle: *uv_connection_t,
133+
status: c_int);
134+
#[cfg(not(stage0))]
135+
pub type uv_timer_cb = extern "C" fn(handle: *uv_timer_t,
136+
status: c_int);
137+
#[cfg(not(stage0))]
138+
pub type uv_write_cb = extern "C" fn(handle: *uv_write_t,
139+
status: c_int);
77140

78141
pub type sockaddr = c_void;
79142
pub type sockaddr_in = c_void;
@@ -191,13 +254,13 @@ pub unsafe fn run(loop_handle: *c_void) {
191254
rust_uv_run(loop_handle);
192255
}
193256

194-
pub unsafe fn close<T>(handle: *T, cb: *u8) {
257+
pub unsafe fn close<T>(handle: *T, cb: uv_close_cb) {
195258
#[fixed_stack_segment]; #[inline(never)];
196259

197260
rust_uv_close(handle as *c_void, cb);
198261
}
199262

200-
pub unsafe fn walk(loop_handle: *c_void, cb: *u8, arg: *c_void) {
263+
pub unsafe fn walk(loop_handle: *c_void, cb: uv_walk_cb, arg: *c_void) {
201264
#[fixed_stack_segment]; #[inline(never)];
202265

203266
rust_uv_walk(loop_handle, cb, arg);
@@ -332,14 +395,14 @@ pub unsafe fn tcp_init(loop_handle: *c_void, handle: *uv_tcp_t) -> c_int {
332395
}
333396

334397
pub unsafe fn tcp_connect(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
335-
addr_ptr: *sockaddr_in, after_connect_cb: *u8) -> c_int {
398+
addr_ptr: *sockaddr_in, after_connect_cb: uv_connect_cb) -> c_int {
336399
#[fixed_stack_segment]; #[inline(never)];
337400

338401
return rust_uv_tcp_connect(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
339402
}
340403

341404
pub unsafe fn tcp_connect6(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
342-
addr_ptr: *sockaddr_in6, after_connect_cb: *u8) -> c_int {
405+
addr_ptr: *sockaddr_in6, after_connect_cb: uv_connect_cb) -> c_int {
343406
#[fixed_stack_segment]; #[inline(never)];
344407

345408
return rust_uv_tcp_connect6(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
@@ -387,7 +450,8 @@ pub unsafe fn tcp_simultaneous_accepts(handle: *uv_tcp_t, enable: c_int) -> c_in
387450
return rust_uv_tcp_simultaneous_accepts(handle, enable);
388451
}
389452

390-
pub unsafe fn listen<T>(stream: *T, backlog: c_int, cb: *u8) -> c_int {
453+
pub unsafe fn listen<T>(stream: *T, backlog: c_int,
454+
cb: uv_connection_cb) -> c_int {
391455
#[fixed_stack_segment]; #[inline(never)];
392456

393457
return rust_uv_listen(stream as *c_void, backlog, cb);
@@ -399,14 +463,19 @@ pub unsafe fn accept(server: *c_void, client: *c_void) -> c_int {
399463
return rust_uv_accept(server as *c_void, client as *c_void);
400464
}
401465

402-
pub unsafe fn write<T>(req: *uv_write_t, stream: *T, buf_in: &[uv_buf_t], cb: *u8) -> c_int {
466+
pub unsafe fn write<T>(req: *uv_write_t,
467+
stream: *T,
468+
buf_in: &[uv_buf_t],
469+
cb: uv_write_cb) -> c_int {
403470
#[fixed_stack_segment]; #[inline(never)];
404471

405472
let buf_ptr = vec::raw::to_ptr(buf_in);
406473
let buf_cnt = buf_in.len() as i32;
407474
return rust_uv_write(req as *c_void, stream as *c_void, buf_ptr, buf_cnt, cb);
408475
}
409-
pub unsafe fn read_start(stream: *uv_stream_t, on_alloc: uv_alloc_cb, on_read: *u8) -> c_int {
476+
pub unsafe fn read_start(stream: *uv_stream_t,
477+
on_alloc: uv_alloc_cb,
478+
on_read: uv_read_cb) -> c_int {
410479
#[fixed_stack_segment]; #[inline(never)];
411480

412481
return rust_uv_read_start(stream as *c_void, on_alloc, on_read);
@@ -435,7 +504,9 @@ pub unsafe fn err_name(err: *uv_err_t) -> *c_char {
435504
return rust_uv_err_name(err);
436505
}
437506

438-
pub unsafe fn async_init(loop_handle: *c_void, async_handle: *uv_async_t, cb: *u8) -> c_int {
507+
pub unsafe fn async_init(loop_handle: *c_void,
508+
async_handle: *uv_async_t,
509+
cb: uv_async_cb) -> c_int {
439510
#[fixed_stack_segment]; #[inline(never)];
440511

441512
return rust_uv_async_init(loop_handle, async_handle, cb);
@@ -460,7 +531,8 @@ pub unsafe fn timer_init(loop_ptr: *c_void, timer_ptr: *uv_timer_t) -> c_int {
460531

461532
return rust_uv_timer_init(loop_ptr, timer_ptr);
462533
}
463-
pub unsafe fn timer_start(timer_ptr: *uv_timer_t, cb: *u8, timeout: u64,
534+
pub unsafe fn timer_start(timer_ptr: *uv_timer_t,
535+
cb: uv_timer_cb, timeout: u64,
464536
repeat: u64) -> c_int {
465537
#[fixed_stack_segment]; #[inline(never)];
466538

@@ -634,8 +706,8 @@ extern {
634706
fn rust_uv_loop_new() -> *c_void;
635707
fn rust_uv_loop_delete(lp: *c_void);
636708
fn rust_uv_run(loop_handle: *c_void);
637-
fn rust_uv_close(handle: *c_void, cb: *u8);
638-
fn rust_uv_walk(loop_handle: *c_void, cb: *u8, arg: *c_void);
709+
fn rust_uv_close(handle: *c_void, cb: uv_close_cb);
710+
fn rust_uv_walk(loop_handle: *c_void, cb: uv_walk_cb, arg: *c_void);
639711

640712
fn rust_uv_idle_new() -> *uv_idle_t;
641713
fn rust_uv_idle_delete(handle: *uv_idle_t);
@@ -644,7 +716,9 @@ extern {
644716
fn rust_uv_idle_stop(handle: *uv_idle_t) -> c_int;
645717

646718
fn rust_uv_async_send(handle: *uv_async_t);
647-
fn rust_uv_async_init(loop_handle: *c_void, async_handle: *uv_async_t, cb: *u8) -> c_int;
719+
fn rust_uv_async_init(loop_handle: *c_void,
720+
async_handle: *uv_async_t,
721+
cb: uv_async_cb) -> c_int;
648722
fn rust_uv_tcp_init(loop_handle: *c_void, handle_ptr: *uv_tcp_t) -> c_int;
649723
fn rust_uv_buf_init(out_buf: *uv_buf_t, base: *u8, len: size_t);
650724
fn rust_uv_last_error(loop_handle: *c_void) -> uv_err_t;
@@ -658,10 +732,12 @@ extern {
658732
fn rust_uv_ip6_name(src: *sockaddr_in6, dst: *u8, size: size_t) -> c_int;
659733
fn rust_uv_ip4_port(src: *sockaddr_in) -> c_uint;
660734
fn rust_uv_ip6_port(src: *sockaddr_in6) -> c_uint;
661-
fn rust_uv_tcp_connect(req: *uv_connect_t, handle: *uv_tcp_t, cb: *u8,
735+
fn rust_uv_tcp_connect(req: *uv_connect_t, handle: *uv_tcp_t,
736+
cb: uv_connect_cb,
662737
addr: *sockaddr_in) -> c_int;
663738
fn rust_uv_tcp_bind(tcp_server: *uv_tcp_t, addr: *sockaddr_in) -> c_int;
664-
fn rust_uv_tcp_connect6(req: *uv_connect_t, handle: *uv_tcp_t, cb: *u8,
739+
fn rust_uv_tcp_connect6(req: *uv_connect_t, handle: *uv_tcp_t,
740+
cb: uv_connect_cb,
665741
addr: *sockaddr_in6) -> c_int;
666742
fn rust_uv_tcp_bind6(tcp_server: *uv_tcp_t, addr: *sockaddr_in6) -> c_int;
667743
fn rust_uv_tcp_getpeername(tcp_handle_ptr: *uv_tcp_t, name: *sockaddr_storage) -> c_int;
@@ -674,10 +750,12 @@ extern {
674750
fn rust_uv_udp_bind(server: *uv_udp_t, addr: *sockaddr_in, flags: c_uint) -> c_int;
675751
fn rust_uv_udp_bind6(server: *uv_udp_t, addr: *sockaddr_in6, flags: c_uint) -> c_int;
676752
fn rust_uv_udp_send(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
677-
buf_cnt: c_int, addr: *sockaddr_in, cb: *u8) -> c_int;
753+
buf_cnt: c_int, addr: *sockaddr_in, cb: uv_udp_send_cb) -> c_int;
678754
fn rust_uv_udp_send6(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
679-
buf_cnt: c_int, addr: *sockaddr_in6, cb: *u8) -> c_int;
680-
fn rust_uv_udp_recv_start(server: *uv_udp_t, on_alloc: *u8, on_recv: *u8) -> c_int;
755+
buf_cnt: c_int, addr: *sockaddr_in6, cb: uv_udp_send_cb) -> c_int;
756+
fn rust_uv_udp_recv_start(server: *uv_udp_t,
757+
on_alloc: uv_alloc_cb,
758+
on_recv: uv_udp_recv_cb) -> c_int;
681759
fn rust_uv_udp_recv_stop(server: *uv_udp_t) -> c_int;
682760
fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
683761
fn rust_uv_udp_getsockname(handle: *uv_udp_t, name: *sockaddr_storage) -> c_int;
@@ -693,14 +771,17 @@ extern {
693771
fn rust_uv_malloc_sockaddr_storage() -> *sockaddr_storage;
694772
fn rust_uv_free_sockaddr_storage(ss: *sockaddr_storage);
695773

696-
fn rust_uv_listen(stream: *c_void, backlog: c_int, cb: *u8) -> c_int;
774+
fn rust_uv_listen(stream: *c_void, backlog: c_int,
775+
cb: uv_connection_cb) -> c_int;
697776
fn rust_uv_accept(server: *c_void, client: *c_void) -> c_int;
698777
fn rust_uv_write(req: *c_void, stream: *c_void, buf_in: *uv_buf_t, buf_cnt: c_int,
699-
cb: *u8) -> c_int;
700-
fn rust_uv_read_start(stream: *c_void, on_alloc: *u8, on_read: *u8) -> c_int;
778+
cb: uv_write_cb) -> c_int;
779+
fn rust_uv_read_start(stream: *c_void,
780+
on_alloc: uv_alloc_cb,
781+
on_read: uv_read_cb) -> c_int;
701782
fn rust_uv_read_stop(stream: *c_void) -> c_int;
702783
fn rust_uv_timer_init(loop_handle: *c_void, timer_handle: *uv_timer_t) -> c_int;
703-
fn rust_uv_timer_start(timer_handle: *uv_timer_t, cb: *u8, timeout: libc::uint64_t,
784+
fn rust_uv_timer_start(timer_handle: *uv_timer_t, cb: uv_timer_cb, timeout: libc::uint64_t,
704785
repeat: libc::uint64_t) -> c_int;
705786
fn rust_uv_timer_stop(handle: *uv_timer_t) -> c_int;
706787

0 commit comments

Comments
 (0)