pub use super::bucket::{Node, NodeStatus, InsertResult, AppliedPending, MAX_NODES_PER_BUCKET};
pub use super::key::*;
use super::*;
pub struct EntryRefView<'a, TPeerId, TVal> {
pub node: NodeRefView<'a, TPeerId, TVal>,
pub status: NodeStatus
}
pub struct NodeRefView<'a, TPeerId, TVal> {
pub key: &'a Key<TPeerId>,
pub value: &'a TVal
}
impl<TPeerId, TVal> EntryRefView<'_, TPeerId, TVal> {
pub fn to_owned(&self) -> EntryView<TPeerId, TVal>
where
TPeerId: Clone,
TVal: Clone
{
EntryView {
node: Node {
key: self.node.key.clone(),
value: self.node.value.clone()
},
status: self.status
}
}
}
#[derive(Clone, Debug)]
pub struct EntryView<TPeerId, TVal> {
pub node: Node<TPeerId, TVal>,
pub status: NodeStatus
}
impl<TPeerId, TVal> AsRef<Key<TPeerId>> for EntryView<TPeerId, TVal> {
fn as_ref(&self) -> &Key<TPeerId> {
&self.node.key
}
}
#[derive(Debug)]
pub enum Entry<'a, TPeerId, TVal> {
Present(PresentEntry<'a, TPeerId, TVal>, NodeStatus),
Pending(PendingEntry<'a, TPeerId, TVal>, NodeStatus),
Absent(AbsentEntry<'a, TPeerId, TVal>),
SelfEntry,
}
#[derive(Debug)]
struct EntryRef<'a, TPeerId, TVal> {
bucket: &'a mut KBucket<TPeerId, TVal>,
key: &'a Key<TPeerId>,
}
impl<'a, TPeerId, TVal> Entry<'a, TPeerId, TVal>
where
TPeerId: Clone,
{
pub(super) fn new(bucket: &'a mut KBucket<TPeerId, TVal>, key: &'a Key<TPeerId>) -> Self {
if let Some(pos) = bucket.position(key) {
let status = bucket.status(pos);
Entry::Present(PresentEntry::new(bucket, key), status)
} else if let Some(pending) = bucket.as_pending(key) {
let status = pending.status();
Entry::Pending(PendingEntry::new(bucket, key), status)
} else {
Entry::Absent(AbsentEntry::new(bucket, key))
}
}
pub fn view(&'a mut self) -> Option<EntryRefView<'a, TPeerId, TVal>> {
match self {
Entry::Present(entry, status) => Some(EntryRefView {
node: NodeRefView {
key: entry.0.key,
value: entry.value()
},
status: *status
}),
Entry::Pending(entry, status) => Some(EntryRefView {
node: NodeRefView {
key: entry.0.key,
value: entry.value()
},
status: *status
}),
_ => None
}
}
pub fn key(&self) -> Option<&Key<TPeerId>> {
match self {
Entry::Present(entry, _) => Some(entry.key()),
Entry::Pending(entry, _) => Some(entry.key()),
Entry::Absent(entry) => Some(entry.key()),
Entry::SelfEntry => None,
}
}
pub fn value(&mut self) -> Option<&mut TVal> {
match self {
Entry::Present(entry, _) => Some(entry.value()),
Entry::Pending(entry, _) => Some(entry.value()),
Entry::Absent(_) => None,
Entry::SelfEntry => None,
}
}
}
#[derive(Debug)]
pub struct PresentEntry<'a, TPeerId, TVal>(EntryRef<'a, TPeerId, TVal>);
impl<'a, TPeerId, TVal> PresentEntry<'a, TPeerId, TVal>
where
TPeerId: Clone,
{
fn new(bucket: &'a mut KBucket<TPeerId, TVal>, key: &'a Key<TPeerId>) -> Self {
PresentEntry(EntryRef { bucket, key })
}
pub fn key(&self) -> &Key<TPeerId> {
self.0.key
}
pub fn value(&mut self) -> &mut TVal {
&mut self.0.bucket
.get_mut(self.0.key)
.expect("We can only build a ConnectedEntry if the entry is in the bucket; QED")
.value
}
pub fn update(self, status: NodeStatus) -> Self {
self.0.bucket.update(self.0.key, status);
Self::new(self.0.bucket, self.0.key)
}
}
#[derive(Debug)]
pub struct PendingEntry<'a, TPeerId, TVal>(EntryRef<'a, TPeerId, TVal>);
impl<'a, TPeerId, TVal> PendingEntry<'a, TPeerId, TVal>
where
TPeerId: Clone,
{
fn new(bucket: &'a mut KBucket<TPeerId, TVal>, key: &'a Key<TPeerId>) -> Self {
PendingEntry(EntryRef { bucket, key })
}
pub fn key(&self) -> &Key<TPeerId> {
self.0.key
}
pub fn value(&mut self) -> &mut TVal {
self.0.bucket
.pending_mut()
.expect("We can only build a ConnectedPendingEntry if the entry is pending; QED")
.value_mut()
}
pub fn update(self, status: NodeStatus) -> PendingEntry<'a, TPeerId, TVal> {
self.0.bucket.update_pending(status);
PendingEntry::new(self.0.bucket, self.0.key)
}
}
#[derive(Debug)]
pub struct AbsentEntry<'a, TPeerId, TVal>(EntryRef<'a, TPeerId, TVal>);
impl<'a, TPeerId, TVal> AbsentEntry<'a, TPeerId, TVal>
where
TPeerId: Clone,
{
fn new(bucket: &'a mut KBucket<TPeerId, TVal>, key: &'a Key<TPeerId>) -> Self {
AbsentEntry(EntryRef { bucket, key })
}
pub fn key(&self) -> &Key<TPeerId> {
self.0.key
}
pub fn insert(self, value: TVal, status: NodeStatus) -> InsertResult<TPeerId> {
self.0.bucket.insert(Node {
key: self.0.key.clone(),
value
}, status)
}
}