Skip to content

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

@mattsoftware

Description

@mattsoftware

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions