rust/bt-broadcast-assistant: Fix EventStream
Make EventStream have a type parameter that implements
bt_gatt::GattTypes so that we can be more flexible with the trait bounds
of GattTypes associated types when we implement and use the on
platform-side.
Change-Id: I6a95c05f3471f5d3be9860f56944f5affb72aecc
Reviewed-on: https://bluetooth-review.git.corp.google.com/c/bluetooth/+/1800
Reviewed-by: Marie Janssen <jamuraa@google.com>
diff --git a/rust/bt-broadcast-assistant/src/assistant.rs b/rust/bt-broadcast-assistant/src/assistant.rs
index 19102c7..2297dba 100644
--- a/rust/bt-broadcast-assistant/src/assistant.rs
+++ b/rust/bt-broadcast-assistant/src/assistant.rs
@@ -108,10 +108,7 @@
// Creates a broadcast assistant and sets it up to be ready
// for broadcast source scanning. Clients must use the `start`
// method to poll the event stream for scan results.
- pub fn new(central: T::Central) -> Self
- where
- <T as bt_gatt::GattTypes>::ScanResultStream: std::marker::Send,
- {
+ pub fn new(central: T::Central) -> Self {
let scan_result_stream = central.scan(&Self::scan_filters());
Self {
central,
@@ -132,14 +129,11 @@
/// Start broadcast assistant. Returns EventStream that the upper layer can
/// poll. Upper layer can call methods on BroadcastAssistant based on the
/// events it sees.
- pub fn start(&mut self) -> Result<EventStream, Error>
- where
- <T as bt_gatt::GattTypes>::ScanResultStream: std::marker::Send,
- {
+ pub fn start(&mut self) -> Result<EventStream<T>, Error> {
if self.scan_stream.is_none() {
return Err(Error::AlreadyStarted);
}
- Ok(EventStream::new(self.scan_stream.take().unwrap(), self.broadcast_sources.clone()))
+ Ok(EventStream::<T>::new(self.scan_stream.take().unwrap(), self.broadcast_sources.clone()))
}
pub fn scan_for_scan_delegators(&mut self) -> T::ScanResultStream {
diff --git a/rust/bt-broadcast-assistant/src/assistant/event.rs b/rust/bt-broadcast-assistant/src/assistant/event.rs
index 4990e9c..3f781f6 100644
--- a/rust/bt-broadcast-assistant/src/assistant/event.rs
+++ b/rust/bt-broadcast-assistant/src/assistant/event.rs
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+use core::pin::Pin;
use futures::stream::{FusedStream, Stream, StreamExt};
use std::sync::Arc;
use std::task::Poll;
@@ -11,7 +12,6 @@
use bt_common::packet_encoding::Error as PacketError;
use bt_common::PeerId;
use bt_gatt::central::{AdvertisingDatum, ScanResult};
-use core::pin::Pin;
use crate::assistant::{
DiscoveredBroadcastSources, Error, BASIC_AUDIO_ANNOUNCEMENT_SERVICE,
@@ -28,17 +28,16 @@
/// A stream of discovered broadcast sources.
/// This stream polls the scan results from GATT client to discover
/// available broadcast sources.
-pub struct EventStream {
- // Central scan result stream for finding broadcast sources.
- scan_result_stream: Pin<Box<dyn Stream<Item = Result<ScanResult, bt_gatt::types::Error>>>>,
+pub struct EventStream<T: bt_gatt::GattTypes> {
+ scan_result_stream: Pin<Box<<T as bt_gatt::GattTypes>::ScanResultStream>>,
terminated: bool,
broadcast_sources: Arc<DiscoveredBroadcastSources>,
}
-impl EventStream {
+impl<T: bt_gatt::GattTypes> EventStream<T> {
pub(crate) fn new(
- scan_result_stream: impl Stream<Item = Result<ScanResult, bt_gatt::types::Error>> + 'static,
+ scan_result_stream: T::ScanResultStream,
broadcast_sources: Arc<DiscoveredBroadcastSources>,
) -> Self {
Self {
@@ -72,13 +71,13 @@
}
}
-impl FusedStream for EventStream {
+impl<T: bt_gatt::GattTypes> FusedStream for EventStream<T> {
fn is_terminated(&self) -> bool {
self.terminated
}
}
-impl Stream for EventStream {
+impl<T: bt_gatt::GattTypes> Stream for EventStream<T> {
type Item = Result<Event, Error>;
fn poll_next(
@@ -137,16 +136,16 @@
use bt_common::core::{AddressType, AdvertisingSetId};
use bt_gatt::central::{AdvertisingDatum, PeerName};
- use bt_gatt::test_utils::ScannedResultStream;
+ use bt_gatt::test_utils::{FakeTypes, ScannedResultStream};
use bt_gatt::types::Error as BtGattError;
use bt_gatt::types::GattError;
- fn setup_stream() -> (EventStream, ScannedResultStream) {
+ fn setup_stream() -> (EventStream<FakeTypes>, ScannedResultStream) {
let fake_scan_result_stream = ScannedResultStream::new();
let broadcast_sources = DiscoveredBroadcastSources::new();
(
- EventStream::new(fake_scan_result_stream.clone(), broadcast_sources),
+ EventStream::<FakeTypes>::new(fake_scan_result_stream.clone(), broadcast_sources),
fake_scan_result_stream,
)
}