deviceplug / btleplug

Rust Cross-Platform Host-Side Bluetooth LE Access Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] What is the associated type of `Manager` trait?

yozachar opened this issue · comments

Hi I'm new to Rust. Since the discussions tab wasn't open, I'm asking a question here.

Here's my code:

use btleplug::api::{Central, Manager};
use std::error::Error;

pub async fn bluetooth() -> Result<(), Box<dyn Error>> {
    // Create a new manager.
    let manager = Manager::new().unwrap(); // type ERROR here !

    // Get the first Bluetooth adapter.
    let adapter = manager.adapters().await?.into_iter().nth(0).unwrap();

    // Create a central instance.
    let central = Central::new(adapter).await?;

    // Start scanning for devices.
    central.start_scan().await?;

    // list devices
    // how ?

    Ok(())
}

LSP complains:

the value of the associated type `Adapter` (from trait `btleplug::api::Manager`) must be specified rustc(Click for full compiler diagnostic)
wireless.rs(8, 19): specify the associated type: `Manager<Adapter = Type>`
error[E0191]: the value of the associated type `Adapter` (from trait `btleplug::api::Manager`) must be specified
 --> src/wireless.rs:8:19
  |
8 |     let manager = Manager::new().unwrap();
  |                   ^^^^^^^ help: specify the associated type: `Manager<Adapter = Type>`

I think I'm supposed to annotate type as follows:

use btleplug::{api::{Central, Manager}, platform::Adapter};

...

let manager: Manager<Adapter = ?> = Manager::new().unwrap();

But what type goes in place of ? ?

Rather than

use btleplug::api::{Central, Manager};

try:

use btleplug::api::{Central, Manager as _};
use btleplug::platform::Manager;

btleplug::api::Manager is a trait, whereas btleplug::platform::Manager is a type implementing that trait. The compiler needs to know the concrete type you want to call new on.

Thank you!

Also found the docs and examples.