Indirect ownership, shallow borrow and self-referential data structures in Rust
yoyo-code.com
yoyo-code.com
In a language like Verona, ParsedFile could have a contents Vec allocated in the same region as the ParsedFile, and a words Vec containing references into the contents Vec. Reference-typed fields by default are constrained to the same region as the parent object, so the above example works straightforwardly.
The downside of this is that, in the general case, you have to garbage collect the regions. You can partition the heap using regions but all the same problems apply to the partitioned heap.
NB: Rust uses the term "regions" somewhat differently than Cyclone (https://homes.cs.washington.edu/~djg/papers/cyclone-cuj.pdf) or Verona. In the latter languages, regions are partitioned subgraphs of the heap. Rust regions, as far as I can tell, are slightly different.
There is a crate that uses async functions to accomplish exactly this. https://crates.io/crates/escher
Disclaimer, I've never actually tried using it. I've used owning_ref or ouroboros depending on what I need.
struct ParsedFile {
contents: Vec<u8>,
words: Vec<(usize, usize)>
}
>This works well, and you can make it ergonomic by wrapping the index pair in a new typeYou should probably just use the std::ops::Range type for this. It's what's created by the `..` syntax
Also: it's weird and really unfair imho that the GP is downvoted. While it is true that some folks have a different opinion, it's extremely subjective, and downvoting them for a slightly minority opinion doesn't seem right to me. I upvoted it, personally.