[blog post] Do we really need language support for self-references?

I just published a post on self-references in safe Rust. I hope it may interest you.

Should I have posted it on internal instead?

1 Like

It is a nice idea for some cases, but I think this wouldn't help when you are trying to store something like object::Object<'a> next to the data it borrows from. The closure would have to construct the Object every time which is somewhat expensive as it has to parse the input bytes.

What do you mean? The index can totally be a reference itself, and the object pointed by the self reference is also going to be a reference. What needs to be constructed every time?

object::File contains references to the input data slice. It is impossible to rewrite it to use numerical indices without having to pass the input data to every method.

struct OwningFile {
    data: Vec<u8>,
    parsed_file: object::File<'self>,

OwningFile {
     data: large_blob,
     parsed_file: object::File::parse(&self.data).unwrap(),

Now I get it. My solution works only when creating the reference is cheap (like when indexing into a Vec a HashMap or a Map), but not when it’s expensive (like parsing a file).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.