Added parent accessor to Ref and OwnedRef

Fortune for parapop's current commit: Small curse − 小凶
master
Avril 2 years ago
parent 9a30ad5665
commit 7748572f79
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -5,6 +5,7 @@ use std::sync::atomic::{
AtomicBool,
AtomicUsize,
};
use std::sync::Arc;
use std::cmp::Ordering;
use std::mem::{self, MaybeUninit};
use std::cell::UnsafeCell;
@ -227,12 +228,12 @@ impl<'a, T> Populator<'a, T>
}
}
/// Consume into a reference to a specific index in the populator, whether it exists or not.
/// Consume an atomic reference-counted instance of a populator array into a reference to a specific index in the populator, whether it exists or not.
///
/// # Panics
/// If the index is out of bounds
#[inline]
pub fn into_ref(self: std::sync::Arc<Self>, idx: usize) -> OwnedRef<'a, T>
pub fn into_ref(self: Arc<Self>, idx: usize) -> OwnedRef<'a, T>
{
OwnedRef {
pop: self,

@ -1,8 +1,7 @@
//! Single-index references.
use super::*;
use std::sync::Arc;
#[derive(Debug)] // PartialEq, PartialOrd
#[derive(Debug)] // PartialEq, PartialOrd ///XXX: TODO: Should this be Clone (and maybe even Copy)? Check OwnedRef below...
pub struct Ref<'re, 'a, T: 'a>
{
pub(super) pop: &'re Populator<'a, T>,
@ -11,7 +10,7 @@ pub struct Ref<'re, 'a, T: 'a>
//TODO: OR: Hold a reference to the actual AtomicBool at `idx` itself?
}
#[derive(Debug)]
#[derive(Debug, Clone)] // PartialEq, PartialOrd //XXX: TODO: Should this actually be `Clone`? Ref isn't, because its supposed to be a single reference. But since this is arc'd?
pub struct OwnedRef<'a, T: 'a> // PartialEq, PartialOrd
{
pub(super) pop: Arc<Populator<'a, T>>,
@ -63,6 +62,12 @@ impl<'re, 'a, T: 'a> PartialOrd for Ref<'re, 'a, T>
impl<'re, 'a, T: 'a> Ref<'re, 'a, T>
{
/// Get a reference to the parent populator
#[inline]
pub fn parent(&self) -> &Populator<'a, T>
{
&self.pop
}
/// The index that this `Ref` refers to.
#[inline]
pub fn slot(&self) -> usize
@ -92,10 +97,30 @@ impl<'re, 'a, T: 'a> Ref<'re, 'a, T>
{
self.pop.insert(self.idx, value)
}
/// Consume into the inner populator reference and slot index
#[inline]
pub fn into_parts(self) -> (&'re Populator<'a, T>, usize)
{
(self.pop, self.idx)
}
/// Consume into the inner populator reference
#[inline]
pub fn into_inner(self) -> &'re Populator<'a, T>
{
self.pop
}
}
impl<'a, T:'a> OwnedRef<'a, T>
{
/// Get a reference to the parent `Arc`.
#[inline]
pub fn parent(&self) -> &Arc<Populator<'a, T>>
{
&self.pop
}
/// The index that this `Ref` refers to.
#[inline]
pub fn slot(&self) -> usize
@ -126,6 +151,13 @@ impl<'a, T:'a> OwnedRef<'a, T>
self.pop.insert(self.idx, value)
}
/// Consume into the inner populator and slot index
#[inline]
pub fn into_parts(self) -> (Arc<Populator<'a, T>>, usize)
{
(self.pop, self.idx)
}
/// Consume into the inner populator
#[inline]
pub fn into_inner(self) -> Arc<Populator<'a, T>>

Loading…
Cancel
Save