Docs›qos-sdk›state_set
qos-sdk Reference

qos_sdk::state::set

Writes a byte array to a specific key in the Sled state store. This is the primary way a WASM module causes a persistent side-effect.

#Signature

pub fn set(key: &str, value: &[u8]) -> Result<(), ()>

#Usage

set() is atomic. If multiple invocations of a module call set() simultaneously, the host guarantees sequential ordering without data corruption.

src/lib.rs
use qos_sdk::state;

#[no_mangle]
pub extern "C" fn run() -> i32 {
    let mut count: u8 = 1;
    
    if let Some(val) = state::get("counter") {
        if val.len() == 1 {
            count = val[0] + 1;
        }
    }
    
    // Atomically save the new count
    state::set("counter", &[count]).unwrap();
    
    count as i32
}

⚠️ WARNING

Every call to set() automatically triggers a STATE_MUTATION WebSocket event to the Admin dashboard. You should bundle updates where possible to avoid spamming the WebSocket channel.