pub struct DataWriter<D: Keyed + Serialize, SA: SerializerAdapter<D> = CDRSerializerAdapter<D>> { /* private fields */ }
Expand description
DDS DataWriter for keyed topics
Examples
use serde::{Serialize, Deserialize};
use rustdds::dds::DomainParticipant;
use rustdds::dds::qos::QosPolicyBuilder;
use rustdds::dds::data_types::TopicKind;
use rustdds::with_key::DataWriter;
use rustdds::dds::traits::Keyed;
use rustdds::serialization::CDRSerializerAdapter;
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None);
Implementations
sourceimpl<D, SA> DataWriter<D, SA>where
D: Keyed + Serialize,
<D as Keyed>::K: Key,
SA: SerializerAdapter<D>,
impl<D, SA> DataWriter<D, SA>where
D: Keyed + Serialize,
<D as Keyed>::K: Key,
SA: SerializerAdapter<D>,
sourcepub fn refresh_manual_liveliness(&self)
pub fn refresh_manual_liveliness(&self)
Manually refreshes liveliness if QoS allows it
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None).unwrap();
data_writer.refresh_manual_liveliness();
sourcepub fn write(&self, data: D, source_timestamp: Option<Timestamp>) -> Result<()>
pub fn write(&self, data: D, source_timestamp: Option<Timestamp>) -> Result<()>
Writes single data instance to a topic.
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None).unwrap();
let some_data = SomeType { a: 1 };
data_writer.write(some_data, None).unwrap();
pub fn write_with_options(
&self,
data: D,
write_options: WriteOptions
) -> Result<SampleIdentity>
sourcepub fn wait_for_acknowledgments(&self, max_wait: Duration) -> Result<bool>
pub fn wait_for_acknowledgments(&self, max_wait: Duration) -> Result<bool>
This operation blocks the calling thread until either all data written by
the reliable DataWriter entities is acknowledged by all
matched reliable DataReader entities, or else the duration specified by
the max_wait
parameter elapses, whichever happens first.
See DDS Spec 1.4 Section 2.2.2.4.1.12 wait_for_acknowledgments.
If this DataWriter is not set to Realiable, or there are no matched DataReaders with Realibale QoS, the call succeeds imediately.
Return values
Ok(true)
- all acknowledgedOk(false)
- timed out waiting for acknowledgmentsErr(_)
- something went wrong
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None).unwrap();
let some_data = SomeType { a: 1 };
data_writer.write(some_data, None).unwrap();
data_writer.wait_for_acknowledgments(std::time::Duration::from_millis(100));
sourcepub fn topic(&self) -> &Topic
pub fn topic(&self) -> &Topic
Topic assigned to this DataWriter
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None).unwrap();
assert_eq!(data_writer.topic(), &topic);
sourcepub fn publisher(&self) -> &Publisher
pub fn publisher(&self) -> &Publisher
Publisher assigned to this DataWriter
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None).unwrap();
assert_eq!(data_writer.publisher(), &publisher);
sourcepub fn assert_liveliness(&self) -> Result<()>
pub fn assert_liveliness(&self) -> Result<()>
Manually asserts liveliness (use this instead of refresh) according to QoS
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None).unwrap();
data_writer.assert_liveliness().unwrap();
sourcepub fn get_matched_subscriptions(&self) -> Vec<SubscriptionBuiltinTopicData>
pub fn get_matched_subscriptions(&self) -> Vec<SubscriptionBuiltinTopicData>
Unimplemented. Do not use.
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32 }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(),
"SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType,
CDRSerializerAdapter<_>>(&topic, None).unwrap();
for sub in data_writer.get_matched_subscriptions().iter() {
// do something
}
sourcepub fn dispose(
&self,
key: &<D as Keyed>::K,
source_timestamp: Option<Timestamp>
) -> Result<()>
pub fn dispose(
&self,
key: &<D as Keyed>::K,
source_timestamp: Option<Timestamp>
) -> Result<()>
Disposes data instance with specified key
Arguments
key
- Key of the instancesource_timestamp
- DDS source timestamp (None uses now as time as specified in DDS spec)
Examples
let domain_participant = DomainParticipant::new(0).unwrap();
let qos = QosPolicyBuilder::new().build();
let publisher = domain_participant.create_publisher(&qos).unwrap();
#[derive(Serialize, Deserialize)]
struct SomeType { a: i32, val: usize }
impl Keyed for SomeType {
type K = i32;
fn key(&self) -> Self::K {
self.a
}
}
// WithKey is important
let topic = domain_participant.create_topic("some_topic".to_string(), "SomeType".to_string(), &qos, TopicKind::WithKey).unwrap();
let data_writer = publisher.create_datawriter::<SomeType, CDRSerializerAdapter<_>>(&topic, None).unwrap();
let some_data_1_1 = SomeType { a: 1, val: 3};
let some_data_1_2 = SomeType { a: 1, val: 4};
// different key
let some_data_2_1 = SomeType { a: 2, val: 5};
let some_data_2_2 = SomeType { a: 2, val: 6};
data_writer.write(some_data_1_1, None).unwrap();
data_writer.write(some_data_1_2, None).unwrap();
data_writer.write(some_data_2_1, None).unwrap();
data_writer.write(some_data_2_2, None).unwrap();
// disposes both some_data_1_1 and some_data_1_2. They are no longer offered by this writer to this topic.
data_writer.dispose(&1, None).unwrap();