use super::{
super::{abstract_event::AbstractEventRef, event::Event},
event_index::EventIndex,
};
use crate::{id::PublicId, observation::ObservationKey, peer_list::PeerIndex};
use std::{
cmp::{Ord, Ordering, PartialOrd},
ops::Deref,
};
#[derive(Clone, Debug)]
pub(crate) struct IndexedEventRef<'a, P: PublicId + 'a> {
pub(super) index: EventIndex,
pub(super) event: &'a Event<P>,
}
impl<'a, P: PublicId> Copy for IndexedEventRef<'a, P> {}
impl<'a, P: PublicId> IndexedEventRef<'a, P> {
pub fn event_index(&self) -> EventIndex {
self.index
}
pub fn topological_index(&self) -> usize {
self.index.topological_index()
}
pub fn inner(&self) -> &'a Event<P> {
self.event
}
}
impl<'a, P: PublicId> Deref for IndexedEventRef<'a, P> {
type Target = Event<P>;
fn deref(&self) -> &Self::Target {
self.event
}
}
impl<'a, P: PublicId> AsRef<Event<P>> for IndexedEventRef<'a, P> {
fn as_ref(&self) -> &Event<P> {
self.event
}
}
impl<'a, P: PublicId> PartialEq for IndexedEventRef<'a, P> {
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl<'a, P: PublicId> Eq for IndexedEventRef<'a, P> {}
impl<'a, P: PublicId> PartialOrd for IndexedEventRef<'a, P> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.index.partial_cmp(&other.index)
}
}
impl<'a, P: PublicId> Ord for IndexedEventRef<'a, P> {
fn cmp(&self, other: &Self) -> Ordering {
self.index.cmp(&other.index)
}
}
impl<'a, P: PublicId> AbstractEventRef<'a> for IndexedEventRef<'a, P> {
fn payload_key(self) -> Option<&'a ObservationKey> {
self.event.payload_key()
}
fn creator(self) -> PeerIndex {
self.event.creator()
}
fn index_by_creator(self) -> usize {
self.event.index_by_creator()
}
}