Skip to content

Commit

Permalink
Make the publish call return a result
Browse files Browse the repository at this point in the history
Potential solution for eclipse-paho#101

Signed-off-by: Sander van Harmelen <sander@vanharmelen.nl>
  • Loading branch information
svanharmelen committed Oct 28, 2020
1 parent 976e7ea commit 3d4a35f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl AsyncClient {
///
/// * `msg` The message to publish.
///
pub fn publish(&self, msg: Message) -> DeliveryToken {
pub fn publish(&self, msg: Message) -> Result<DeliveryToken> {
debug!("Publish: {:?}", msg);

let ver = self.mqtt_version();
Expand All @@ -648,13 +648,11 @@ impl AsyncClient {
};

if rc != 0 {
let _ = unsafe { Token::from_raw(rsp_opts.copts.context) };
let msg: Message = tok.into();
DeliveryToken::from_error(msg, rc)
}
else {
warn!("Publish result: {}", rc);
Err(rc.into())
} else {
tok.set_msgid(rsp_opts.copts.token as i16);
tok
Ok(tok)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Client {

/// Publishes a message to an MQTT broker
pub fn publish(&self, msg: Message) -> Result<()> {
self.cli.publish(msg).wait_for(self.timeout)
self.cli.publish(msg)?.wait_for(self.timeout)
}

/// Subscribes to a single topic.
Expand Down
6 changes: 4 additions & 2 deletions src/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use crate::{
async_client::AsyncClient,
errors::Result,
token::{
Token,
DeliveryToken,
Expand Down Expand Up @@ -107,8 +108,9 @@ impl<'a> Topic<'a>
///
/// `payload` The payload of the message
///
pub fn publish<V>(&self, payload: V) -> DeliveryToken
where V: Into<Vec<u8>>
pub fn publish<V>(&self, payload: V) -> Result<DeliveryToken>
where
V: Into<Vec<u8>>,
{
// OPTIMIZE: This could be more efficient.
let msg = Message::new(self.topic.clone(), payload, self.qos);
Expand Down

0 comments on commit 3d4a35f

Please sign in to comment.