0

I'm using crossbeam-epoch to write a lock-free data structure in rust. crossbeam-epoch uses a guard to load the heap-allocated atomic pointer. One example of the data structure's interface is:

fn get(&self, index: IndexType, guard: &'guard Guard) -> Option<&'guard Value>

This method requires a guard which has the same lifetime with the returned value's reference.

Now, I want to wrap this method without providing a Guard. For example:

struct ReturnType<'guard, Value> {
    guard: Guard,
    value: Option<&'guard Value>
}

impl<'guard, Value> MyDataStructure<Value> {
    fn my_get(&self, index: IndexType) -> ReturnType<'guard, Value> {
        let guard = pin();
        let value = self.get(index, &guard);
        ReturnType {
            guard: guard,
            value: value
        }
    }
}

But the compiler won't allow me to do this.

My question is how to implement the method my_get?

mmch
  • 15
  • 5
  • I will try to fix your problem. But first, is the pointer shared? If it is I think `Arc` would be a great option and if it is not then `Box` is – sb27 Oct 30 '20 at 13:30
  • @sb27 Thanks for your help. The pointer is shared, but I think it doesn't matter whether we use `Arc` or not. Because `crossbeam_epoch` is responsible for destroying it if it is not used anymore. – mmch Oct 30 '20 at 13:53
  • But `Value` lives at least as long as `self`, doesn't it? – sb27 Oct 30 '20 at 14:14

1 Answers1

1

This question needs some improvement. You should always add a minimal reproducable example and you haven't shown what the compiler error was either.

Anyways, in your code, you just forget to specify what the lifetime should be linked to. If the guard at least lives as long as self, then you should declare your method with:

fn my_get<'guard>(&'guard self, index: IndexType) -> ReturnType<'guard, Value> {

&'guard self tells the compiler to link 'guard to self. If the guard should live longer than the struct then you have to move the Value or if you want to share the reference use Arc<Value> (Documentation)

sb27
  • 382
  • 4
  • 14