Skip to content
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

Example in README file does not work, cannot find encode/decode in this scope #38

Open
mattsoftware opened this issue Jan 22, 2021 · 2 comments

Comments

@mattsoftware
Copy link

Apologies if I am missing something obvious. I am a rust newbie and and am trying to get a simple mqtt POC working.

I have this in my Cargo.toml file

[dependencies]
mqttrs = { version = "0.4", features = [ "derive" ] }
bytes = "1.0"

and this is my main file (copied verbatim from the README.md file)

use bytes::BytesMut;
use mqttrs::*;

fn main() {
    // Allocate write buffer.
    let mut buf = BytesMut::with_capacity(1024);

    // Encode an MQTT Connect packet.
    let pkt = Packet::Connect(Connect {
        protocol: Protocol::MQTT311,
        keep_alive: 30,
        client_id: "doc_client".into(),
        clean_session: true,
        last_will: None,
        username: None,
        password: None,
    });
    assert!(encode(&pkt, &mut buf).is_ok());
    assert_eq!(&buf[14..], "doc_client".as_bytes());
    let mut encoded = buf.clone();

    // Decode one packet. The buffer will advance to the next packet.
    assert_eq!(Ok(Some(pkt)), decode(&mut buf));

    // Example decode failures.
    let mut incomplete = encoded.split_to(10);
    assert_eq!(Ok(None), decode(&mut incomplete));
    let mut garbage = BytesMut::from(&[0u8, 0, 0, 0] as &[u8]);
    assert_eq!(Err(Error::InvalidHeader), decode(&mut garbage));
}

And the compiler complains that the encode and decode functions do not exist...

error[E0425]: cannot find function `encode` in this scope
  --> src/main.rs:18:13
   |
18 |     assert!(encode(&pkt, &mut buf).is_ok());
   |             ^^^^^^ not found in this scope

error[E0425]: cannot find function `decode` in this scope
  --> src/main.rs:23:31
   |
23 |     assert_eq!(Ok(Some(pkt)), decode(&mut buf));
   |                               ^^^^^^ not found in this scope
   |
help: consider importing this function
   |
1  | use core::num::flt2dec::decode;
   |

error[E0425]: cannot find function `decode` in this scope
  --> src/main.rs:27:26
   |
27 |     assert_eq!(Ok(None), decode(&mut incomplete));
   |                          ^^^^^^ not found in this scope
   |
help: consider importing this function
   |
1  | use core::num::flt2dec::decode;
   |

error[E0425]: cannot find function `decode` in this scope
  --> src/main.rs:29:43
   |
29 |     assert_eq!(Err(Error::InvalidHeader), decode(&mut garbage));
   |                                           ^^^^^^ not found in this scope
   |
help: consider importing this function
   |
1  | use core::num::flt2dec::decode;
   |

error: aborting due to 4 previous errors

I have tried importing those functions directly in the use statement, but they do not seem to exist (compiler suggested encoder and decoder, however they were private so that provided more errors). I have also tried removing the serde feature in the Cargo.toml file to no effect.

Thanks.

@irexiz
Copy link

irexiz commented Feb 9, 2021

encode and decode have changed to encode_slice and decode_slice since 1e6d843

The following changes to the example work for me:

use bytes::BytesMut;
use mqttrs::*;

fn main() {
    // Allocate write buffer.
    let mut buf = [0u8; 1024];

    // Encode an MQTT Connect packet.
    let pkt = Packet::Connect(Connect {
        protocol: Protocol::MQTT311,
        keep_alive: 30,
        client_id: "doc_client".into(),
        clean_session: true,
        last_will: None,
        username: None,
        password: None,
    });
    let encoded = encode_slice(&pkt, &mut buf);
    assert!(encoded.is_ok());
    let mut buf = BytesMut::from(&buf[..encoded.unwrap()]);

    assert_eq!(&buf[14..], "doc_client".as_bytes());

    // Decode one packet. The buffer will advance to the next packet.
    assert_eq!(Ok(Some(pkt)), decode_slice(&mut buf));

    // Example decode failures.
    let mut incomplete = buf.split_to(10);
    assert_eq!(Ok(None), decode_slice(&mut incomplete));
    let mut garbage = BytesMut::from(&[0u8, 0, 0, 0] as &[u8]);
    assert_eq!(Err(Error::InvalidHeader), decode_slice(&mut garbage));
}

I suggest updating README.md with this, if the repository owner is ok with this

@irexiz
Copy link

irexiz commented Feb 9, 2021

In addition, I'll be forking the repository to try and work in some MQTTv5 features as I have a need for some of them (mainly Message Expiry and Maximum Packet Size)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants