deviceplug / btleplug

Rust Cross-Platform Host-Side Bluetooth LE Access Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`characteristics()` and `services()` of `Peripheral` returning a `HashMap` instead of a `BTreeSet`

hansmbakker opened this issue · comments

Feature Description

Would it be possible to have characteristics() and services() of Peripheral return a HashMap instead of a BTreeSet?

That way, looping over the characteristics or calling iter().find() to find the characteristic you need is not needed anymore as below:

let chars = light.characteristics();
let cmd_char = chars
.iter()
.find(|c| c.uuid == LIGHT_CHARACTERISTIC_UUID)

Instead you could do something like

let cmd_char = light.characteristics()[&LIGHT_CHARACTERISTIC_UUID];

One thing to consider here is that bluetooth le peripherals may advertise multiple instances of a service or characteristic with the same Uuid (differentiated by their underlying AT handle) which couldn't be supported by btleplug if they were only exposed via a HashMap, indexed by uuid.

Their on-device order may also be significant, which would be lost with a HashMap.

let cmd_char = light.characteristics()[&LIGHT_CHARACTERISTIC_UUID];

If it is just about API ergonomics, then a new type around the returned BTreeSet plus an imlementation of Index would also allow this API.