This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better story about in flight messages and rate limit (#141)
- Loading branch information
Showing
4 changed files
with
116 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,59 @@ | ||
use futures::{Async, Poll, Stream}; | ||
use std::collections::VecDeque; | ||
use std::iter::IntoIterator; | ||
|
||
pub trait StreamExt: Stream { | ||
fn prepend(self, first: VecDeque<Self::Item>) -> Prepend<Self> | ||
pub trait Prepend: Stream { | ||
fn prependable(self) -> Prependable<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
new(self, first) | ||
new(self) | ||
} | ||
} | ||
|
||
impl<T: ?Sized> StreamExt for T where T: Stream {} | ||
impl<T: ?Sized> Prepend for T where T: Stream {} | ||
|
||
/// An adapter for chaining the output of two streams. | ||
/// | ||
/// The resulting stream produces items from first stream and then | ||
/// from second stream. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct Prepend<S> | ||
pub struct Prependable<S> | ||
where | ||
S: Stream, | ||
{ | ||
stream: S, | ||
pub session: VecDeque<<S as Stream>::Item>, | ||
items: VecDeque<<S as Stream>::Item>, | ||
} | ||
|
||
pub fn new<S>(stream: S, session: VecDeque<<S as Stream>::Item>) -> Prepend<S> | ||
pub fn new<S>(stream: S) -> Prependable<S> | ||
where | ||
S: Stream, | ||
{ | ||
Prepend { stream, session } | ||
Prependable { | ||
stream, | ||
items: VecDeque::new(), | ||
} | ||
} | ||
|
||
impl<S> Prepend<S> | ||
impl<S> Prependable<S> | ||
where | ||
S: futures::Stream, | ||
{ | ||
pub fn merge_session(&mut self, session: VecDeque<<S as Stream>::Item>) { | ||
self.session.extend(session) | ||
/// Insert items in between present items and wrapped stream | ||
pub fn insert(&mut self, items: impl IntoIterator<Item = <S as Stream>::Item>) { | ||
self.items.extend(items) | ||
} | ||
} | ||
|
||
impl<S> Stream for Prepend<S> | ||
impl<S> Stream for Prependable<S> | ||
where | ||
S: Stream, | ||
{ | ||
type Item = <S as Stream>::Item; | ||
type Error = <S as Stream>::Error; | ||
|
||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { | ||
if let Some(v) = self.session.pop_front() { | ||
debug!("Sending previous session data"); | ||
return Ok(Async::Ready(Some(v))); | ||
if let Some(v) = self.items.pop_front() { | ||
Ok(Async::Ready(Some(v))) | ||
} else { | ||
self.stream.poll() | ||
} | ||
|
||
self.stream.poll() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.