From 3b4effe8508dfaa38317ae860672fa4adc73df2e Mon Sep 17 00:00:00 2001 From: Jani Monoses Date: Fri, 4 Jul 2025 08:43:57 +0300 Subject: [PATCH] Use Arc instead of Rc --- examples/adder.rs | 12 ++++++------ src/lax/var/operators.rs | 9 ++++----- src/lax/var/var.rs | 27 ++++++++++++++------------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/examples/adder.rs b/examples/adder.rs index 4ab28b4..8c005d6 100644 --- a/examples/adder.rs +++ b/examples/adder.rs @@ -9,6 +9,10 @@ //! use open_hypergraphs::lax::var; use open_hypergraphs::lax::*; +use std::sync::{Arc, Mutex}; + +type Term = OpenHypergraph; +type Builder = Arc>; // There is a single generating object in the category: the bit. #[derive(PartialEq, Clone, Debug)] @@ -56,11 +60,7 @@ impl var::HasNot for Gate { } } -use std::cell::RefCell; -use std::rc::Rc; -type Term = OpenHypergraph; -type Builder = Rc>; type Var = var::Var; fn zero(state: Builder) -> Var { @@ -78,14 +78,14 @@ fn full_adder(a: Var, b: Var, cin: Var) -> (Var, Var) { (sum, cout) } -fn ripple_carry_adder(state: Builder, a: &[Var], b: &[Var]) -> (Vec, Var) { +fn ripple_carry_adder(state: Arc>, a: &[Var], b: &[Var]) -> (Vec, Var) { let n = a.len(); assert_eq!(n, b.len(), "Input bit arrays must have the same length"); let mut sum = Vec::with_capacity(n); // Start with carry_in = 0 - let mut carry = zero(state); + let mut carry = zero(state.clone()); // Process each bit position for i in 0..n { diff --git a/src/lax/var/operators.rs b/src/lax/var/operators.rs index 2b6c37b..6a60061 100644 --- a/src/lax/var/operators.rs +++ b/src/lax/var/operators.rs @@ -3,14 +3,13 @@ use crate::lax::open_hypergraph::OpenHypergraph; use super::var::*; -use std::cell::RefCell; use std::ops::*; -use std::rc::Rc; +use std::sync::{Arc, Mutex}; /// A general helper for constructing `m → n` maps pub fn operation( // TODO: generalise to something which borrows a mutable OpenHypergraph? - builder: &Rc>>, + builder: &Arc>>, vars: &[Var], result_types: Vec, // types of output vars op: A, @@ -26,7 +25,7 @@ pub fn operation( .collect(); let result_nodes = result_vars.iter().map(|v| v.new_source()).collect(); - let mut term = builder.borrow_mut(); + let mut term = builder.lock().unwrap(); let _ = term.new_edge( op, Hyperedge { @@ -41,7 +40,7 @@ pub fn operation( /// An `n → 1` operation, returning its sole target `Var`. pub fn fn_operation( // TODO: generalise to something which borrows a mutable OpenHypergraph? - builder: &Rc>>, + builder: &Arc>>, vars: &[Var], result_type: O, op: A, diff --git a/src/lax/var/var.rs b/src/lax/var/var.rs index 7e2a54d..9a3b5df 100644 --- a/src/lax/var/var.rs +++ b/src/lax/var/var.rs @@ -1,8 +1,7 @@ use crate::lax::hypergraph::*; use crate::lax::open_hypergraph::*; -use std::cell::RefCell; -use std::rc::Rc; +use std::sync::{Arc, Mutex}; // Given a set of operation labels (i.e., edge labels), // distinguish one as the type of "Vars". @@ -15,15 +14,15 @@ pub trait HasVar { #[derive(Clone, Debug)] pub struct Var { - pub state: Rc>>, + pub state: Arc>>, pub edge_id: EdgeId, pub label: O, } /// Vars can be created when the set of edge labels has a 'Copy' operation. impl Var { - pub fn new(state: Rc>>, default_node_label: O) -> Self { - let (edge_id, _) = state.borrow_mut().new_operation(A::var(), vec![], vec![]); + pub fn new(state: Arc>>, default_node_label: O) -> Self { + let (edge_id, _) = state.lock().unwrap().new_operation(A::var(), vec![], vec![]); Var { state, edge_id, @@ -33,30 +32,32 @@ impl Var { pub fn new_source(&self) -> NodeId { self.state - .borrow_mut() + .lock() + .unwrap() .add_edge_source(self.edge_id, self.label.clone()) } pub fn new_target(&self) -> NodeId { self.state - .borrow_mut() + .lock() + .unwrap() .add_edge_target(self.edge_id, self.label.clone()) } } -pub type BuildResult = Result, Rc>>>; +pub type BuildResult = Result, Arc>>>; /// Construct an [`OpenHypergraph`] from a function taking an empty OpenHypergraph, /// and returning two lists of [`Var`]s corresponding to *sources* and *targets*. pub fn build(f: F) -> BuildResult where - F: Fn(&Rc>>) -> (Vec>, Vec>), + F: Fn(&Arc>>) -> (Vec>, Vec>), { - let state = Rc::new(RefCell::new(OpenHypergraph::::empty())); + let state = Arc::new(Mutex::new(OpenHypergraph::::empty())); { let (s, t) = f(&state); - state.borrow_mut().sources = s.iter().map(|x| x.new_source()).collect(); - state.borrow_mut().targets = t.iter().map(|x| x.new_target()).collect(); + state.lock().unwrap().sources = s.iter().map(|x| x.new_source()).collect(); + state.lock().unwrap().targets = t.iter().map(|x| x.new_target()).collect(); } - Rc::try_unwrap(state).map(|f| f.into_inner()) + Arc::try_unwrap(state).map(|f| f.into_inner().unwrap()) }