@ -1,5 +1,6 @@
//! Single-index references.
use super ::* ;
use std ::sync ::Arc ;
#[ derive(Debug) ] // PartialEq, PartialOrd
pub struct Ref < ' re , ' a , T : ' a >
@ -10,6 +11,34 @@ pub struct Ref<'re, 'a, T: 'a>
//TODO: OR: Hold a reference to the actual AtomicBool at `idx` itself?
}
#[ derive(Debug) ]
pub struct OwnedRef < ' a , T : ' a > // PartialEq, PartialOrd
{
pub ( super ) pop : Arc < Populator < ' a , T > > ,
pub ( super ) idx : usize ,
}
impl < ' a , T : ' a > PartialEq for OwnedRef < ' a , T >
{
#[ inline ]
fn eq ( & self , other : & Self ) -> bool
{
address_eq ( self . pop . as_ref ( ) , other . pop . as_ref ( ) ) & & self . idx = = other . idx
}
}
impl < ' a , T : ' a > PartialOrd for OwnedRef < ' a , T >
{
#[ inline ]
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
if address_eq ( self . pop . as_ref ( ) , other . pop . as_ref ( ) ) {
self . idx . partial_cmp ( & other . idx )
} else {
None
}
}
}
impl < ' re , ' a , T : ' a > PartialEq for Ref < ' re , ' a , T >
{
@ -63,8 +92,65 @@ impl<'re, 'a, T: 'a> Ref<'re, 'a, T>
{
self . pop . insert ( self . idx , value )
}
}
impl < ' a , T :' a > OwnedRef < ' a , T >
{
/// The index that this `Ref` refers to.
#[ inline ]
pub fn slot ( & self ) -> usize
{
self . idx
}
/// Checks if the references item currently exists.
#[ inline ]
pub fn exists ( & self ) -> bool
{
self . pop . exists ( self . idx )
}
/// Try to insert `value` at the referred slot.
///
/// If the slot already has a value, then `Err(value)` is returned, otherwise, `value` is inserted into the table and the number of items now populated is returned.
#[ inline ]
pub fn try_insert ( & self , value : T ) -> Result < usize , T >
{
self . pop . try_insert ( self . idx , value )
}
/// Insert `value` at the referred slot.
///
/// # Panics
/// If the slot has a value.
#[ inline ]
pub fn insert ( & self , value : T ) -> usize
{
self . pop . insert ( self . idx , value )
}
/// Consume into the inner populator
#[ inline ]
pub fn into_inner ( self ) -> Arc < Populator < ' a , T > >
{
self . pop
}
}
impl < ' a , T : ' a > From < ( Arc < Populator < ' a , T > > , usize ) > for OwnedRef < ' a , T >
{
#[ inline ]
fn from ( ( pop , idx ) : ( Arc < Populator < ' a , T > > , usize ) ) -> Self
{
Self { pop , idx }
}
}
impl < ' a , T : ' a > From < OwnedRef < ' a , T > > for ( Arc < Populator < ' a , T > > , usize )
{
#[ inline ]
fn from ( from : OwnedRef < ' a , T > ) -> Self
{
( from . pop , from . idx )
}
}
//TODO: OwnedRef: From Arc<Self> receiver of Arc<Populator<'static, T>>
//TODO: RefEx: Exclusive reference, holds &'ref mut Populator<'a, T>
//TODO: RefEx: Exclusive reference, holds &'re mut Populator<'a, T>