Don't panic in Polling::listen

This commit is contained in:
Maybe Waffle 2024-01-11 01:15:41 +01:00
parent 339076d3c5
commit d60552a878

View file

@ -1,7 +1,6 @@
use std::{ use std::{
convert::TryInto, convert::TryInto,
future::Future, future::Future,
mem,
pin::Pin, pin::Pin,
task::{ task::{
self, self,
@ -106,7 +105,6 @@ where
drop_pending_updates, drop_pending_updates,
flag: Some(flag), flag: Some(flag),
token, token,
stop_token_cloned: false,
}; };
assert_update_listener(polling) assert_update_listener(polling)
@ -251,7 +249,6 @@ pub struct Polling<B> {
drop_pending_updates: bool, drop_pending_updates: bool,
flag: Option<StopFlag>, flag: Option<StopFlag>,
token: StopToken, token: StopToken,
stop_token_cloned: bool,
} }
impl<R> Polling<R> impl<R> Polling<R>
@ -275,15 +272,12 @@ where
/// Returns true if re-initialization happened *and* /// Returns true if re-initialization happened *and*
/// the previous token was cloned. /// the previous token was cloned.
fn reinit_stop_flag_if_needed(&mut self) -> bool { fn reinit_stop_flag_if_needed(&mut self) {
if self.flag.is_some() { if self.flag.is_none() {
return false;
}
let (token, flag) = mk_stop_token(); let (token, flag) = mk_stop_token();
self.token = token; self.token = token;
self.flag = Some(flag); self.flag = Some(flag);
mem::replace(&mut self.stop_token_cloned, false) }
} }
} }
@ -332,20 +326,9 @@ impl<B: Requester + Send + 'static> UpdateListener for Polling<B> {
let allowed_updates = self.allowed_updates.clone(); let allowed_updates = self.allowed_updates.clone();
let drop_pending_updates = self.drop_pending_updates; let drop_pending_updates = self.drop_pending_updates;
let token_used_and_updated = self.reinit_stop_flag_if_needed();
// FIXME: document that `listen` is a destructive operation, actually, // FIXME: document that `listen` is a destructive operation, actually,
// and you need to call `stop_token` *again* after it // and you need to call `stop_token` *again* after it
// self.reinit_stop_flag_if_needed();
// maybe also remove the panic, it's a lot of additional work, for little
// benefit, it seems like.
if token_used_and_updated {
panic!(
"detected calling `as_stream` a second time after calling `stop_token`. \
`as_stream` updates the stop token, thus you need to call it again after \
calling `as_stream`"
)
}
// FIXME: do update dropping *here* // FIXME: do update dropping *here*
@ -370,7 +353,6 @@ impl<B: Requester + Send + 'static> UpdateListener for Polling<B> {
fn stop_token(&mut self) -> StopToken { fn stop_token(&mut self) -> StopToken {
self.reinit_stop_flag_if_needed(); self.reinit_stop_flag_if_needed();
self.stop_token_cloned = true;
self.token.clone() self.token.clone()
} }