pub trait Entity: Sized {
type Key: PrimaryKey;
const NAME: &'static str;
const PATH: &'static str;
const SCHEMA: &'static EntitySchema;
// Required methods
fn key(&self) -> Self::Key;
fn validate(&self) -> Result<(), Error>;
fn encode(&self) -> Result<Vec<u8>, Error>;
fn decode(bytes: &[u8]) -> Result<Self, Error>;
}Expand description
Codec boundary emitted by #[entity]; hand-written implementations are unsupported.
Typed references use Rust resolution; an unresolved name fails compilation:
use ic_db::entity;
#[entity(pk(field="id"), index(field="target"), fields(
field(name="id",value(item(prim="Nat64"))),
field(name="target",value(item(prim="Nat64")),references(entity="Missing"))))]
struct Example {}Lookups require the declared key type; integers cannot stand in for ULIDs:
use ic_db::{entity, Database};
#[entity(pk(field="id"), fields(field(name="id",value(item(prim="Ulid")))))]
struct Asset {}
fn wrong_key(db: &Database) { let _ = db.get::<Asset>(7_u64); }Integer key widths are exact, without implicit widening:
use ic_db::{entity, Database};
#[entity(pk(field="id"), fields(field(name="id",value(item(prim="Nat8")))))]
struct Small {}
fn wrong_width(db: &Database) { let _ = db.get::<Small>(7_u64); }Required Associated Constants§
Sourceconst PATH: &'static str
const PATH: &'static str
Rust-resolved declaration path for host schema assembly, not durable identity.
Sourceconst SCHEMA: &'static EntitySchema
const SCHEMA: &'static EntitySchema
This executable’s compiled entity contract.
Required Associated Types§
Sourcetype Key: PrimaryKey
type Key: PrimaryKey
Exact supplied primary-key type, selected by the declared field.
Required Methods§
Sourcefn key(&self) -> Self::Key
fn key(&self) -> Self::Key
Read the application’s primary key without encoding or inspecting indexes.
Sourcefn validate(&self) -> Result<(), Error>
fn validate(&self) -> Result<(), Error>
Validate declared field bounds before encoding; no persistent effects.
§Errors
Refuses values outside the authored field contract.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".