Introduce TracePos type for trace positions (#273)

Many internal structures refer to trace positions; this is an important type of values. In the code, it is sometimes `u32`, sometimes `usize`. While such variables usually have "_pos" suffix, having a dedicated type does improve code with type guarantees.
This commit is contained in:
Ivan Boldyrev
2022-06-10 08:29:56 +03:00
committed by GitHub
parent e0f6fa1b4d
commit 6df2b9d084
47 changed files with 439 additions and 233 deletions

View File

@ -18,6 +18,7 @@ use super::ExecutedState;
use super::ExecutionTrace;
use super::KeeperError::*;
use super::KeeperResult;
use crate::TracePos;
/// This slider is intended to slide on a subtrace inside provided trace. This subtrace
/// is identified by position and len.
@ -28,7 +29,7 @@ pub struct TraceSlider {
trace: ExecutionTrace,
/// Position of current subtrace inside trace.
position: usize,
position: TracePos,
/// Length of a current subtrace.
subtrace_len: usize,
@ -38,7 +39,8 @@ pub struct TraceSlider {
}
impl TraceSlider {
pub(crate) fn new(trace: ExecutionTrace) -> Self {
pub(crate) fn new(trace: impl Into<ExecutionTrace>) -> Self {
let trace = trace.into();
let subtrace_len = trace.len();
Self {
@ -52,7 +54,7 @@ impl TraceSlider {
/// and None otherwise.
#[allow(clippy::suspicious_operation_groupings)]
pub(crate) fn next_state(&mut self) -> Option<ExecutedState> {
if self.seen_elements >= self.subtrace_len || self.position >= self.trace.len() {
if self.seen_elements >= self.subtrace_len || usize::from(self.position) >= self.trace.len() {
return None;
}
@ -62,9 +64,10 @@ impl TraceSlider {
Some(result)
}
pub(crate) fn set_position_and_len(&mut self, position: usize, subtrace_len: usize) -> KeeperResult<()> {
pub(crate) fn set_position_and_len(&mut self, position: TracePos, subtrace_len: usize) -> KeeperResult<()> {
let pos: usize = position.into();
// it's possible to set empty subtrace_len and inconsistent position
if subtrace_len != 0 && position + subtrace_len > self.trace.len() {
if subtrace_len != 0 && pos + subtrace_len > self.trace.len() {
return Err(SetSubtraceLenAndPosFailed {
requested_pos: position,
requested_subtrace_len: subtrace_len,
@ -80,7 +83,7 @@ impl TraceSlider {
}
pub(crate) fn set_subtrace_len(&mut self, subtrace_len: usize) -> KeeperResult<()> {
let trace_remainder = self.trace.len() - self.position;
let trace_remainder = self.trace.len() - usize::from(self.position);
if trace_remainder < subtrace_len {
return Err(SetSubtraceLenFailed {
requested_subtrace_len: subtrace_len,
@ -95,7 +98,7 @@ impl TraceSlider {
Ok(())
}
pub(crate) fn position(&self) -> usize {
pub(crate) fn position(&self) -> TracePos {
self.position
}
@ -103,7 +106,8 @@ impl TraceSlider {
self.subtrace_len - self.seen_elements
}
pub(super) fn state_at_position(&self, position: usize) -> Option<&ExecutedState> {
pub(super) fn state_at_position(&self, position: TracePos) -> Option<&ExecutedState> {
// it would be nice to have the `impl SliceIndex for TracePos`, but it is unstable
self.trace.get(position)
}