29 lines
812 B
Rust
29 lines
812 B
Rust
|
|
use core::sync::atomic::{AtomicU64, Ordering};
|
||
|
|
|
||
|
|
mod bindings {
|
||
|
|
wit_bindgen::generate!({
|
||
|
|
path: "wit",
|
||
|
|
world: "counter-component",
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
static COUNTER: AtomicU64 = AtomicU64::new(0);
|
||
|
|
|
||
|
|
struct Counter;
|
||
|
|
|
||
|
|
impl bindings::Guest for Counter {
|
||
|
|
fn init(_config: Vec<u8>) -> Result<(), bindings::ServiceError> {
|
||
|
|
// Calls are serialized by the host Actor, so this component intentionally
|
||
|
|
// uses one mutable global to make resident memory observable in tests.
|
||
|
|
COUNTER.store(0, Ordering::Relaxed);
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
fn invoke(_input: Vec<u8>) -> Result<Vec<u8>, bindings::ServiceError> {
|
||
|
|
let next = COUNTER.fetch_add(1, Ordering::Relaxed).wrapping_add(1);
|
||
|
|
Ok(next.to_le_bytes().to_vec())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bindings::export!(Counter with_types_in bindings);
|