Skip to content

Commit

Permalink
fix: corebluetooth fulfill characteristics futures on peripheral di…
Browse files Browse the repository at this point in the history
…sconnect
  • Loading branch information
szymonlesisz authored and qwandor committed Nov 18, 2024
1 parent 70ee65c commit 5f64e11
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,32 @@ impl PeripheralInternal {
if let Some(future) = self.disconnected_future_state.take() {
future.lock().unwrap().set_reply(CoreBluetoothReply::Ok)
}

// Fulfill all pending futures
let error = CoreBluetoothReply::Err(String::from("Device disconnected"));
self.services.iter().for_each(|(_, service)| {
service
.characteristics
.iter()
.for_each(|(_, characteristic)| {
let CharacteristicInternal {
read_future_state,
write_future_state,
subscribe_future_state,
unsubscribe_future_state,
..
} = characteristic;

let futures = read_future_state
.into_iter()

Check warning on line 347 in src/corebluetooth/internal.rs

View workflow job for this annotation

GitHub Actions / clippy macOS-latest

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque`

warning: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque` --> src/corebluetooth/internal.rs:347:26 | 347 | .into_iter() | ^^^^^^^^^ help: call directly: `iter` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref = note: `#[warn(clippy::into_iter_on_ref)]` on by default
.chain(write_future_state.into_iter())

Check warning on line 348 in src/corebluetooth/internal.rs

View workflow job for this annotation

GitHub Actions / clippy macOS-latest

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> src/corebluetooth/internal.rs:348:32 | 348 | .chain(write_future_state.into_iter()) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `write_future_state` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/iter/traits/iterator.rs:479:12 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default

Check warning on line 348 in src/corebluetooth/internal.rs

View workflow job for this annotation

GitHub Actions / clippy macOS-latest

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque`

warning: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque` --> src/corebluetooth/internal.rs:348:51 | 348 | .chain(write_future_state.into_iter()) | ^^^^^^^^^ help: call directly: `iter` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref
.chain(subscribe_future_state.into_iter())

Check warning on line 349 in src/corebluetooth/internal.rs

View workflow job for this annotation

GitHub Actions / clippy macOS-latest

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> src/corebluetooth/internal.rs:349:32 | 349 | .chain(subscribe_future_state.into_iter()) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `subscribe_future_state` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/iter/traits/iterator.rs:479:12 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

Check warning on line 349 in src/corebluetooth/internal.rs

View workflow job for this annotation

GitHub Actions / clippy macOS-latest

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque`

warning: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque` --> src/corebluetooth/internal.rs:349:55 | 349 | .chain(subscribe_future_state.into_iter()) | ^^^^^^^^^ help: call directly: `iter` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref
.chain(unsubscribe_future_state.into_iter());

Check warning on line 350 in src/corebluetooth/internal.rs

View workflow job for this annotation

GitHub Actions / clippy macOS-latest

explicit call to `.into_iter()` in function argument accepting `IntoIterator`

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator` --> src/corebluetooth/internal.rs:350:32 | 350 | .chain(unsubscribe_future_state.into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `unsubscribe_future_state` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/core/src/iter/traits/iterator.rs:479:12 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion

Check warning on line 350 in src/corebluetooth/internal.rs

View workflow job for this annotation

GitHub Actions / clippy macOS-latest

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque`

warning: this `.into_iter()` call is equivalent to `.iter()` and will not consume the `VecDeque` --> src/corebluetooth/internal.rs:350:57 | 350 | .chain(unsubscribe_future_state.into_iter()); | ^^^^^^^^^ help: call directly: `iter` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref
for state in futures {
state.lock().unwrap().set_reply(error.clone());
}
});
});
}
}

Expand Down Expand Up @@ -734,8 +760,9 @@ impl CoreBluetoothInternal {
self.get_characteristic(peripheral_uuid, service_uuid, characteristic_uuid)
{
trace!("Got subscribed event!");
let state = characteristic.subscribe_future_state.pop_back().unwrap();
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
if let Some(state) = characteristic.subscribe_future_state.pop_back() {
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
}
}
}

Expand All @@ -749,8 +776,9 @@ impl CoreBluetoothInternal {
self.get_characteristic(peripheral_uuid, service_uuid, characteristic_uuid)
{
trace!("Got unsubscribed event!");
let state = characteristic.unsubscribe_future_state.pop_back().unwrap();
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
if let Some(state) = characteristic.unsubscribe_future_state.pop_back() {
state.lock().unwrap().set_reply(CoreBluetoothReply::Ok);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/corebluetooth/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::Ok => {}
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
reply => panic!("Unexpected reply: {:?}", reply),
}
Ok(())
Expand All @@ -333,6 +334,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::ReadResult(chars) => Ok(chars),
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
_ => {
panic!("Shouldn't get anything but read result!");
}
Expand All @@ -353,6 +355,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::Ok => trace!("subscribed!"),
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
_ => panic!("Didn't subscribe!"),
}
Ok(())
Expand All @@ -372,6 +375,7 @@ impl api::Peripheral for Peripheral {
.await?;
match fut.await {
CoreBluetoothReply::Ok => {}
CoreBluetoothReply::Err(msg) => return Err(Error::RuntimeError(msg)),
_ => panic!("Didn't unsubscribe!"),
}
Ok(())
Expand Down

0 comments on commit 5f64e11

Please sign in to comment.