Skip to main content

Module message

Module message 

Source
Expand description

Typed messages over the plain-data payload.

Payloads are Values so that Lua, C and JSX share them. A Rust app gets an exhaustive match back with #[derive(Message)] (from kui-derive, re-exported by kui-native and, behind the derive feature, by this crate): it turns an enum into a {kind, ...fields} map and back, so on_click(Msg::Save) builds the payload and UiEvent::message::<Msg>() reads it.

ⓘ
// Needs the `derive` feature (on by default in kui-native).
#[derive(kui::Message)]
enum Msg {
    Save,
    Rename { to: String },
}

fn update(ev: &kui::UiEvent) {
    match ev.message::<Msg>() {
        Some(Msg::Save) => { /* ... */ }
        Some(Msg::Rename { to }) => { /* ... */ }
        None => {}
    }
}

The derive is built from MessageField, the conversion each field’s type has, and MessageError, what a payload that is not one of the enum’s says. Both are plain enough to implement by hand for a type the derive does not cover.

Enums§

MessageError
Why a payload is not the message it was read as.

Traits§

MessageField
A type a message field can hold: to a Value and back. Implemented for the numbers, bool, String, Value itself, Option (absent or null is None) and Vec; #[derive(Message)] implements it for the type it derives, so one message can carry another.

Functions§

field
Field key of the map v, as T: what the derive reads each field with. An absent key reads as null, so an Option field may be left out.
kind_of
The kind of a map payload, for the derive’s match.