Skip to main content

HasAttribute

Trait HasAttribute 

Source
pub trait HasAttribute<A: AttributeDefinition> {
    type Id<'id>;
    type Value: IntoTypedAttributeValue<A::Type>;

    // Required methods
    fn attribute_id(&self) -> Self::Id<'_>;
    fn attribute_value(id: Self::Id<'_>) -> Self::Value;

    // Provided method
    fn attribute(&self) -> Self::Value { ... }
}
Expand description

Links an item type to a dynamic DynamoDB attribute.

Implementing this trait for a pair (Item, Attr) declares that Item contributes the attribute Attr to its DynamoDB representation, where the attribute value is derived from the item at runtime.

The trait has two key methods:

  • attribute_id — extracts an “Id” value from &self (e.g. a &str field).
  • attribute_value — converts the Id into a Rust value of type Self::Value (e.g. produces "USER#{id}" as a String). This is not an AttributeValue yet — the library converts it downstream using IntoTypedAttributeValue.
  • attribute — convenience method that calls both in sequence to obtain the Self::Value from &self.

Implementations are generated by dynamodb_item! and has_attributes!. Every type that implements HasConstAttribute<A> automatically gets a blanket HasAttribute<A> implementation.

§Examples

use dynamodb_facade::HasAttribute;

let user = sample_user();

// Retrieve the DynamoDB PK value for this user.
let pk_value = <User as HasAttribute<PK>>::attribute(&user);
assert_eq!(pk_value, "USER#user-1");

Required Associated Types§

Source

type Id<'id>

The identifier extracted from &self, passed to attribute_value.

For constant attributes this is NoId. For dynamic attributes it is typically a borrowed field (e.g. &str).

Source

type Value: IntoTypedAttributeValue<A::Type>

A Rust type convertible to the DynamoDB attribute value for this attribute.

Bounded by IntoTypedAttributeValue<A::Type>, which guarantees that when this Rust value is converted to an AttributeValue it will produce the correct DynamoDB scalar type (S for StringAttribute, N for NumberAttribute, B for BinaryAttribute).

Required Methods§

Source

fn attribute_id(&self) -> Self::Id<'_>

Extracts the attribute ID from this item.

Source

fn attribute_value(id: Self::Id<'_>) -> Self::Value

Converts an attribute ID into a Rust value of type Self::Value which can then be converted into the correct AttributeValue at serialization using the via IntoTypedAttributeValue.

Provided Methods§

Source

fn attribute(&self) -> Self::Value

Convenience method: calls attribute_id then attribute_value, returning a Rust value of type Self::Value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§