diff --git a/wisp/src/extensions/mod.rs b/wisp/src/extensions/mod.rs index 8d2e936..0db95be 100644 --- a/wisp/src/extensions/mod.rs +++ b/wisp/src/extensions/mod.rs @@ -264,3 +264,51 @@ impl From for AnyProtocolExtensionBuilder { Self::new(value) } } + +/// Helper functions for `Vec` +pub trait ProtocolExtensionBuilderVecExt { + /// Returns a reference to the protocol extension builder specified, if it was found. + fn find_extension(&self) -> Option<&T>; + /// Returns a mutable reference to the protocol extension builder specified, if it was found. + fn find_extension_mut(&mut self) -> Option<&mut T>; + + /// Removes any instances of the protocol extension builder specified, if it was found. + fn remove_extension(&mut self); +} + +impl ProtocolExtensionBuilderVecExt for Vec { + fn find_extension(&self) -> Option<&T> { + self.iter().find_map(|x| x.downcast_ref::()) + } + fn find_extension_mut(&mut self) -> Option<&mut T> { + self.iter_mut().find_map(|x| x.downcast_mut::()) + } + + fn remove_extension(&mut self) { + self.retain(|x| x.downcast_ref::().is_none()); + } +} + +/// Helper functions for `Vec` +pub trait ProtocolExtensionVecExt { + /// Returns a reference to the protocol extension specified, if it was found. + fn find_extension(&self) -> Option<&T>; + /// Returns a mutable reference to the protocol extension specified, if it was found. + fn find_extension_mut(&mut self) -> Option<&mut T>; + + /// Removes any instances of the protocol extension specified, if it was found. + fn remove_extension(&mut self); +} + +impl ProtocolExtensionVecExt for Vec { + fn find_extension(&self) -> Option<&T> { + self.iter().find_map(|x| x.downcast_ref::()) + } + fn find_extension_mut(&mut self) -> Option<&mut T> { + self.iter_mut().find_map(|x| x.downcast_mut::()) + } + + fn remove_extension(&mut self) { + self.retain(|x| x.downcast_ref::().is_none()); + } +} diff --git a/wisp/src/mux/mod.rs b/wisp/src/mux/mod.rs index 18624f0..8b97ef3 100644 --- a/wisp/src/mux/mod.rs +++ b/wisp/src/mux/mod.rs @@ -123,16 +123,15 @@ where } } +/// Wisp V2 middleware closure. +pub type WispV2Middleware = dyn for<'a> Fn( + &'a mut Vec, + ) -> Pin> + Sync + Send + 'a>> + + Send; /// Wisp V2 handshake and protocol extension settings wrapper struct. pub struct WispV2Handshake { builders: Vec, - #[expect(clippy::type_complexity)] - closure: Box< - dyn Fn( - &mut Vec, - ) -> Pin> + Sync + Send>> - + Send, - >, + closure: Box, } impl WispV2Handshake { @@ -145,18 +144,11 @@ impl WispV2Handshake { } /// Create a Wisp V2 settings struct with some middleware. - pub fn new_with_middleware(builders: Vec, closure: C) -> Self - where - C: Fn( - &mut Vec, - ) -> Pin> + Sync + Send>> - + Send - + 'static, - { - Self { - builders, - closure: Box::new(closure), - } + pub fn new_with_middleware( + builders: Vec, + closure: Box, + ) -> Self { + Self { builders, closure } } /// Add a Wisp V2 extension builder to the settings struct.