Assert closure provided to Subscription::map is non-capturing

This commit is contained in:
Héctor Ramón Jiménez 2024-02-05 21:34:35 +01:00
parent f39a5fd895
commit 2470a3fb40
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -89,14 +89,22 @@ impl<Message> Subscription<Message> {
}
/// Transforms the [`Subscription`] output with the given function.
pub fn map<A>(
mut self,
f: impl Fn(Message) -> A + MaybeSend + Clone + 'static,
) -> Subscription<A>
///
/// # Panics
/// The closure provided must be a non-capturing closure. The method
/// will panic in debug mode otherwise.
pub fn map<F, A>(mut self, f: F) -> Subscription<A>
where
Message: 'static,
F: Fn(Message) -> A + MaybeSend + Clone + 'static,
A: 'static,
{
debug_assert!(
std::mem::size_of::<F>() == 0,
"the closure {} provided in `Subscription::map` is capturing",
std::any::type_name::<F>(),
);
Subscription {
recipes: self
.recipes