Hi,
I'm trying to interface to some C code which uses a lot of structs. I
can declare these using raw pointers, but then I lose the benefits of
Rust's compile-time pointer checking. So I tried replacing the raw
pointers with owned pointers, which more accurately captures the
meaning of the C interface. But when I do this, the pointers get
offset by 32 bytes.
Here's my test-case:
use core::libc::c_void;
struct WithRaw {
p: *c_void
}
struct WithOwned {
p: ~c_void
}
fn main() {
let p = unsafe { cast::transmute(100) };
let raw_s = &WithRaw{p: p};
let raw_p = raw_s.p;
io::println(fmt!("raw_s.p = %?", raw_p));
let owned_s :&WithOwned = unsafe { cast::transmute(raw_s) };
let owned_p = ptr::to_unsafe_ptr(owned_s.p);
io::println(fmt!("owned_s.p = %?", owned_p));
}
This prints (rust 0.6):
raw_s.p = 100
owned_s.p = 132
This makes me think that owned structures perhaps each have an extra
32 byte header, but I can't imagine why that would be. I thought all
the checking of owned pointers happened at compile time?
For example, I'd like to replace this:
struct xmlNode {
name: *i8,
...
}
with:
struct CString;
struct xmlNode {
name: ~CString,
...
}
Then I could (I assume) define safe functions on my CString type (len,
eq, etc) and not have to worry about memory leaks, etc when using them
from safe code.
Is this possible?
Thanks,
--
Dr Thomas Leonard http://0install.net/
GPG: 9242 9807 C985 3C07 44A6 8B9A AE07 8280 59A5 3CC1
GPG: DA98 25AE CAD0 8975 7CDA BD8E 0713 3F96 CA74 D8BA
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev