This commit is contained in:
Waffle 2019-12-16 12:40:23 +03:00
parent c2677929f1
commit 3cf3fe2acf
3 changed files with 22 additions and 30 deletions

View file

@ -75,8 +75,8 @@ type FiltersAndHandlers<'a, T, E> = Vec<FilterAndHandler<'a, T, E>>;
/// // with error policy that just ignores all errors (that can't ever happen)
/// let mut dp = FilterDispatcher::<Infallible, _>::new(|_| async {})
/// // Add 'handler' that will handle all messages sent to the bot
/// .message_handler(true, |mes: Message| {
/// async move { println!("New message: {:?}", mes) }
/// .message_handler(true, |mes: Message| async move {
/// println!("New message: {:?}", mes)
/// })
/// // Add 'handler' that will handle all
/// // messages edited in chat with the bot
@ -318,16 +318,12 @@ mod tests {
let counter2 = &AtomicI32::new(0);
let mut dp = FilterDispatcher::<Infallible, _>::new(|_| async {})
.message_handler(true, |_mes: Message| {
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
.message_handler(true, |_mes: Message| async move {
counter.fetch_add(1, Ordering::SeqCst);
})
.message_handler(true, |_mes: Message| {
async move {
counter2.fetch_add(1, Ordering::SeqCst);
Ok::<_, Infallible>(())
}
.message_handler(true, |_mes: Message| async move {
counter2.fetch_add(1, Ordering::SeqCst);
Ok::<_, Infallible>(())
});
dp.dispatch(one_message_updater()).await;

View file

@ -136,19 +136,17 @@ where
}
pub fn polling<'a>(bot: &'a Bot) -> impl Updater<Error = RequestError> + 'a {
let stream = stream::unfold((bot, 0), |(bot, mut offset)| {
async move {
let updates = match bot.get_updates().offset(offset).send().await {
Ok(updates) => {
if let Some(upd) = updates.last() {
offset = upd.id + 1;
}
updates.into_iter().map(Ok).collect::<Vec<_>>()
let stream = stream::unfold((bot, 0), |(bot, mut offset)| async move {
let updates = match bot.get_updates().offset(offset).send().await {
Ok(updates) => {
if let Some(upd) = updates.last() {
offset = upd.id + 1;
}
Err(err) => vec![Err(err)],
};
Some((stream::iter(updates), (bot, offset)))
}
updates.into_iter().map(Ok).collect::<Vec<_>>()
}
Err(err) => vec![Err(err)],
};
Some((stream::iter(updates), (bot, offset)))
})
.flatten();

View file

@ -42,13 +42,11 @@ pub async fn download_file_stream(
.await?
.error_for_status()?;
Ok(futures::stream::unfold(res, |mut res| {
async {
match res.chunk().await {
Err(err) => Some((Err(err), res)),
Ok(Some(c)) => Some((Ok(c), res)),
Ok(None) => None,
}
Ok(futures::stream::unfold(res, |mut res| async {
match res.chunk().await {
Err(err) => Some((Err(err), res)),
Ok(Some(c)) => Some((Ok(c), res)),
Ok(None) => None,
}
}))
}