Skip to main content

HostTransaction

Trait HostTransaction 

pub trait HostTransaction: ServerHost {
    // Required method
    fn transaction<T, E>(
        &mut self,
        f: impl FnOnce(&mut Self, &Transaction) -> Result<T, E>,
    ) -> Result<T, E>;
}
Expand description

Transactions are available only on the logical server and preserve the receiver type.

use mod_api::{BlockEntityError, BlockEntityRef, HostTransaction, ServerHost};
fn write(host: &mut dyn ServerHost, block: BlockEntityRef) {
    let _: Result<(), BlockEntityError> = host.transaction(|host, _tx| {
        block.set_changed(host)?;
        Ok(())
    });
}
ⓘ
use mod_api::{BlockEntityError, BlockEntityRef, Host, HostTransaction};
fn write(host: &mut dyn Host, block: BlockEntityRef) {
    let _: Result<(), BlockEntityError> = host.transaction(|host, _tx| {
        block.set_changed(host)?;
        Ok(())
    });
}
ⓘ
use mod_api::{ClientHost, HostTransaction};
fn client(host: &mut dyn ClientHost) {
    let _: Result<(), ()> = host.transaction(|_, _| Ok(()));
}

Required Methods§

fn transaction<T, E>( &mut self, f: impl FnOnce(&mut Self, &Transaction) -> Result<T, E>, ) -> Result<T, E>

Runs f in a transaction, nested in the open one if any (Transaction.openRoot and open): Ok commits it (commit), into the transaction around it or, at the outermost, for good, with the world told of each change once; Err aborts it (close without commit), putting every storage it wrote back as it was. A panic unwinding out of f aborts this transaction as it passes, so whoever catches it sees nothing f changed, and the transaction around it stays open. A transaction f opened itself and left open is aborted first, when this one closes.

f gets the Transaction to write in, never its TransactionSeal: only this guard ends the transaction.

Covered: block entities (item slots, saved compound, custom name and attachments), cauldrons, players’ inventories and cursors, entities’ inventories (ItemPlace::EntitySlot) and item entities’ stacks.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl<H> HostTransaction for H
where H: ServerHost + ?Sized,