[rust-dev] How to allocate record on memory?

Alexander Stavonin a.stavonin at gmail.com
Sun Apr 15 17:51:27 PDT 2012


I have a C function with void* argument wich will be passed to callback and
I want to pass Rust record as the argument. The question is, how to
allocate record on memory but not on the stack? I've tried a lot of
different ways, but with same result: in callback function record data
looked as already destroyed.

example:

...
type data_rec = {
    on_connect_cb: fn@(listner: *evconnlistener, sock: c_int) -> bool,
    data: int,
    data1: int
};

fn re_listener_new_bind(ev_base: *event_base, flags: [listner_flags], addr:
sockaddr,
            on_connect: fn@(listner: *evconnlistener, sock: c_int) -> bool)
        -> *evconnlistener unsafe {
...
// initialising callback:

    let callback = unsafe{ {on_connect_cb: on_connect, data: 10, data1:
-10} }; // will the record be allocated on the stack?
    ret ev::evconnlistener_new_bind(ev_base, connect_callback, data,
res_flags, -1 as c_int,
        ptr::addr_of(a), l as c_int);
}

crust fn connect_callback(listner: *evconnlistener, sock: c_int,
    sockaddr: *c_void, len: c_int, ptr: *c_void) unsafe {

    let data = ptr as *data_rec;
    io::println(#fmt("%u, data: %d, data1 %d", ptr as uint, (*data).data,
(*data).data1));
}
...

console output:

1088426520, data: 1088426784, data1 146814400

expected output:
1088426520, data: 10, data1 -10
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120416/ab727859/attachment.html>


More information about the Rust-dev mailing list