Skip to content

Commit

Permalink
Simplify shared-from-this pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
The0x539 committed Mar 9, 2024
1 parent 6eddaa3 commit 93bc468
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
27 changes: 1 addition & 26 deletions src/global_safety.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -53,30 +52,6 @@ impl<T: ?Sized> AtomicRef<T> {
}
}

pub struct SharedFromThisBase<T> {
weak: RefCell<Weak<T>>,
}

impl<T> SharedFromThisBase<T> {
pub fn new() -> SharedFromThisBase<T> {
SharedFromThisBase {
weak: RefCell::new(Weak::new()),
}
}

pub fn initialize(&self, r: &Rc<T>) {
*self.weak.borrow_mut() = Rc::downgrade(r);
}
}

pub trait SharedFromThis<T> {
fn get_base(&self) -> &SharedFromThisBase<T>;

fn shared_from_this(&self) -> Rc<T> {
self.get_base().weak.borrow().upgrade().unwrap()
}
}

pub struct DebugRef<'a, T>(Ref<'a, T>);

impl<'a, T> DebugRef<'a, T> {
Expand Down
19 changes: 6 additions & 13 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -295,7 +295,7 @@ pub type BlockId = usize;
pub type ParserRef = Rc<Parser>;

pub struct Parser {
base: SharedFromThisBase<Parser>,
this: Weak<Self>,

/// The current execution context.
execution_context: RefCell<Option<ParseExecutionContext>>,
Expand Down Expand Up @@ -335,17 +335,11 @@ pub struct Parser {
pub global_event_blocks: AtomicU64,
}

impl SharedFromThis<Parser> for Parser {
fn get_base(&self) -> &SharedFromThisBase<Parser> {
&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>| Self {
this: Weak::clone(this),
execution_context: RefCell::default(),
job_list: RefCell::default(),
wait_handles: RefCell::new(WaitHandleStore::new()),
Expand All @@ -372,7 +366,6 @@ impl Parser {
}
}

result.base.initialize(&result);
result
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 93bc468

Please sign in to comment.