News for hackers. Stuff that matters.
Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by rattray on Sunday July 10, 2016 @12:32PM (#12066130)
I really hope nobody mistakes the sexy title of this book as a reason to learn unsafe rust. If it can be done without this advanced feature, it probably should.
Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by vvanders on Sunday July 10, 2016 @12:41PM (#12066159)
> However if you intend to write unsafe code -- or just want to dig into the guts of the language -- this book contains invaluable information.

Agreed, however I think the latter part of the intro is worthwhile to anyone who wants a deeper understanding of Rust.

I've always found that a in depth understanding of what you use pays off in unforeseen ways.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by bluejekyll on Sunday July 10, 2016 @12:43PM (#12066164)
It's true. I have a 30k+ LoC project, and it has zero unsafe code blocks.

Though I do have another project that exports a C API, and that requires unsafe, though it's literally only for wrapping/unwrapping pointers.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by sanderjd on Sunday July 10, 2016 @12:50PM (#12066188)
Are you able to say what your project is? I'm always curious to hear about bigger projects!
Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by bluejekyll on Sunday July 10, 2016 @01:06PM (#12066242)
Sure: https://github.com/bluejekyll/trust-dns

It's a WIP, but mostly feature complete at this point. I'm working on DNSCrypt at the moment.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by e12e on Sunday July 10, 2016 @01:54PM (#12066431)
Nice project. Seems a bit light on documentation? Is the intention to be a bind work-alike, or have you taken some inspiration from other servers, like eg: DJB's djbdns?

[ed: just found src/config/test - looks more like a "better bind" than "djbdns in rust"]

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by bluejekyll on Sunday July 10, 2016 @02:32PM (#12066600)
Not sure I totally understand what you mean by djbdns in Rust. I have taken some of djb's ideas, and plan on incorporating more, like randomized QID and Port to make cache poisoning more difficult (added recently). I've implemented this from RFC's only. It's not based on any other DNS server per sé.

My insiration started with yet another Bind exploit, and then grew from there. Unlike djbdns, I want this really to have as little manual operation as possible. After DNSCrypt I'm going to look at implementing a version of Raft on top of DNS updates/notifies, etc, to make server resiliency even better. DNSCrypt is next, mostly so thy I can use it for private comms between DNS nodes.

djb's is amazing, but what I'm looking for is to build something more flexible and more feature rich.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by bluejekyll on Sunday July 10, 2016 @06:13PM (#12067508)
Re: docs

The code is well documented ;)

Operational docs will come, but I haven't really had an opportunity to focus on those yet.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by vertex-four on Sunday July 10, 2016 @01:03PM (#12066228)
The book actually contains a bunch of information on implementation details and other things that aren't unsafe, but just don't have a good place in the regular book.

Additionally, if nobody writes unsafe Rust, who's going to maintain the abstractions that the rest of us rely on? :)

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by kibwen on Sunday July 10, 2016 @12:49PM (#12066185)
I wouldn't worry too much, the text of the book goes to great and colorful lengths to dissuade people from using these features frivolously. :)
Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by pjmlp on Sunday July 10, 2016 @01:36PM (#12066347)
Looking at Rust code with the eyes of a Wirth and Xerox Parc language fan, I still get the feeling that Rust requires too much uses of unsafe code versus Ada, Modula-3 and similar.

Specially for basic data structures like trees, graphs and double linked lists.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by Manishearth on Monday July 11, 2016 @12:26AM (#12068887)
Aside from what other people have pointed out about these being possible in safe Rust (just with a cost -- a cost you pay in the other languages anyway); this is not a useful yardstick for the amount of unsafe code you need to write.

These data structures are write once. You write them once, verify them, and use them as much as you want. The doubly linked list is in the stdlib. A tree without parent pointers is possible with zero cost in safe rust. A tree with parent pointers IIRC exists in a crate somewhere. So you will rarely be implementing these, just using these with safe code.

While we use datastructures to teach the "basics" of languages like C and C++, that doesn't mean that they are basic and any language where they are hard is crap. Rust makes them hard so that the rest of your code is easy to write. And since these are write-once, they have a minimal to zero impact on the amount of unsafe code in your codebase.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by Gankro on Sunday July 10, 2016 @01:56PM (#12066443)
I would be very interested in how these two languages enable safe data structuring. Do they have significant tradeoffs in terms of ergonomics, control, or perf?

Keep in mind basically all of Rust's libcollections uses unsafe code for perf and low-level representation reasons. They can all be made safe using the strategies every other safe language uses (and eating the associated downsides).

Also keep in mind Rust's data structures are basically language primitives in a lot of other languages!

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by pjmlp on Sunday July 10, 2016 @02:06PM (#12066483)
Modula-3 has GC. You can only make use of untraced pointers in unsafe modules.

So unless proven otherwise by the profiler, no need to make use of unsafe code.

Ada requires explicit use of unsafe code for memory deallocation, but it is not required when using RAII pools.

So unless a custom allocator is required, the standard pools can be used.

In Rust even a basic double linked list requires unsafe.

Then there are those that make use of unsafe to try to express code that cannot be proven by the type system, instead of plain memory corruption issues.

Thankfully the core team is against this trend.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by pcwalton on Sunday July 10, 2016 @02:28PM (#12066582)
> Modula-3 has GC. You can only make use of untraced pointers in unsafe modules.

> So unless proven otherwise by the profiler, no need to make use of unsafe code.

> Ada requires explicit use of unsafe code for memory deallocation, but it is not required when using RAII pools.

So, if you want to write hash tables like this without unsafe code, you're in luck: Rust lets you do it!

(As an aside, I keep seeing these fantastical claims about Ada that, when you dig into them, come with the exact same or more restrictive semantics than Rust. Ada is a good language, but it isn't magical, and the region/lifetime/uniqueness systems in Rust actually solve real problems that Ada doesn't have a solution to.)

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by nickpsecurity on Sunday July 10, 2016 @06:14PM (#12067510)
"As an aside, I keep seeing these fantastical claims about Ada that, when you dig into them, come with the exact same or more restrictive semantics than Rust."

Ive been looking forward to seeing such a point-by-point comparison of safety features between Rust, Ada, and SPARK. I gave you or someone else involved in Rust the Safe and Secure Ada 2012 book listing those features to help. Just link to it in a reply to me if and when you publish such analyses.

Btw, even if I agree on restrictive semantics, the thing you didnt mention is results where Ada and esp SPARK are immune by default to so many error classes. Rust definitely had it beat on dynamic safety and probably concurrency. However, unsafe programming in SPARK is still quite safer than Rust. :)

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by tatterdemalion on Sunday July 10, 2016 @02:15PM (#12066530)
> In Rust even a basic double linked list requires unsafe.

Well this just isn't true. You can write a doubly linked list using Rc and Weak pointers and no unsafe code. This is the same as saying you can write a doubly linked list using GC'd pointers.

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by geofft on Sunday July 10, 2016 @02:17PM (#12066538)
I didn't see this comment when replying above, but you can write a doubly-linked list in safe Rust with either of these approaches (GC, or stuffing everything into a single giant reference). The only reason a "basic" doubly-linked list in Rust is unsafe is that those aren't really reasonable options, and Rust wants to do better. I don't think it would improve Rust to give it mandatory GC (Go and Java are both great languages if you want that), or to require that all allocations are unsafe unless you use memory pools.
Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by geofft on Sunday July 10, 2016 @02:14PM (#12066518)
How do Ada and Modula-3 let you implement doubly-linked lists without unsafe code? I'd guess they don't enforce the constraint Rust does that mutable references must be unique, but my experience with both C and C++ is that you want this rule anyway for your code to have any chance of being correct.

In particular, removing a node from a doubly-linked list has three steps: repoint prev->next at next, repoint next->prev at prev, deallocate. If you can write this in safe code, it's equally possible to write a procedure that fixes prev->next and deallocates, but leaves next->prev dangling. What do Ada and Modula-3 do to prevent this?

(If you're willing to sacrifice efficiency, you can always use reference-counting or actual GC. Or you can just stuff each node in a dynamically-allocated vector, and have the prev and next pointers be indexes into that vector, instead; you'll have semantically-dangling numbers, but you won't have memory unsafety. Both of these approaches can be done in safe Rust with the normal standard library.)

Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by moosingin3space on Sunday July 10, 2016 @02:28PM (#12066584)
If I'm not mistaken, Modula-3 is garbage-collected, so doubly-linked lists are implemented with GCed pointers like in Go.
Re:The Rustonomicon: The Dark Arts of Advanced and Unsafe Ru... by nickpsecurity on Sunday July 10, 2016 @06:22PM (#12067547)
I can't remember if it bounds-checks the pointers or just arrays. However, SPARK lets you prove conformance of code to specs and absence of specific error conditions. Here's a linked list example:

http://www.spark-2014.org/entries/detail/spark-2014-rational...

There's also formal verification work on linked lists. If any have simple VC's, they could be encoded in containers like above as well. Tools exist to do similar stuff for C, too.