#[derive(SensorData)]
{
// Attributes available to this derive:
#[sensor]
}
derive only.Expand description
Derive SensorData, binding a type to a sensor ID.
Requires the derive feature. See the macro’s own documentation for the
#[sensor(id = ..., version = ...)] attribute.
Derive [sensor_db::SensorData], binding a type to a sensor ID.
§Example
use sensor_db::SensorData;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, SensorData)]
#[sensor(id = 0x01, version = 1)]
#[repr(C)]
struct Temperature {
celsius: f32,
}§Attributes
| Key | Required | Meaning |
|---|---|---|
id | yes | The sensor ID. Any const expression of type u16. |
version | no (default 0) | The schema version. Any const expression of type u8. |
crate | no (default ::sensor_db) | Path to the sensor_db crate, if renamed. |
id and version take arbitrary constant expressions, not just literals,
so a central ID module — the practice that actually prevents collisions —
works directly:
mod ids {
pub const TEMPERATURE: u16 = 0x01;
}
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, SensorData)]
#[sensor(id = ids::TEMPERATURE, version = 1)]
#[repr(C)]
struct Temperature { celsius: f32 }§What this macro does not do
It emits the SensorData impl and nothing else. In particular it does not
emit the zerocopy derives or a repr attribute, because a derive macro
cannot add attributes to the item it is applied to, and because zerocopy’s
derives expand to absolute ::zerocopy:: paths — so zerocopy has to be a
direct dependency of your crate either way. Injecting its derives would make
that requirement invisible and its absence inscrutable.
Padding is already a compile error from zerocopy’s IntoBytes derive,
which reports the exact number of wasted bytes. This macro does not
second-guess it.