Skip to content

feat: Synchronous Client: Remove mut from methods #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/clients/rumqttsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn start(id: &str, payload_size: usize, count: usize) -> Result<(), Box<dyn
mqttoptions.set_keep_alive(Duration::from_secs(20));
mqttoptions.set_inflight(100);

let (mut client, mut connection) = Client::new(mqttoptions, 10);
let (client, mut connection) = Client::new(mqttoptions, 10);
thread::spawn(move || {
for _i in 0..count {
let payload = vec![0; payload_size];
Expand Down
1 change: 1 addition & 0 deletions rumqttc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Expose `EventLoop::clean` to allow triggering shutdown and subsequent storage of pending requests

### Changed
- Synchronous client methods take `&self` instead of `&mut self` (#646)

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion rumqttc/examples/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TryFrom<&[u8]> for Message {
fn main() {
let mqqt_opts = MqttOptions::new("test-1", "localhost", 1883);

let (mut client, mut connection) = Client::new(mqqt_opts, 10);
let (client, mut connection) = Client::new(mqqt_opts, 10);
client.subscribe("hello/rumqtt", QoS::AtMostOnce).unwrap();
thread::spawn(move || {
for i in 0..10 {
Expand Down
2 changes: 1 addition & 1 deletion rumqttc/examples/syncpubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
println!("Done with the stream!!");
}

fn publish(mut client: Client) {
fn publish(client: Client) {
thread::sleep(Duration::from_secs(1));
client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();
for i in 0..10_usize {
Expand Down
2 changes: 1 addition & 1 deletion rumqttc/examples/syncrecv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
}
}

fn publish(mut client: Client) {
fn publish(client: Client) {
client.subscribe("hello/+/world", QoS::AtMostOnce).unwrap();
for i in 0..3 {
let payload = vec![1; i];
Expand Down
24 changes: 10 additions & 14 deletions rumqttc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Client {

/// Sends a MQTT Publish to the `EventLoop`
pub fn publish<S, V>(
&mut self,
&self,
topic: S,
qos: QoS,
retain: bool,
Expand All @@ -279,7 +279,7 @@ impl Client {
}

pub fn try_publish<S, V>(
&mut self,
&self,
topic: S,
qos: QoS,
retain: bool,
Expand Down Expand Up @@ -310,25 +310,21 @@ impl Client {
}

/// Sends a MQTT Subscribe to the `EventLoop`
pub fn subscribe<S: Into<String>>(&mut self, topic: S, qos: QoS) -> Result<(), ClientError> {
pub fn subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
let subscribe = Subscribe::new(topic.into(), qos);
let request = Request::Subscribe(subscribe);
self.client.request_tx.send(request)?;
Ok(())
}

/// Sends a MQTT Subscribe to the `EventLoop`
pub fn try_subscribe<S: Into<String>>(
&mut self,
topic: S,
qos: QoS,
) -> Result<(), ClientError> {
pub fn try_subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<(), ClientError> {
self.client.try_subscribe(topic, qos)?;
Ok(())
}

/// Sends a MQTT Subscribe for multiple topics to the `EventLoop`
pub fn subscribe_many<T>(&mut self, topics: T) -> Result<(), ClientError>
pub fn subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
where
T: IntoIterator<Item = SubscribeFilter>,
{
Expand All @@ -338,36 +334,36 @@ impl Client {
Ok(())
}

pub fn try_subscribe_many<T>(&mut self, topics: T) -> Result<(), ClientError>
pub fn try_subscribe_many<T>(&self, topics: T) -> Result<(), ClientError>
where
T: IntoIterator<Item = SubscribeFilter>,
{
self.client.try_subscribe_many(topics)
}

/// Sends a MQTT Unsubscribe to the `EventLoop`
pub fn unsubscribe<S: Into<String>>(&mut self, topic: S) -> Result<(), ClientError> {
pub fn unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
let unsubscribe = Unsubscribe::new(topic.into());
let request = Request::Unsubscribe(unsubscribe);
self.client.request_tx.send(request)?;
Ok(())
}

/// Sends a MQTT Unsubscribe to the `EventLoop`
pub fn try_unsubscribe<S: Into<String>>(&mut self, topic: S) -> Result<(), ClientError> {
pub fn try_unsubscribe<S: Into<String>>(&self, topic: S) -> Result<(), ClientError> {
self.client.try_unsubscribe(topic)?;
Ok(())
}

/// Sends a MQTT disconnect to the `EventLoop`
pub fn disconnect(&mut self) -> Result<(), ClientError> {
pub fn disconnect(&self) -> Result<(), ClientError> {
let request = Request::Disconnect(Disconnect);
self.client.request_tx.send(request)?;
Ok(())
}

/// Sends a MQTT disconnect to the `EventLoop`
pub fn try_disconnect(&mut self) -> Result<(), ClientError> {
pub fn try_disconnect(&self) -> Result<(), ClientError> {
self.client.try_disconnect()?;
Ok(())
}
Expand Down