dashmap icon indicating copy to clipboard operation
dashmap copied to clipboard

`await`-protected API

Open sukhmel opened this issue 7 months ago • 0 comments

I happened to create deadlocks accidentally by holding a reference into the DashMap across awaits, and came to think that maybe it might be possible to make an API that disallowed such use even by accident?

It looks like one way to do that is to bar all the work performed on the returned value behind a function, something like this:

pub fn process<Q, R, F: FnOnce(&T) -> R>(&'a self, key: &Q, f: F) -> R {
    f(self.get(key))
}

pub fn process_mut<Q, R, F: FnOnce(&mut T) -> R>(&'a self, key: &Q, f: F) -> R {
    f(self.get_mut(key)) 
}

Is there a better way to do that? Maybe there's some way to opt-in to non-awaitable code without changes to DashMap by wrapping return value into something clever?

Is there a need for such API?

sukhmel avatar May 15 '25 09:05 sukhmel