On 19/02/2012 2:21 PM, Brian Anderson wrote:
----- Original Message -----
From: "Zack Corr"<[email protected]>
To: [email protected]
Sent: Sunday, February 19, 2012 1:42:52 AM
Subject: [rust-dev] Why exactly do we need low-level libuv bindings?
The current target for std::uv are a set of low-level libuv bindings,
closely mimicking the C API. Why exactly do they need to be
low-level?
We don't need them from an end user perspective, but the task of creating
bindings to this complex API is difficult enough that, in my opinion, it would
be cleaner and more maintainable to design it in two layers. Others have
disagreed with me on this before though.
I concur with Brian here. And I think there's a pretty explicit reason:
C interfaces are semantically "thinner" than rust interfaces. A "good"
(easy, friendly) user-facng rust interface typically takes and returns
some combination of closures, interfaces, enums, vectors, polymorphic
functions, etc. Whereas a C interface is typically just calls to static
functions passing either scalar C types or raw pointers to buffers /
opaque C structures.
It's much, much easier to work with the C values when in rust than it is
to work with the rust values when in C. That is, to write the code that
"translates down" from the richer rust semantics to the sparser C
semantics _in rust_ rather than on the C side of the FFI. You have to
"make no mistakes" no matter which side you write it on (you can shoot
yourself in the foot in C or in "unsafe rust" just as easily) but it
happens to be a lot _easier_ to write the code that looks at the rust
values when writing in rust. We have pattern matching and polymorphic
dispatch and whatnot.
So the easiest way to write a friendly library binding, it seems, is to
write a minimal (as minimal as possible) binding to the C part, and then
do a second layer, in rust, that does that translation-down from
rust-friendly form. This is what we're going to be doing as we redo the
libc / os interface.
The one place this wasn't true, until recently, was in task activation
and single-threaded callbacks. When C needed to do that, we couldn't
model its need in rust, so had to push logic -- often far too much --
into C code. But Brian recently cracked that nut via the new explicit
schedulers, so I *think* we're ok again (fingers crossed).
-Graydon
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev