pub struct StringKey(/* private fields */);Expand description
Default string-based record key
Supports both static (zero-cost) and interned (leaked) names.
Use string literals for the common case; they auto-convert via From.
§Memory Model
Dynamic keys are “interned” by leaking memory into 'static lifetime.
This is optimal for AimDB’s use case where keys are:
- Registered once at startup
- Never deallocated during runtime
- Frequently cloned (now just pointer copy)
The tradeoff: memory for dynamic keys is never freed. This is acceptable because typical deployments have <100 keys totaling <4KB.
§Naming Convention
Recommended format: <namespace>.<category>.<instance>
Examples:
sensors.temperature.outdoorsensors.temperature.indoormesh.weather.sf-bayconfig.app.settingstenant.acme.sensors.temp
§Examples
use aimdb_core::record_id::StringKey;
// Static (preferred) - zero allocation
let key: StringKey = "sensors.temperature".into();
// Dynamic - for runtime-generated names
let key = StringKey::intern(format!("tenant.{}.sensors", "acme"));Implementations§
Source§impl StringKey
impl StringKey
Sourcepub const fn new(s: &'static str) -> Self
pub const fn new(s: &'static str) -> Self
Create from a static string literal
This is a const fn, usable in const contexts.
Sourcepub fn intern(s: impl AsRef<str>) -> Self
pub fn intern(s: impl AsRef<str>) -> Self
Create from a runtime-generated string
Use this for dynamic names (multi-tenant, config-driven, etc.).
§Memory
The string is leaked into 'static lifetime. This is intentional:
- Keys are registered once at startup
- Enables O(1) Copy/Clone
- Typical overhead: <4KB for 100 keys
§Panics (debug builds only)
In debug builds with std feature, panics if more than 1000 keys are
interned. This catches accidental misuse (e.g., creating keys in a loop).
Production builds have no limit.
Sourcepub fn from_dynamic(s: &str) -> Self
pub fn from_dynamic(s: &str) -> Self
Create from a runtime string (alias for intern)
The string is leaked into 'static lifetime for O(1) cloning.
Sourcepub fn is_interned(&self) -> bool
pub fn is_interned(&self) -> bool
Returns true if this is an interned (runtime) key
Trait Implementations§
Source§impl Borrow<str> for StringKey
Enable O(1) HashMap lookup by &str
impl Borrow<str> for StringKey
Enable O(1) HashMap lookup by &str
This allows hashmap.get("string_literal") without allocating a StringKey.
Source§impl<'de> Deserialize<'de> for StringKey
Available on crate feature std only.
impl<'de> Deserialize<'de> for StringKey
std only.Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl From<String> for StringKey
Available on crate feature std only.Ergonomic conversion from owned String (std)
impl From<String> for StringKey
std only.Ergonomic conversion from owned String (std)