From 93bc468669550a5a550f2588f3725415252478e0 Mon Sep 17 00:00:00 2001 From: The0x539 Date: Fri, 8 Mar 2024 18:08:55 -0600 Subject: [PATCH] Simplify shared-from-this pattern --- src/global_safety.rs | 27 +-------------------------- src/parser.rs | 19 ++++++------------- 2 files changed, 7 insertions(+), 39 deletions(-) diff --git a/src/global_safety.rs b/src/global_safety.rs index 95d68e3e46fd..94936da643d1 100644 --- a/src/global_safety.rs +++ b/src/global_safety.rs @@ -1,6 +1,5 @@ use crate::flog::FLOG; -use std::cell::{Ref, RefCell, RefMut}; -use std::rc::{Rc, Weak}; +use std::cell::{Ref, RefMut}; use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering}; use std::sync::MutexGuard; @@ -53,30 +52,6 @@ impl AtomicRef { } } -pub struct SharedFromThisBase { - weak: RefCell>, -} - -impl SharedFromThisBase { - pub fn new() -> SharedFromThisBase { - SharedFromThisBase { - weak: RefCell::new(Weak::new()), - } - } - - pub fn initialize(&self, r: &Rc) { - *self.weak.borrow_mut() = Rc::downgrade(r); - } -} - -pub trait SharedFromThis { - fn get_base(&self) -> &SharedFromThisBase; - - fn shared_from_this(&self) -> Rc { - self.get_base().weak.borrow().upgrade().unwrap() - } -} - pub struct DebugRef<'a, T>(Ref<'a, T>); impl<'a, T> DebugRef<'a, T> { diff --git a/src/parser.rs b/src/parser.rs index eeec59299f22..ffaf89479d66 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -15,7 +15,7 @@ use crate::expand::{ use crate::fds::open_cloexec; use crate::flog::FLOGF; use crate::function; -use crate::global_safety::{RelaxedAtomicBool, SharedFromThis, SharedFromThisBase}; +use crate::global_safety::RelaxedAtomicBool; use crate::io::IoChain; use crate::job_group::MaybeJobId; use crate::operation_context::{OperationContext, EXPANSION_LIMIT_DEFAULT}; @@ -42,7 +42,7 @@ use std::ffi::{CStr, OsStr}; use std::os::fd::{AsRawFd, OwnedFd, RawFd}; use std::os::unix::prelude::OsStrExt; use std::pin::Pin; -use std::rc::Rc; +use std::rc::{Rc, Weak}; use std::sync::{ atomic::{AtomicIsize, AtomicU64, Ordering}, Arc, @@ -295,7 +295,7 @@ pub type BlockId = usize; pub type ParserRef = Rc; pub struct Parser { - base: SharedFromThisBase, + this: Weak, /// The current execution context. execution_context: RefCell>, @@ -335,17 +335,11 @@ pub struct Parser { pub global_event_blocks: AtomicU64, } -impl SharedFromThis for Parser { - fn get_base(&self) -> &SharedFromThisBase { - &self.base - } -} - impl Parser { /// Create a parser pub fn new(variables: EnvStackRef, is_principal: bool) -> ParserRef { - let result = Rc::new(Self { - base: SharedFromThisBase::new(), + let result = Rc::new_cyclic(|this: &Weak| Self { + this: Weak::clone(this), execution_context: RefCell::default(), job_list: RefCell::default(), wait_handles: RefCell::new(WaitHandleStore::new()), @@ -372,7 +366,6 @@ impl Parser { } } - result.base.initialize(&result); result } @@ -1075,7 +1068,7 @@ impl Parser { /// \return a shared pointer reference to this parser. pub fn shared(&self) -> ParserRef { - self.shared_from_this() + self.this.upgrade().unwrap() } /// \return the operation context for this parser.