Skip to main content

Topicable

Trait Topicable 

Source
pub trait Topicable:
    Serialize
    + DeserializeOwned
    + Clone
    + Debug {
    type Key: Serialize + DeserializeOwned + Clone + Debug + PartialEq + Hash + CdrBounds;

    const FORCE_MD5_KEYHASH: bool = false;

    // Required methods
    fn from_key(key: &Self::Key) -> Self;
    fn as_key(&self) -> Self::Key;

    // Provided method
    fn dds_type_name() -> impl AsRef<str> { ... }
}
Expand description

A type that can be used as a DDS topic payload.

Implement this trait to register a type as a DDS topic payload. The derive macro Topicable handles the common case; implement manually when you need control over the key type or type name.

§Keys

Every Topicable type has an associated Key type that uniquely identifies an instance. For unkeyed topics, use any zero-sized type as the key (() being the straightforward choice), and all samples will be treated as belonging to a single instance.

If using the derive macro, a hidden type gets generated which contains just the fields marked as a key fields. To access this type see the Key type alias.

§Examples

#[derive(
    cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Default, Clone, Debug,
)]
struct Temperature {
    #[dds(key)]
    sensor_id: u32,
    value: f32,
}

Manual implementation:

#[derive(serde::Serialize, serde::Deserialize, Clone, Default, Debug)]
struct Temperature {
    sensor_id: u32,
    value: f32,
}

impl cyclonedds::Topicable for Temperature {
    type Key = u32;

    fn from_key(key: &u32) -> Self {
        Self {
            sensor_id: *key,
            value: 0.0,
        }
    }

    fn as_key(&self) -> u32 {
        self.sensor_id
    }
}

Provided Associated Constants§

Source

const FORCE_MD5_KEYHASH: bool = false

Forces MD5 keyhash generation regardless of key size.

By default, the big-endian CDR serialization of the key is used as the keyhash when the maximum serialized key size is 16 bytes or fewer, and MD5 otherwise. Set this to true to force MD5 unconditionally.

Required Associated Types§

Source

type Key: Serialize + DeserializeOwned + Clone + Debug + PartialEq + Hash + CdrBounds

The key type that uniquely identifies an instance of this topic.

For unkeyed topics, use any zero-sized type as the key (() being the straightforward choice), and all samples will be treated as belonging to a single instance.

The key type must implement CdrBounds to provide serialization size information required by DDS when computing keyhashes.

Required Methods§

Source

fn from_key(key: &Self::Key) -> Self

Constructs a default instance of Self from a key.

Used to materialize a full sample from a key-only notification. Fields not part of the key should be set to their default values.

Source

fn as_key(&self) -> Self::Key

Extracts the key from this instance.

Provided Methods§

Source

fn dds_type_name() -> impl AsRef<str>

Returns the DDS type name for this type.

Defaults to the Rust type name as it would appear within the crate. Override when interoperating with an existing system whose IDL type names differ from the Rust type names.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§