Skip to main content

rebind

Function rebind 

Source
pub async fn rebind<T, Out, Fut, E>(
    slot: &mut Option<T>,
    op: impl FnOnce(T) -> Fut,
) -> Result<Out::Rest, E>
where Out: Threaded<T>, Fut: Future<Output = Result<Out, E>>,
Expand description

Runs an async operation that consumes the value in slot, then restores the returned value and yields any extra outputs (see Threaded).

Use this to call a method that consumes self on a value stored in an Option.

On error, slot stays empty.

§Cancellation

The value is removed when this future is first polled. Dropping the future while the operation is pending leaves slot empty.

§Examples

Append a record to a journal whose append method returns (Self, offset, length):

use commonware_utils::futures::rebind;

let mut journal = Some(Journal::default());
let (offset, len) = rebind(&mut journal, |journal| journal.append(b"vote"))
    .await
    .expect("append failed");
assert_eq!((offset, len), (0, 4));
assert!(journal.is_some());

§Panics

Panics when slot is empty.