4 ms·
So this article mentioned that there are other types of smart pointers, which are in the standard library instead of part of the language. How do those work? I
by lmkg 14y ago
So this article mentioned that there are other types of smart pointers, which are in the standard library instead of part of the language. How do those work? I would imagine that they're generics, like how C++ smart pointers are templates, but I can't figure out how you would go about writing something like that if your existing primitives are also smart pointers. Does Rust also support C-style pointers, and if so, does it allow you to restrict their usage?
- pcwalton 14y agoYes, they're generics implemented with C-style pointers under the hood. The use of raw C-style pointers is restricted to designated "unsafe" blocks.
- haberman 14y agoAre you saying that these smart pointers (like the return of the allocation operator) are implemented in Rust itself?! If so that is extremely cool, but then how do they implement the semantics that sure references can't outlive their referent? It seems like the smart pointer would have to hook into the program analysis.
- pcwalton 14y agoThe built-in pointers with special names call out to functions written in Rust to perform the actual allocation (specifically, the functions annotated with lang="malloc"/"free"/"exchange_malloc"/"exchange_free"), so you can customize their behavior in that way. Right now we just call through to the C allocator, but there is a WIP GC written in Rust that does some extra bookkeeping in the allocation function and performs the mark and sweep periodically. But the compiler does know about the built-in smart pointers' semantics in order to perform the reference analysis. For custom smart pointers, you usually have to delimit the scope of a reference yourself with a block. For example: let p = ARC(1024); // atomically reference counted do p.ref |r: &int| { ... do stuff with r ... // The compiler ensures that r cannot leave this // scope... } So custom smart pointers cause some rightward drift. Figuring out how to remove this rightward drift is something I would like to solve in the future, but it'll require some more thought. In any case, it's a case of code being a bit uglier, not a case of loss of flexibility, so building a couple of smart pointers into the language seemed like an acceptable worse-is-better solution for Rust 1.0.
- lobster_johnson 14y agoAre you sure that ARC is an appropriate name? Apple already uses the acronym for Automatic Reference Counting. Correct me if I'm wrong, but Rust's reference counting does not look automatic, since you must wrap it in "do" blocks. It's a bit misleading to reuse a fairly well-esablished acronym for something that is nearly, but not quite, the same.
- asb 14y agoYeah, the rust devs have said a number of times they're going to change the name due to this confusion.
- fzzzy 14y agoThe rust ARC stands for Atomic Reference Counting, not Automatic Reference Counting. An unfortunate namespace collision.
- lobster_johnson 14y agoYes. That was my point (atomic vs. automatic).
- pcwalton 14y agoYeah, everyone gets confused here because of Obj-C. We should change the name.