Skip to content

Commit

Permalink
fix: mut ref
Browse files Browse the repository at this point in the history
  • Loading branch information
brech1 committed Jan 29, 2024
1 parent ea7ac59 commit 562315d
Showing 1 changed file with 28 additions and 37 deletions.
65 changes: 28 additions & 37 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub enum SubAccess {
pub struct Runtime {
ctx_stack: Vec<Context>,
current_ctx: u32,
last_ctx: u32,
_last_ctx: u32,
}

impl Runtime {
Expand All @@ -67,7 +67,7 @@ impl Runtime {
Ok(Self {
ctx_stack: vec![Context::new(0, 0)],
current_ctx: 0,
last_ctx: 0,
_last_ctx: 0,
})
}

Expand Down Expand Up @@ -353,39 +353,9 @@ impl Variable {

/// Sets the content of the variable at the specified index path.
fn set(&mut self, index_path: &[u32], val: Option<u32>) -> Result<(), RuntimeError> {
if index_path.is_empty() {
return match &mut self.value {
NestedValue::Value(inner_value) => {
*inner_value = val;
Ok(())
}
_ => Err(RuntimeError::AccessError),
};
}

let (&last_index, indexes) = index_path.split_last().ok_or(RuntimeError::AccessError)?;

let mut current_level = &mut self.value;
for &index in indexes {
current_level = match current_level {
NestedValue::Array(values) => values
.get_mut(index as usize)
.ok_or(RuntimeError::IndexOutOfBounds)?,
_ => return Err(RuntimeError::AccessError),
};
}

match current_level {
NestedValue::Array(values) => {
if let Some(NestedValue::Value(inner_value)) = values.get(last_index as usize) {
*inner_value = val;
Ok(())
} else {
Err(RuntimeError::IndexOutOfBounds)
}
}
_ => Err(RuntimeError::AccessError),
}
let inner_value = get_nested_value_mut(&mut self.value, index_path)?;
*inner_value = val;
Ok(())
}

/// Retrieves the content of the variable at the specified index path.
Expand All @@ -396,7 +366,7 @@ impl Variable {

/// Component
#[derive(Clone, Debug)]
struct Component {
pub struct Component {
connections: NestedValue<HashMap<DataAccess, DataAccess>>,
}

Expand Down Expand Up @@ -476,7 +446,7 @@ impl DataAccess {
}

#[derive(Debug)]
enum ProcessedAccess {
pub enum ProcessedAccess {
Array(Vec<u32>),
Component(Vec<u32>, String, Vec<u32>), // (initial_path, signal_name, final_path)
}
Expand Down Expand Up @@ -543,6 +513,27 @@ pub fn get_nested_value<T: Clone>(
}
}

/// Generic function to navigate through NestedValue and return a mutable reference to the inner value.
pub fn get_nested_value_mut<'a, T>(
nested_value: &'a mut NestedValue<T>,
index_path: &[u32],
) -> Result<&'a mut T, RuntimeError> {
let mut current_level = nested_value;
for &index in index_path {
current_level = match current_level {
NestedValue::Array(values) => values
.get_mut(index as usize)
.ok_or(RuntimeError::IndexOutOfBounds)?,
_ => return Err(RuntimeError::AccessError),
};
}

match current_level {
NestedValue::Value(inner_value) => Ok(inner_value),
_ => Err(RuntimeError::NotAValue),
}
}

/// Converts a vector of u32 to a vector of SubAccess.
pub fn u32_to_access(indices: &[u32]) -> Vec<SubAccess> {
indices
Expand Down

0 comments on commit 562315d

Please sign in to comment.