Skip to content

Commit c719181

Browse files
Devdutt Shenoiswanandx
andauthored
refactor(rumqttc): clean-up examples (#794)
Co-authored-by: swanandx <[email protected]>
1 parent e63925a commit c719181

16 files changed

+44
-61
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rumqttc/Cargo.toml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,24 @@ color-backtrace = "0.6"
5454
matches = "0.1"
5555
pretty_assertions = "1"
5656
pretty_env_logger = "0.5"
57-
rustls = "0.21"
58-
rustls-native-certs = "0.6"
5957
serde = { version = "1", features = ["derive"] }
60-
tokio = { version = "1", features = ["full", "macros"] }
58+
59+
[[example]]
60+
name = "tls"
61+
path = "examples/tls.rs"
62+
required-features = ["use-rustls"]
63+
64+
[[example]]
65+
name = "tls2"
66+
path = "examples/tls2.rs"
67+
required-features = ["use-rustls"]
68+
69+
[[example]]
70+
name = "websocket"
71+
path = "examples/websocket.rs"
72+
required-features = ["websocket"]
73+
74+
[[example]]
75+
name = "websocket_proxy"
76+
path = "examples/websocket_proxy.rs"
77+
required-features = ["websocket", "proxy"]

rumqttc/examples/async_manual_acks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use tokio::{task, time};
22

3-
use rumqttc::{self, AsyncClient, Event, EventLoop, Incoming, MqttOptions, QoS};
3+
use rumqttc::{AsyncClient, Event, EventLoop, Incoming, MqttOptions, QoS};
44
use std::error::Error;
55
use std::time::Duration;
66

@@ -14,7 +14,7 @@ fn create_conn() -> (AsyncClient, EventLoop) {
1414
AsyncClient::new(mqttoptions, 10)
1515
}
1616

17-
#[tokio::main(worker_threads = 1)]
17+
#[tokio::main(flavor = "current_thread")]
1818
async fn main() -> Result<(), Box<dyn Error>> {
1919
pretty_env_logger::init();
2020

rumqttc/examples/async_manual_acks_v5.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(dead_code, unused_imports)]
21
use rumqttc::v5::mqttbytes::v5::Packet;
32
use rumqttc::v5::mqttbytes::QoS;
43
use tokio::{task, time};
@@ -17,7 +16,7 @@ fn create_conn() -> (AsyncClient, EventLoop) {
1716
AsyncClient::new(mqttoptions, 10)
1817
}
1918

20-
#[tokio::main(worker_threads = 1)]
19+
#[tokio::main(flavor = "current_thread")]
2120
async fn main() -> Result<(), Box<dyn Error>> {
2221
// todo!("fix this example with new way of spawning clients")
2322
pretty_env_logger::init();

rumqttc/examples/asyncpubsub.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use tokio::{task, time};
22

3-
use rumqttc::{self, AsyncClient, MqttOptions, QoS};
3+
use rumqttc::{AsyncClient, MqttOptions, QoS};
44
use std::error::Error;
55
use std::time::Duration;
66

7-
#[tokio::main(worker_threads = 1)]
7+
#[tokio::main(flavor = "current_thread")]
88
async fn main() -> Result<(), Box<dyn Error>> {
99
pretty_env_logger::init();
1010
// color_backtrace::install();

rumqttc/examples/asyncpubsub_v5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rumqttc::v5::{AsyncClient, MqttOptions};
55
use std::error::Error;
66
use std::time::Duration;
77

8-
#[tokio::main(worker_threads = 1)]
8+
#[tokio::main(flavor = "current_thread")]
99
async fn main() -> Result<(), Box<dyn Error>> {
1010
pretty_env_logger::init();
1111
// color_backtrace::install();

rumqttc/examples/serde.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ fn main() {
5151
});
5252

5353
// Iterate to poll the eventloop for connection progress
54-
for notification in connection.iter() {
55-
if let Ok(Event::Incoming(Incoming::Publish(packet))) = notification {
54+
for notification in connection.iter().flatten() {
55+
if let Event::Incoming(Incoming::Publish(packet)) = notification {
5656
match Message::try_from(packet.payload.as_ref()) {
5757
Ok(message) => println!("Payload = {message:?}"),
5858
Err(error) => println!("Error = {error}"),

rumqttc/examples/subscription_ids.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rumqttc::v5::{AsyncClient, MqttOptions};
66
use std::error::Error;
77
use std::time::Duration;
88

9-
#[tokio::main(worker_threads = 1)]
9+
#[tokio::main(flavor = "current_thread")]
1010
async fn main() -> Result<(), Box<dyn Error>> {
1111
pretty_env_logger::init();
1212

rumqttc/examples/syncpubsub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rumqttc::{self, Client, LastWill, MqttOptions, QoS};
1+
use rumqttc::{Client, LastWill, MqttOptions, QoS};
22
use std::thread;
33
use std::time::Duration;
44

rumqttc/examples/syncrecv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rumqttc::{self, Client, LastWill, MqttOptions, QoS};
1+
use rumqttc::{Client, LastWill, MqttOptions, QoS};
22
use std::thread;
33
use std::time::Duration;
44

rumqttc/examples/tls.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//! Example of how to configure rumqttd to connect to a server using TLS and authentication.
22
use std::error::Error;
33

4-
#[cfg(feature = "use-rustls")]
5-
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn Error>> {
7-
use rumqttc::{self, AsyncClient, Event, Incoming, MqttOptions, Transport};
8-
use tokio_rustls::rustls::ClientConfig;
4+
use rumqttc::{AsyncClient, Event, Incoming, MqttOptions, Transport};
5+
use tokio_rustls::rustls::ClientConfig;
96

7+
#[tokio::main(flavor = "current_thread")]
8+
async fn main() -> Result<(), Box<dyn Error>> {
109
pretty_env_logger::init();
1110
color_backtrace::install();
1211

@@ -45,8 +44,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
4544
}
4645
}
4746
}
48-
49-
#[cfg(not(feature = "use-rustls"))]
50-
fn main() -> Result<(), Box<dyn Error>> {
51-
panic!("Enable feature 'use-rustls'");
52-
}

rumqttc/examples/tls2.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Example of how to configure rumqttd to connect to a server using TLS and authentication.
22
use std::error::Error;
33

4-
#[cfg(feature = "use-rustls")]
5-
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn Error>> {
7-
use rumqttc::{self, AsyncClient, MqttOptions, TlsConfiguration, Transport};
4+
use rumqttc::{AsyncClient, MqttOptions, TlsConfiguration, Transport};
85

6+
#[tokio::main(flavor = "current_thread")]
7+
async fn main() -> Result<(), Box<dyn Error>> {
98
pretty_env_logger::init();
109
color_backtrace::install();
1110

@@ -44,8 +43,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
4443

4544
Ok(())
4645
}
47-
48-
#[cfg(not(feature = "use-rustls"))]
49-
fn main() -> Result<(), Box<dyn Error>> {
50-
panic!("Enable feature 'use-rustls'");
51-
}

rumqttc/examples/topic_alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rumqttc::v5::{AsyncClient, MqttOptions};
55
use std::error::Error;
66
use std::time::Duration;
77

8-
#[tokio::main(worker_threads = 1)]
8+
#[tokio::main(flavor = "current_thread")]
99
async fn main() -> Result<(), Box<dyn Error>> {
1010
pretty_env_logger::init();
1111

rumqttc/examples/websocket.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
#[cfg(feature = "websocket")]
2-
use rumqttc::{self, AsyncClient, MqttOptions, QoS, Transport};
3-
#[cfg(feature = "websocket")]
1+
use rumqttc::{AsyncClient, MqttOptions, QoS, Transport};
42
use std::{error::Error, time::Duration};
5-
#[cfg(feature = "websocket")]
63
use tokio::{task, time};
74

8-
#[cfg(feature = "websocket")]
9-
#[tokio::main(worker_threads = 1)]
5+
#[tokio::main(flavor = "current_thread")]
106
async fn main() -> Result<(), Box<dyn Error>> {
117
pretty_env_logger::init();
128

@@ -39,7 +35,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
3935
}
4036
}
4137

42-
#[cfg(feature = "websocket")]
4338
async fn requests(client: AsyncClient) {
4439
client
4540
.subscribe("hello/world", QoS::AtMostOnce)
@@ -57,8 +52,3 @@ async fn requests(client: AsyncClient) {
5752

5853
time::sleep(Duration::from_secs(120)).await;
5954
}
60-
61-
#[cfg(not(feature = "websocket"))]
62-
fn main() {
63-
panic!("Enable websocket feature with `--features=websocket`");
64-
}

rumqttc/examples/websocket_proxy.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
#[cfg(all(feature = "websocket", feature = "proxy"))]
2-
use rumqttc::{self, AsyncClient, Proxy, ProxyAuth, ProxyType, QoS, Transport};
3-
#[cfg(all(feature = "websocket", feature = "proxy"))]
1+
use rumqttc::{AsyncClient, Proxy, ProxyAuth, ProxyType, QoS, Transport};
42
use std::{error::Error, time::Duration};
5-
#[cfg(all(feature = "websocket", feature = "proxy"))]
63
use tokio::{task, time};
74

8-
#[cfg(all(feature = "websocket", feature = "proxy"))]
9-
#[tokio::main(worker_threads = 1)]
5+
#[tokio::main(flavor = "current_thread")]
106
async fn main() -> Result<(), Box<dyn Error>> {
117
use rumqttc::MqttOptions;
128

@@ -48,7 +44,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
4844
}
4945
}
5046

51-
#[cfg(all(feature = "websocket", feature = "proxy"))]
5247
async fn requests(client: AsyncClient) {
5348
client
5449
.subscribe("hello/world", QoS::AtMostOnce)
@@ -66,8 +61,3 @@ async fn requests(client: AsyncClient) {
6661

6762
time::sleep(Duration::from_secs(120)).await;
6863
}
69-
70-
#[cfg(not(all(feature = "websocket", feature = "proxy")))]
71-
fn main() {
72-
panic!("Enable websocket and proxy feature with `--features=websocket, proxy`");
73-
}

rumqttc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//! use std::time::Duration;
3939
//! use std::error::Error;
4040
//!
41-
//! # #[tokio::main(worker_threads = 1)]
41+
//! # #[tokio::main(flavor = "current_thread")]
4242
//! # async fn main() {
4343
//! let mut mqttoptions = MqttOptions::new("rumqtt-async", "test.mosquitto.org", 1883);
4444
//! mqttoptions.set_keep_alive(Duration::from_secs(5));

0 commit comments

Comments
 (0)