Skip to main content

RecordKey

Trait RecordKey 

Source
pub trait RecordKey:
    Clone
    + Eq
    + Hash
    + Borrow<str>
    + Send
    + Sync
    + 'static {
    // Required method
    fn as_str(&self) -> &'static str;

    // Provided method
    fn link_address(&self) -> Option<&str> { ... }
}
Expand description

Trait for record key types

Enables compile-time checked enum keys for embedded while preserving String flexibility for edge/cloud deployments.

The Borrow<str> bound is required for O(1) HashMap lookups by string in the remote access layer (e.g., hashmap.get("record.name")).

§Implementing RecordKey

The easiest way is to use the derive macro (illustrative, not compiled: the macro lives in aimdb-derive, behind the optional derive feature):

ⓘ
#[derive(RecordKey, Clone, Copy, PartialEq, Eq)]
pub enum AppKey {
    #[key = "temp.indoor"]
    TempIndoor,
    #[key = "temp.outdoor"]
    TempOutdoor,
}

With connector metadata (MQTT topics, KNX addresses):

ⓘ
#[derive(RecordKey, Clone, Copy, PartialEq, Eq)]
pub enum SensorKey {
    #[key = "temp.indoor"]
    #[link_address = "mqtt://sensors/temp/indoor"]
    TempIndoor,

    #[key = "temp.outdoor"]
    #[link_address = "knx://9/1/0"]
    TempOutdoor,
}

For manual implementation:

use aimdb_core::RecordKey;
use core::borrow::Borrow;

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum MyKey {
    Temperature,
    Humidity,
}

impl RecordKey for MyKey {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Temperature => "sensor.temp",
            Self::Humidity => "sensor.humid",
        }
    }
}

impl Borrow<str> for MyKey {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

§Hash and Borrow Contract

The RecordKey trait requires Hash because keys are stored in HashMaps. Per Rust’s Borrow trait contract, hash(key) == hash(key.borrow())—meaning your Hash implementation must hash the same value as as_str() returns.

Using the derive macro: #[derive(RecordKey)] automatically generates both Hash and Borrow<str> implementations that satisfy this contract. Do not also derive Hash manually—it would conflict.

Manual implementation: Implement Hash by hashing self.as_str():

use core::hash::{Hash, Hasher};
impl Hash for MyKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

Required Methods§

Source

fn as_str(&self) -> &'static str

String representation for connectors, logging, serialization, remote access

Provided Methods§

Connector address for this key

Returns the URL/address to use with connectors (MQTT topics, KNX addresses, etc.). Use with .link_to() for outbound or .link_from() for inbound connections.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl RecordKey for &'static str

Source§

fn as_str(&self) -> &'static str

Implementors§