Struct pharos::Pharos[][src]

pub struct Pharos<Event> where
    Event: 'static + Clone + Send
{ /* fields omitted */ }
Expand description

The Pharos lighthouse. When you implement Observable on your type, you can forward the observe method to Pharos and use SinkExt::send to notify observers.

You can of course create several Pharos (I know, historical sacrilege) for (different) types of events.

Please see the docs for Observable for an example. Others can be found in the README and the examples directory of the repository.

Implementation.

Currently just holds a Vec<Option<Sender>>. It will drop observers if the channel has returned an error, which means it is closed or disconnected. However, we currently don’t compact the vector. Slots are reused for new observers, but the vector never shrinks.

Note: we only detect that observers can be removed when SinkExt::send or Pharos::num_observers is being called. Otherwise, we won’t find out about disconnected observers and the vector of observers will not mark deleted observers and thus their slots can not be reused.

The Sink impl is not very optimized for the moment. It just loops over all observers in each poll method so it will call poll_ready and poll_flush again for observers that already returned Poll::Ready(Ok(())).

TODO: I will do some benchmarking and see if this can be improved, eg. by keeping a state which tracks which observers we still have to poll.

Implementations

Create a new Pharos. May it’s light guide you to safe harbor.

You can set the initial capacity of the vector of observers, if you know you will a lot of observers it will save allocations by setting this to a higher number.

For pharos 0.4.0 on x64 Linux: std::mem::size_of::<Option<Sender<_>>>() == 56 bytes.

Returns the size of the vector used to store the observers. Useful for debugging and testing if it seems to get to big.

Returns the number of actual observers that are still listening (have not closed or dropped the Events). This will loop and it will verify for each if they are closed, clearing them from the internal storage if they are closed. This is similar to what notify does, but without sending an event.

Trait Implementations

Formats the value using the given formatter. Read more

Creates a new pharos, using 10 as the initial capacity of the vector used to store observers. If this number does really not fit your use case, call Pharos::new.

Returns the “default value” for a type. Read more

Will re-use slots from disconnected observers to avoid growing to much.

TODO: provide API for the client to compact the pharos object after reducing the number of observers.

The error type that is returned if observing is not possible. Read more

Will close and drop all observers.

The type of value produced by the sink when an error occurs.

Attempts to prepare the Sink to receive a value. Read more

Begin the process of sending a value to the sink. Each call to this function must be preceded by a successful call to poll_ready which returned Poll::Ready(Ok(())). Read more

Flush any remaining output from this sink. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Composes a function in front of the sink. Read more

Composes a function in front of the sink. Read more

Transforms the error returned by the sink.

Map this sink’s error to a different error type using the Into trait. Read more

This is supported on crate feature alloc only.

Adds a fixed-size buffer to the current sink. Read more

Close the sink.

Fanout items to multiple sinks. Read more

Flush the sink, processing all pending items. Read more

A future that completes after the given item has been fully processed into the sink, including flushing. Read more

A future that completes after the given item has been received by the sink. Read more

A future that completes after the given stream has been fully processed into the sink, including flushing. Read more

Wrap this sink in an Either sink, making it the left-hand variant of that Either. Read more

Wrap this stream in an Either stream, making it the right-hand variant of that Either. Read more

A convenience method for calling [Sink::poll_ready] on Unpin sink types. Read more

A convenience method for calling [Sink::start_send] on Unpin sink types. Read more

A convenience method for calling [Sink::poll_flush] on Unpin sink types. Read more

A convenience method for calling [Sink::poll_close] on Unpin sink types. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.