[−][src]Struct pharos::Pharos
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.
Methods
impl<Event> Pharos<Event> where
Event: 'static + Clone + Send,
[src]
Event: 'static + Clone + Send,
pub fn new(capacity: usize) -> Self
[src]
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
.
pub fn storage_len(&self) -> usize
[src]
Returns the size of the vector used to store the observers. Useful for debugging and testing if it seems to get to big.
pub fn num_observers(&mut self) -> usize
[src]
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
impl<Event> Observable<Event> for Pharos<Event> where
Event: 'static + Clone + Send,
[src]
Event: 'static + Clone + Send,
type Error = Error
The error type that is returned if observing is not possible. Read more
fn observe(
&mut self,
options: ObserveConfig<Event>
) -> Result<Events<Event>, Self::Error>
[src]
&mut self,
options: ObserveConfig<Event>
) -> Result<Events<Event>, Self::Error>
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.
impl<Event> Default for Pharos<Event> where
Event: 'static + Clone + Send,
[src]
Event: 'static + Clone + Send,
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.
impl<Event> Debug for Pharos<Event> where
Event: 'static + Clone + Send,
[src]
Event: 'static + Clone + Send,
impl<Event> Sink<Event> for Pharos<Event> where
Event: Clone + 'static + Send,
[src]
Event: Clone + 'static + Send,
type Error = Error
The type of value produced by the sink when an error occurs.
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
fn start_send(self: Pin<&mut Self>, evt: Event) -> Result<(), Self::Error>
[src]
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context
) -> Poll<Result<(), Self::Error>>
Will close and drop all observers. The pharos object will remain operational however. The main annoyance would be that we'd have to make
Auto Trait Implementations
impl<Event> Send for Pharos<Event>
impl<Event> !Sync for Pharos<Event>
impl<Event> Unpin for Pharos<Event>
impl<Event> !UnwindSafe for Pharos<Event>
impl<Event> !RefUnwindSafe for Pharos<Event>
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T, Item> SinkExt<Item> for T where
T: Sink<Item> + ?Sized,
[src]
T: Sink<Item> + ?Sized,
fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F> where
E: From<Self::Error>,
F: FnMut(U) -> Fut,
Fut: Future<Output = Result<Item, E>>,
[src]
E: From<Self::Error>,
F: FnMut(U) -> Fut,
Fut: Future<Output = Result<Item, E>>,
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>>,
[src]
F: FnMut(U) -> St,
St: Stream<Item = Result<Item, Self::Error>>,
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F> where
F: FnOnce(Self::Error) -> E,
[src]
F: FnOnce(Self::Error) -> E,
fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E> where
Self::Error: Into<E>,
[src]
Self::Error: Into<E>,
fn buffer(self, capacity: usize) -> Buffer<Self, Item>
[src]
fn close(&mut self) -> Close<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
fn fanout<Si>(self, other: Si) -> Fanout<Self, Si> where
Item: Clone,
Si: Sink<Item, Error = Self::Error>,
[src]
Item: Clone,
Si: Sink<Item, Error = Self::Error>,
fn flush(&mut self) -> Flush<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
fn send(&mut self, item: Item) -> Send<Self, Item> where
Self: Unpin,
[src]
Self: Unpin,
fn send_all<St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St> where
Self: Unpin,
St: TryStream<Ok = Item, Error = Self::Error> + Stream + Unpin + ?Sized,
[src]
Self: Unpin,
St: TryStream<Ok = Item, Error = Self::Error> + Stream + Unpin + ?Sized,
fn left_sink<Si2>(self) -> Either<Self, Si2> where
Si2: Sink<Item, Error = Self::Error>,
[src]
Si2: Sink<Item, Error = Self::Error>,
fn right_sink<Si1>(self) -> Either<Si1, Self> where
Si1: Sink<Item, Error = Self::Error>,
[src]
Si1: Sink<Item, Error = Self::Error>,