Skip to main content

rebind_entry

Function rebind_entry 

Source
pub async fn rebind_entry<K, V, Out, Fut, E>(
    map: &mut BTreeMap<K, V>,
    key: &K,
    op: impl FnOnce(V) -> Fut,
) -> Result<Out::Rest, E>
where K: Ord, Out: Threaded<V>, Fut: Future<Output = Result<Out, E>>,
Expand description

Runs an async operation that consumes the value at key in map, then restores the returned value under the original key and yields any extra outputs (see Threaded).

Use this to call a method that consumes self on a value stored in a BTreeMap. For a value stored in an Option, use rebind.

On error, the entry stays absent.

§Cancellation

The entry is removed when this future is first polled. Dropping the future while the operation is pending leaves the entry absent.

§Examples

Append a record to epoch 7’s journal, whose append method returns (Self, position):

use commonware_utils::futures::rebind_entry;
use std::collections::BTreeMap;

let mut journals = BTreeMap::from([(7, Journal::default())]);
let position = rebind_entry(&mut journals, &7, |journal| journal.append("vote"))
    .await
    .expect("append failed");
assert_eq!(position, 0);
assert!(journals.contains_key(&7));

§Panics

Panics when key is absent from map.