Struct pharos::Pharos [−][src]
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
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.
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.
Auto Trait Implementations
impl<Event> !RefUnwindSafe for Pharos<Event>
impl<Event> !UnwindSafe for Pharos<Event>
Blanket Implementations
Mutably borrows from an owned value. Read more
impl<T, Item> SinkExt<Item> for T where
T: Sink<Item> + ?Sized,
impl<T, Item> SinkExt<Item> for T where
T: Sink<Item> + ?Sized,
Composes a function in front of the sink. Read more
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F> where
F: FnMut(U) -> St,
St: Stream<Item = Result<Item, Self::Error>>,
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F> where
F: FnMut(U) -> St,
St: Stream<Item = Result<Item, Self::Error>>,
Composes a function in front of the sink. Read more
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F> where
F: FnOnce(Self::Error) -> E,
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F> where
F: FnOnce(Self::Error) -> E,
Transforms the error returned by the sink.
fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E> where
Self::Error: Into<E>,
fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E> where
Self::Error: Into<E>,
Map this sink’s error to a different error type using the Into
trait. Read more
alloc
only.Adds a fixed-size buffer to the current sink. Read more
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
fn left_sink<Si2>(self) -> Either<Self, Si2> where
Si2: Sink<Item, Error = Self::Error>,
fn left_sink<Si2>(self) -> Either<Self, Si2> where
Si2: Sink<Item, Error = Self::Error>,
Wrap this sink in an Either
sink, making it the left-hand variant
of that Either
. Read more
fn right_sink<Si1>(self) -> Either<Si1, Self> where
Si1: Sink<Item, Error = Self::Error>,
fn right_sink<Si1>(self) -> Either<Si1, Self> where
Si1: Sink<Item, Error = Self::Error>,
Wrap this stream in an Either
stream, making it the right-hand variant
of that Either
. Read more