From 48145ba51e045f8b0b4788f3a75d20b9d9b7e6ad Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Dec 2019 08:51:44 +0100 Subject: Use `oneshot` and `future::select` to cancel streams --- core/src/subscription.rs | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) (limited to 'core/src') diff --git a/core/src/subscription.rs b/core/src/subscription.rs index 1e6695d6..796982c7 100644 --- a/core/src/subscription.rs +++ b/core/src/subscription.rs @@ -2,60 +2,51 @@ /// An event subscription. pub struct Subscription { - definitions: Vec>>, + handles: Vec>>, } impl Subscription { pub fn none() -> Self { Self { - definitions: Vec::new(), + handles: Vec::new(), } } pub fn batch(subscriptions: impl Iterator>) -> Self { Self { - definitions: subscriptions - .flat_map(|subscription| subscription.definitions) + handles: subscriptions + .flat_map(|subscription| subscription.handles) .collect(), } } - pub fn definitions(self) -> Vec>> { - self.definitions + pub fn handles(self) -> Vec>> { + self.handles } } impl From for Subscription where - A: Definition + 'static, + A: Handle + 'static, { - fn from(definition: A) -> Self { + fn from(handle: A) -> Self { Self { - definitions: vec![Box::new(definition)], + handles: vec![Box::new(handle)], } } } -/// The definition of an event subscription. -pub trait Definition { - type Message; +/// The handle of an event subscription. +pub trait Handle { + type Output; fn id(&self) -> u64; - fn stream( - &self, - ) -> ( - futures::stream::BoxStream<'static, Self::Message>, - Box, - ); -} - -pub trait Handle { - fn cancel(&mut self); + fn stream(&self) -> futures::stream::BoxStream<'static, Self::Output>; } impl std::fmt::Debug for Subscription { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Command").finish() + f.debug_struct("Subscription").finish() } } -- cgit