Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I have a hard time seeing how you could use self references without a combination of raw pointers, pins/projects, and unsafe code. The tediousness of doing so is pretty much a no-go for any sane developer.

You just do it. I can't recall a good example at the moment, so I've just thrown together a load of Cells: the thing I was doing when I learnt this technique didn't have any Cells in it.

https://play.rust-lang.org/?version=nightly&edition=2021&gis...

  #[derive(Debug)]
  struct SlotMachine<'a> {
    slots: Vec<Cell<i32>>,
    current: Cell<Option<&'a Cell<i32>>>,
  }

  fn main() {
    let machine = SlotMachine {
        slots: vec![Cell::new(0); 5],
        current: Cell::new(None),
    };
    machine.current.set(Some(&machine.slots[4]));
    machine.slots[4].set(12);
    println!("{:#?}", machine);
  }
> Yes, and most of us don't really want to use nightly in production.

Strictly speaking, this is only needed if you want to use Rust's standard library types with custom allocators. You've been able to have per-struct allocators for your own types since long before I learnt the language.

> Well I mean overall any way to allow a somewhat eased setup for storing and retrieving objects to/from shared memory, across processes that do not map said memory at the same location.

Can't you just store offsets into the memory region, and PhantomData references, then unsafely index the mmap'd region when you need an actual reference? Seems like the same thing you'd do in C, except the function you abstract that with can be a method instead. (Unless I'm still misunderstanding.)



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: