Nick Cameron: A very rough few thoughts on initialising data structures in Rust
|
|
Воскресенье, 06 Апреля 2014 г. 11:40
+ в цитатник
This is not a well thought out blog post. It ought to be a tweet, but it is too long.
Thoughts (in kind of random order):
- Rust has an ownership model based on hierarchies.
- It is easy to support tree-based data structures this way.
- Structures with cycles are much more annoying.
- The real pain is in initialisation.
- You can have a data structure with cycles using ref-counting or borrowed references as long as you 'delete' the whole data structure at once.
- Rust helps you do this with its type system.
- BUT there is no way to initialise such a data structure because of the way `mut` works. Specifically the requirement that `mut` variables are unique and have move/borrow semantics.
- Therefore you need unsafe blocks, which is a pain.
- Since it seems that you ought to be able to verify safe initialisation (I'm not sure how though).
- I think this is an area of the language which has not caused much pain because compilers (especially the frontend) and browsers are both very 'tree-heavy' programs. Long term we should find a solution to this, but it is certainly something that can be put off till post-1.0 (since it has an unsafe solution and I can' imagine a safe solution not being backwards-compatible).
- Kind of related - when using a data structure where all components have the same lifetime (i.e., the whole structure must be destroyed at once) you end up writing a huge number of really un-interesting lifetime parameters on functions and data which basically just get passed around. I would love to have a shorthand here - perhaps modules could have lifetime parameters? Not sure if that would help that much. More thought required in any case...
http://featherweightmusings.blogspot.com/2014/04/a-very-rough-few-thoughts-on.html
-
Запись понравилась
-
0
Процитировали
-
0
Сохранили
-