I'm a completely newbie here working on a hobby project and I want to try (and learn) Rust this time. And the project itself includes a HTTP server and a TCP server, so I thought I may need some network support.
Digging through the GitHub, I found
tokio project, but they're both under 1.0 yet, so I turn my head around searching for libuv (which is very handy in C) bindings.
That's when I discovered this crate by sorear, but it gives me segment fault (Of course it does, the
uv_loop_t has been changed in the newer version). And it's the only libuv binding crate out there as far as I can found.
So I started binding the libuv myself (TCP part of it actually, and which is very hard consider I only half way through the Rust book).
My main problem here is the size of those C structures. For struct like
uv_loop_t, they made a function named
uv_loop_size which will return the size of the
uv_loop_t so I can just
libc::malloc that size of memory. But for many other structs, they didn't provide such function.
So, is that to say I have to manually add each member of the C struct to a Rust struct and keep them aligned in memory? If true then, that means a lot of work (and keep ups) ...
My second problem is about struct casting. In C, I can cast anything by just
(something) val, but it seems Rust won't allow me do that.
For example, when I trying to cast
libc::sockaddr by using
as, the Rust compiler told me it's invalid, so I had to
mem::transmute it. But ...
mem::transmute do that by
copies the bits from the source value into the destination value, and I don't like it. So I want to know is there any other better way to cast these struct?
BTW: I've tried
bindgen, and failed to get it to work for libuv correctly, sad