Skip to content

Commit

Permalink
Change Rc's to Arc's (#35)
Browse files Browse the repository at this point in the history
Arc's are better
  • Loading branch information
Vandesm14 authored May 26, 2024
1 parent f2828f8 commit 1896261
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions stack-core/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use core::cell::RefCell;
use std::{borrow::BorrowMut, rc::Rc};
use std::{borrow::BorrowMut, sync::Arc};

#[derive(Debug, PartialEq, Clone)]
pub struct Chain<T> {
value: Rc<RefCell<T>>,
child: Option<Rc<RefCell<Chain<T>>>>,
value: Arc<RefCell<T>>,
child: Option<Arc<RefCell<Chain<T>>>>,
root: bool,
}

impl<T> Chain<T> {
pub fn new(value: T) -> Self {
Self {
value: Rc::new(RefCell::new(value)),
value: Arc::new(RefCell::new(value)),
child: None,
root: true,
}
}

pub fn link(&mut self) -> Rc<RefCell<Self>> {
let child = Rc::new(RefCell::new(Self {
pub fn link(&mut self) -> Arc<RefCell<Self>> {
let child = Arc::new(RefCell::new(Self {
value: self.value.clone(),
child: None,
root: false,
Expand All @@ -28,7 +28,7 @@ impl<T> Chain<T> {
child
}

pub fn root(&self) -> Rc<RefCell<T>> {
pub fn root(&self) -> Arc<RefCell<T>> {
self.value.clone()
}

Expand All @@ -45,7 +45,7 @@ where
self.value.borrow().clone()
}

fn unlink_with_rc(&mut self, value: Rc<RefCell<T>>, new_root: bool) {
fn unlink_with_rc(&mut self, value: Arc<RefCell<T>>, new_root: bool) {
let mut new_root = new_root;

if new_root {
Expand All @@ -61,7 +61,7 @@ where
}

pub fn unlink_with(&mut self, val: T) {
self.unlink_with_rc(Rc::new(RefCell::new(val)), true);
self.unlink_with_rc(Arc::new(RefCell::new(val)), true);
}

pub fn set(&mut self, val: T) {
Expand Down
4 changes: 2 additions & 2 deletions stack-core/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use std::{cell::RefCell, collections::HashMap, sync::Arc};

use crate::{
chain::Chain,
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Context {
#[inline]
pub fn scope_items(
&self,
) -> impl Iterator<Item = (&Symbol, &Rc<RefCell<Chain<Option<Expr>>>>)> {
) -> impl Iterator<Item = (&Symbol, &Arc<RefCell<Chain<Option<Expr>>>>)> {
self.scopes.last().items.iter()
}

Expand Down
8 changes: 4 additions & 4 deletions stack-core/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::fmt;
use std::{cell::RefCell, collections::HashMap, fmt::Formatter, rc::Rc};
use std::{cell::RefCell, collections::HashMap, fmt::Formatter, sync::Arc};

use crate::{chain::Chain, expr::FnIdent, prelude::*};

pub type Val = Rc<RefCell<Chain<Option<Expr>>>>;
pub type Val = Arc<RefCell<Chain<Option<Expr>>>>;

#[derive(Default, PartialEq)]
pub struct Scope {
Expand Down Expand Up @@ -50,14 +50,14 @@ impl Scope {
}
}
} else {
let val = Rc::new(RefCell::new(Chain::new(Some(item))));
let val = Arc::new(RefCell::new(Chain::new(Some(item))));
self.items.insert(name, val);
}
}

pub fn reserve(&mut self, name: Symbol) {
if self.items.get(&name).is_none() {
let val = Rc::new(RefCell::new(Chain::new(None)));
let val = Arc::new(RefCell::new(Chain::new(None)));
self.items.insert(name, val);
}
}
Expand Down
6 changes: 3 additions & 3 deletions stack-core/src/source.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use core::{fmt, num::NonZeroUsize};
use std::{fs, io, path::Path, rc::Rc};
use std::{fs, io, path::Path, sync::Arc};

use unicode_segmentation::UnicodeSegmentation;

/// Contains metadata for a source.
///
/// This internally stores an [`Rc`], hence it is *cheap* to clone.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Source(Rc<SourceInner>);
pub struct Source(Arc<SourceInner>);

impl Source {
/// Creates a new [`Source`].
Expand All @@ -27,7 +27,7 @@ impl Source {
)
.collect::<Vec<_>>();

Self(Rc::new(SourceInner {
Self(Arc::new(SourceInner {
name,
source,
line_starts,
Expand Down

0 comments on commit 1896261

Please sign in to comment.