Skip to content

Commit 1de7ebc

Browse files
The0x539krobelus
authored andcommitted
Simplify shared-from-this pattern
1 parent e5f83cd commit 1de7ebc

File tree

2 files changed

+7
-39
lines changed

2 files changed

+7
-39
lines changed

src/global_safety.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::flog::FLOG;
2-
use std::cell::{Ref, RefCell, RefMut};
3-
use std::rc::{Rc, Weak};
2+
use std::cell::{Ref, RefMut};
43
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
54
use std::sync::MutexGuard;
65

@@ -53,30 +52,6 @@ impl<T: ?Sized> AtomicRef<T> {
5352
}
5453
}
5554

56-
pub struct SharedFromThisBase<T> {
57-
weak: RefCell<Weak<T>>,
58-
}
59-
60-
impl<T> SharedFromThisBase<T> {
61-
pub fn new() -> SharedFromThisBase<T> {
62-
SharedFromThisBase {
63-
weak: RefCell::new(Weak::new()),
64-
}
65-
}
66-
67-
pub fn initialize(&self, r: &Rc<T>) {
68-
*self.weak.borrow_mut() = Rc::downgrade(r);
69-
}
70-
}
71-
72-
pub trait SharedFromThis<T> {
73-
fn get_base(&self) -> &SharedFromThisBase<T>;
74-
75-
fn shared_from_this(&self) -> Rc<T> {
76-
self.get_base().weak.borrow().upgrade().unwrap()
77-
}
78-
}
79-
8055
pub struct DebugRef<'a, T>(Ref<'a, T>);
8156

8257
impl<'a, T> DebugRef<'a, T> {

src/parser.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::expand::{
1515
use crate::fds::open_cloexec;
1616
use crate::flog::FLOGF;
1717
use crate::function;
18-
use crate::global_safety::{RelaxedAtomicBool, SharedFromThis, SharedFromThisBase};
18+
use crate::global_safety::RelaxedAtomicBool;
1919
use crate::io::IoChain;
2020
use crate::job_group::MaybeJobId;
2121
use crate::operation_context::{OperationContext, EXPANSION_LIMIT_DEFAULT};
@@ -42,7 +42,7 @@ use std::ffi::{CStr, OsStr};
4242
use std::os::fd::{AsRawFd, OwnedFd, RawFd};
4343
use std::os::unix::prelude::OsStrExt;
4444
use std::pin::Pin;
45-
use std::rc::Rc;
45+
use std::rc::{Rc, Weak};
4646
use std::sync::{
4747
atomic::{AtomicIsize, AtomicU64, Ordering},
4848
Arc,
@@ -295,7 +295,7 @@ pub type BlockId = usize;
295295
pub type ParserRef = Rc<Parser>;
296296

297297
pub struct Parser {
298-
base: SharedFromThisBase<Parser>,
298+
this: Weak<Self>,
299299

300300
/// The current execution context.
301301
execution_context: RefCell<Option<ParseExecutionContext>>,
@@ -335,17 +335,11 @@ pub struct Parser {
335335
pub global_event_blocks: AtomicU64,
336336
}
337337

338-
impl SharedFromThis<Parser> for Parser {
339-
fn get_base(&self) -> &SharedFromThisBase<Parser> {
340-
&self.base
341-
}
342-
}
343-
344338
impl Parser {
345339
/// Create a parser
346340
pub fn new(variables: EnvStackRef, is_principal: bool) -> ParserRef {
347-
let result = Rc::new(Self {
348-
base: SharedFromThisBase::new(),
341+
let result = Rc::new_cyclic(|this: &Weak<Self>| Self {
342+
this: Weak::clone(this),
349343
execution_context: RefCell::default(),
350344
job_list: RefCell::default(),
351345
wait_handles: RefCell::new(WaitHandleStore::new()),
@@ -372,7 +366,6 @@ impl Parser {
372366
}
373367
}
374368

375-
result.base.initialize(&result);
376369
result
377370
}
378371

@@ -1075,7 +1068,7 @@ impl Parser {
10751068

10761069
/// \return a shared pointer reference to this parser.
10771070
pub fn shared(&self) -> ParserRef {
1078-
self.shared_from_this()
1071+
self.this.upgrade().unwrap()
10791072
}
10801073

10811074
/// \return the operation context for this parser.

0 commit comments

Comments
 (0)