4 ms·
For someone not familliar with Windows API, why does creating a Windows needs unsafe and other low level things? I guess it's the same for the C++/C# version?
by Thaxll 6y ago
For someone not familliar with Windows API, why does creating a Windows needs unsafe and other low level things? I guess it's the same for the C++/C# version?
https://github.com/kennykerr/samples-rs/blob/master/create_window/src/main.rs#L15 https://github.com/kennykerr/samples-rs/blob/master/create_w...
- steveklabnik 6y agoAnything not defined in Rust needs to be marked as unsafe, because Rust cannot understand non-Rust code. FFI bindings are inherently unsafe.
- ywei3410 6y agoOn a side-note; do you know of any good resources where someone tries to wrap a non-trivial C wrapper where they go over common C idioms and ways to provide a (safer) API inside Rust without too much overhead? I've always found it to be incredibly difficult because of the number of gotchas which can leak into causing a segfault in Rust; it's immensely frustrating.
- sensitive-ears 6y agoIt's more of an example than a resource, but the 'git2' crate that wraps libgit2 does a good job of providing a nice rust interface over a C library.
- cetra3 6y agoI think maybe the ZeroMQ rust crate as a case study is something that is non-trivial: https://github.com/erickt/rust-zmq https://github.com/erickt/rust-zmq
- eska 6y agohttps://medium.com/dwelo-r-d/using-c-libraries-in-rust-13961948c72a https://medium.com/dwelo-r-d/using-c-libraries-in-rust-13961...
- steveklabnik 6y agoMy co-author Carol Nichols gave a few talks on this a while back, but there's not a ton of resources, it's true. I would look at big libraries and see what they do. I know it's not the best thing, but it's probably the best that exists right now.
- metalliqaz 6y agoIt's a call out to Windows libraries that long predate Rust, and they are implemented in (mostly) C++. They don't provide any of the safety features on any data structure you pass to it. I don't see how it could be anything other than unsafe.
- Blikkentrekker 6y agoYet the xcb bindings in that crate are mostly safe: https://rtbo.github.io/rust-xcb/xcb/xproto/fn.create_window.html https://rtbo.github.io/rust-xcb/xcb/xproto/fn.create_window.... They have provided nontrivial wrappers, wrapping xcb datatypes so to make them safe and uphold the invariants. I don't know much of any Windows interfaces, but I doubt this would be impossible there too; they simply did not do so.
- metalliqaz 6y agoThe Windows API is a sprawling mess.
- Blikkentrekker 6y agoI really doubt that would have anything to do with whether or not the binding wrote safe, Rust-esque wrappers or not.
- tadfisher 6y agoDoes it actually wrap libxcb, or does it generate bindings from the XCB protocol descriptions (XML)? I would think the latter would be less work and higher quality.
- Blikkentrekker 6y agoI don't know how it's achieved, but as I look at the bindings for this Windows a.p.i they return all sorts of raw pointers and other things, whereas in XCB bindings for Rust wrap the types in a Rust-friendly way so almost all of them are safe. This Windows a.p.i. in Rust seems of a frankly atrocious design and most of the functions seem simple `extern "C"` declarations rather than actual attempts at proper wrapping.
- deleted 6y ago[deleted]