multiversx_sc_modules::ongoing_operation

Trait OngoingOperationModule

Source
pub trait OngoingOperationModule: ContractBase + Sized {
    // Required method
    fn current_ongoing_operation(
        &self,
    ) -> SingleValueMapper<Self::Api, ManagedBuffer<Self::Api>>;

    // Provided methods
    fn run_while_it_has_gas<Process>(
        &self,
        min_gas_to_save_progress: u64,
        process: Process,
    ) -> OperationCompletionStatus
       where Process: FnMut() -> LoopOp { ... }
    fn can_continue_operation(
        &self,
        operation_cost: u64,
        min_gas_to_save_progress: u64,
    ) -> bool { ... }
    fn load_operation<T: TopDecode + Default>(&self) -> T { ... }
    fn save_progress<T: TopEncode>(&self, op: &T) { ... }
    fn clear_operation(&self) { ... }
}

Required Methods§

Provided Methods§

Source

fn run_while_it_has_gas<Process>( &self, min_gas_to_save_progress: u64, process: Process, ) -> OperationCompletionStatus
where Process: FnMut() -> LoopOp,

Run the given lambda function until it’s either completed or it runs out of gas. min_gas_to_save_progress should be a reasonable value to save gas. This can vary a lot based on the given ongoing operation data structures.

§Usage example: Counting to 100
fn count_to_100(&self) -> OperationCompletionStatus {
    let mut current_number = self.load_operation::<usize>();
    let run_result = self.run_while_it_has_gas(DEFAULT_MIN_GAS_TO_SAVE_PROGRESS, || {
        if current_number == 100 {
            return STOP_OP;
        }

        current_number += 1;
         
        CONTINUE_OP
    });
     
    if run_result == OperationCompletionStatus::InterruptedBeforeOutOfGas {
        self.save_progress(&current_number);
    }

    run_result
}
Source

fn can_continue_operation( &self, operation_cost: u64, min_gas_to_save_progress: u64, ) -> bool

Source

fn load_operation<T: TopDecode + Default>(&self) -> T

Load the current ongoing operation. Will return the default value if no operation is saved.

Source

fn save_progress<T: TopEncode>(&self, op: &T)

Save progress for the current operation. The given value can be any serializable type.

Source

fn clear_operation(&self)

Clears the currently stored operation. This is for internal use.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§