Skip to main content

EventAdapter

Struct EventAdapter 

Source
pub struct EventAdapter<T> { /* private fields */ }
Expand description

Adapts various event value types into an AttributeValue for event attributes.

The html! macro generates EventAdapter::new(expr).into_attribute(event_name) instead of inline trait dispatch boilerplate. This eliminates the per-attribute-site generation of __EventWrapper, __IsClosure, __ClosurePicker, __ValuePicker, __FallbackHelper, and __dispatch types, significantly reducing macro output size.

The adapter pattern handles three cases:

  • FnMut(NativeEvent) closure → AttributeValue::Event via NativeEventHandler
  • NativeEventHandler directly → AttributeValue::Event as-is
  • Option<NativeEventHandler>AttributeValue::Event or AttributeValue::Text

Implementations§

Source§

impl<T> EventAdapter<T>

Constructs an EventAdapter that wraps any event-compatible value.

Source

pub fn new(inner: T) -> Self

Creates a new EventAdapter wrapping the given value.

§Arguments
  • T - The value to wrap for event attribute adaptation.
§Returns
  • EventAdapter<T> - A new adapter wrapping the value.
Source§

impl<F> EventAdapter<F>
where F: FnMut(NativeEvent) + 'static,

Adapts a FnMut(NativeEvent) closure into an AttributeValue::Event.

Wraps the closure into a NativeEventHandler and returns it as an event attribute value. This replaces the __EventWrapper<F> type that was previously generated inline by the html! macro.

Source

pub fn into_attribute(self, event_name: NativeEventName) -> AttributeValue

Converts the wrapped closure into an event AttributeValue.

§Arguments
  • NativeEventName - The event name enum variant to associate with the handler.
§Returns
  • AttributeValue - An AttributeValue::Event wrapping the handler.
Source§

impl EventAdapter<NativeEventHandler>

Adapts an owned NativeEventHandler into an AttributeValue::Event directly.

When the user already provides a NativeEventHandler, the handler is re-wrapped with the given event_name to ensure the DOM event listener is bound to the correct event type (e.g., “click” rather than “onclick”). This replaces the impl __EventWrapper<NativeEventHandler> that was previously generated inline.

Source

pub fn into_attribute(self, event_name: NativeEventName) -> AttributeValue

Converts the wrapped handler into an event AttributeValue.

Re-wraps the handler with the provided event_name so that the DOM event listener uses the correct event type string.

§Arguments
  • NativeEventName - The event name to bind the handler to.
§Returns
  • AttributeValue - An AttributeValue::Event containing the re-wrapped handler.
Source§

impl EventAdapter<Option<NativeEventHandler>>

Adapts an Option<NativeEventHandler> into an AttributeValue.

Some(handler) becomes AttributeValue::Event(handler) re-wrapped with the given event name, and None becomes AttributeValue::Text(String::new()). This replaces the impl __EventWrapper<Option<NativeEventHandler>> that was previously generated inline by the html! macro.

Source

pub fn into_attribute(self, event_name: NativeEventName) -> AttributeValue

Converts the wrapped optional handler into an attribute value.

Re-wraps a Some handler with the provided event_name so that the DOM event listener uses the correct event type string.

§Arguments
  • NativeEventName - The event name to bind the handler to.
§Returns
  • AttributeValue - An event attribute if Some, otherwise an empty text attribute.

Auto Trait Implementations§

§

impl<T> Freeze for EventAdapter<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for EventAdapter<T>
where T: RefUnwindSafe,

§

impl<T> Send for EventAdapter<T>
where T: Send,

§

impl<T> Sync for EventAdapter<T>
where T: Sync,

§

impl<T> Unpin for EventAdapter<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for EventAdapter<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for EventAdapter<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more