By default, a pinned map guard (LocalGuard) is not Send because it is tied to the current thread. This prevents you from holding a reference across an .await point in work-stealing schedulers (like Tokio).
To use the map across .await points, use pin_owned() to create an OwnedGuard.
Note: OwnedGuard is more expensive to create than a regular guard. If you only need a value briefly, it is more efficient to drop the guard, perform the async operation, and then re-pin the map to fetch the value again.
use std::sync::Arc;
use papaya::HashMap;
async fn run(map: Arc<HashMap<i32, String>>) {
tokio::spawn(async move {
// Use pin_owned() to allow the guard to be Send
let map = map.pin_owned();
// The reference can now be held across this .await
let value = map.get(&37);
tokio::fs::write("db.txt", format!("{value:?}")).await;
println!("{value:?}");
});
}