Skip to content

Commit

Permalink
wrap environments in refcell in order to maintain an immutable refere…
Browse files Browse the repository at this point in the history
…nce to manager
  • Loading branch information
kinrezC committed Aug 17, 2023
1 parent bb4b7af commit 138f7ae
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions arbiter-core/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#![warn(missing_docs, unsafe_code)]

use std::collections::HashMap;
use std::{cell::RefCell, collections::HashMap};

use log::{info, warn};
use thiserror::Error;
Expand All @@ -25,7 +25,7 @@ use crate::math::SeededPoisson;
pub struct Manager {
/// A map of environment labels to their corresponding environment
/// structures.
pub environments: HashMap<String, Environment>,
pub environments: RefCell<HashMap<String, Environment>>,
}

/// Errors that can occur while operating on or with the [`Manager`].
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Manager {
/// Creates a new [`Manager`] with an empty set of environments.
pub fn new() -> Self {
Self {
environments: HashMap::new(),
environments: RefCell::new(HashMap::new()),
}
}

Expand Down Expand Up @@ -117,21 +117,22 @@ impl Manager {
/// manager.add_environment("example_env", 1.0, 42).unwrap();
/// ```
pub fn add_environment<S: Into<String> + Clone>(
&mut self,
&self,
environment_label: S,
block_rate: f64,
seed: u64,
) -> Result<(), ManagerError> {
if self
.environments
.borrow()
.get(&environment_label.clone().into())
.is_some()
{
return Err(ManagerError::EnvironmentAlreadyExists(
environment_label.into(),
));
}
self.environments.insert(
self.environments.borrow_mut().insert(
environment_label.clone().into(),
Environment::new(environment_label.clone(), block_rate, seed),
);
Expand Down Expand Up @@ -172,10 +173,14 @@ impl Manager {
/// manager.start_environment("example_env").unwrap();
/// ```
pub fn start_environment<S: Into<String> + Clone>(
&mut self,
&self,
environment_label: S,
) -> Result<(), ManagerError> {
match self.environments.get_mut(&environment_label.clone().into()) {
match self
.environments
.borrow_mut()
.get_mut(&environment_label.clone().into())
{
Some(environment) => {
match environment.state.load(std::sync::atomic::Ordering::SeqCst) {
State::Initialization => {
Expand Down Expand Up @@ -244,10 +249,14 @@ impl Manager {
/// manager.pause_environment("example_env").unwrap();
/// ```
pub fn pause_environment<S: Into<String> + Clone>(
&mut self,
&self,
environment_label: S,
) -> Result<(), ManagerError> {
match self.environments.get_mut(&environment_label.clone().into()) {
match self
.environments
.borrow_mut()
.get_mut(&environment_label.clone().into())
{
Some(environment) => {
match environment.state.load(std::sync::atomic::Ordering::SeqCst) {
State::Initialization => Err(ManagerError::EnvironmentNotRunning(
Expand Down Expand Up @@ -311,10 +320,14 @@ impl Manager {
/// manager.stop_environment("example_env").unwrap();
/// ```
pub fn stop_environment<S: Into<String> + Clone>(
&mut self,
&self,
environment_label: S,
) -> Result<(), ManagerError> {
match self.environments.get_mut(&environment_label.clone().into()) {
match self
.environments
.borrow_mut()
.get_mut(&environment_label.clone().into())
{
Some(environment) => {
match environment.state.load(std::sync::atomic::Ordering::SeqCst) {
State::Initialization => Err(ManagerError::EnvironmentNotRunning(
Expand Down Expand Up @@ -376,6 +389,6 @@ pub(crate) mod tests {
#[test]
fn new_manager() {
let manager = Manager::new();
assert!(manager.environments.is_empty());
assert!(manager.environments.borrow().is_empty());
}
}

0 comments on commit 138f7ae

Please sign in to comment.