# WeakEvent

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-base/utilities/struct-weakevent/

**struct** · [`ferroui-base`](/docs/0.5.0/api/ferroui-base/) · v0.5.0

```rust
struct WeakEvent<S: WeakEventSender, A: WeakEventArgs + ?Sized>
```

Manages subscriptions to events using weak listeners.

One instance describes one event (usually a thread-local static, see
`WeakEvents`). For each sender it adds a single
handler to the event, while there are subscribers, and forwards the
events to the subscribers that are still alive. The sender holds the
subscription; neither the sender nor the subscription holds the
subscribers.

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/weak_event.rs#L95)

## Members

- [`register`](#function-register)
- [`register_returning_unsubscribe`](#function-register_returning_unsubscribe)
- [`subscribe`](#function-subscribe)
- [`unsubscribe`](#function-unsubscribe)
- [`register_plain`](#function-register_plain)

<a id="function-register"></a>

### register

```rust
fn register<Sub, Unsub>(subscribe: Sub, unsubscribe: Unsub) -> Rc<Self>
where Sub: Fn(&S, &Rc<WeakEventHandler<A>>) -> () + 'static,
      Unsub: Fn(&S, &Rc<WeakEventHandler<A>>) -> () + 'static
```

Registers an event through the functions that add a handler to it
and remove the handler from it.

The managed original removes the handler through the sender it
captured; here the removal holds the sender weakly (a strong
reference would keep it alive through its own subscription), so the
handler is not removed from a sender that no longer exists. It also
holds the handler weakly: the handler holds the subscription, which
holds the removal, and the sender owns the handler.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/weak_event.rs#L130)

<a id="function-register_returning_unsubscribe"></a>

### register_returning_unsubscribe

```rust
fn register_returning_unsubscribe<Sub>(subscribe: Sub) -> Rc<Self>
where Sub: Fn(&S, Rc<WeakEventHandler<A>>) -> Box<dyn FnOnce() -> ()> + 'static
```

Registers an event through a function that adds a handler to it and
returns the action that removes the handler.

The action must not hold the sender strongly: the sender holds the
subscription, and the subscription holds the action.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/weak_event.rs#L154)

<a id="function-subscribe"></a>

### subscribe

```rust
fn subscribe(self: &Self, target: &S, subscriber: &Rc<dyn IWeakEventSubscriber<A>>)
```



[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/weak_event.rs#L161)

<a id="function-unsubscribe"></a>

### unsubscribe

```rust
fn unsubscribe(self: &Self, target: &S, subscriber: &Rc<dyn IWeakEventSubscriber<A>>)
```



[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/weak_event.rs#L170)

<a id="function-register_plain"></a>

### register_plain

```rust
fn register_plain<Sub, Unsub>(subscribe: Sub, unsubscribe: Unsub) -> Rc<Self>
where Sub: Fn(&S, &Rc<WeakEventHandler<EventArgs>>) -> () + 'static,
      Unsub: Fn(&S, &Rc<WeakEventHandler<EventArgs>>) -> () + 'static
```

Registers an event whose handlers take the plain event arguments,
through the functions that add a handler to it and remove the
handler from it.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/weak_event.rs#L203)

## Trait implementations

- `Send`
- `Sync`
- `Freeze`
- `UnsafeUnpin`
- `Unpin`
- `UnwindSafe`
- `RefUnwindSafe`
- [`ClassDefaults`](/docs/0.5.0/api/ferroui-base/trait-classdefaults/)
- `Borrow<T>`
- `BorrowMut<T>`
- `Into<U>`
- `From<T>`
- `TryInto<U>`
- `TryFrom<U>`
- `Any`
- `ErasedDestructor`
- `IntoEither`

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
