From 7748572f791ad5cd81ed0ae5642445a15f0d7ccf Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 21 Jan 2022 23:47:18 +0000 Subject: [PATCH] Added parent accessor to Ref and OwnedRef MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for parapop's current commit: Small curse − 小凶 --- src/lib.rs | 5 +++-- src/refer.rs | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dce7ed7..ae623c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, idx: usize) -> OwnedRef<'a, T> + pub fn into_ref(self: Arc, idx: usize) -> OwnedRef<'a, T> { OwnedRef { pop: self, diff --git a/src/refer.rs b/src/refer.rs index 957777c..09b1dce 100644 --- a/src/refer.rs +++ b/src/refer.rs @@ -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>, @@ -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> + { + &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>, usize) + { + (self.pop, self.idx) + } + /// Consume into the inner populator #[inline] pub fn into_inner(self) -> Arc>