4 ms·
What about good practice for the actual writing of Rust library code? I know C++ and there we have advice such as not reordering virtual methods, not using STL
by jzwinck 11y ago
What about good practice for the actual writing of Rust library code? I know C++ and there we have advice such as not reordering virtual methods, not using STL types in public APIs, not reusing enum values, exception safety, RAII, etc. What about in Rust? What are some guidelines for writing libraries which will give their users maximum convenience and safety? Is there a stable ABI, and if so what do library implementers need to know about it? How about writing C APIs in Rust?
- kibwen 11y ago> not reordering virtual methods, not using STL types in > public APIs, not reusing enum values There's no reason to worry about any of these in Rust. The design of the language is intended to minimize and discourage footguns in general. > exception safety Exception safety is only a concern from within `unsafe` blocks. If you're using `unsafe` blocks at all, 1) really try not to, 2) if you still must use them, read The Rustonomicon first (here's the specific chapter on exception safety: https://doc.rust-lang.org/nightly/nomicon/exception-safety.html https://doc.rust-lang.org/nightly/nomicon/exception-safety.h... ). > What are some guidelines for writing libraries which > will give their users maximum convenience and safety? Not many guidelines spring to mind. The first is still "don't use `unsafe` blocks if you can help it" (the OP gets into this by mentioning the lint that denies unsafe code in your program, which gives your users an easy way to determine your policy on avoiding unsafe code). The second is "don't panic in library code, use the Result type instead". > Is there a stable ABI Nope! Far-future work. > How about writing C APIs in Rust? Now this is something that could use real documentation and advice like you're requesting. Nothing authoritative springs to mind, but here's a recent experience report on the process to get people started: http://www.joshmatthews.net/blog/2015/10/creating-a-c-api-for-a-rust-library/ http://www.joshmatthews.net/blog/2015/10/creating-a-c-api-fo...