Make SpanId and RegId also use new ID struct (#13963)

# Description
In the PR #13832 I used some newtypes for the old IDs. `SpanId` and
`RegId` already used newtypes, to streamline the code, I made them into
the same style as the other marker-based IDs.

Since `RegId` should be a bit smaller (it uses a `u32` instead of
`usize`) according to @devyn, I made the `Id` type generic with `usize`
as the default inner value.

The question still stands how `Display` should be implemented if even.

# User-Facing Changes
Users of the internal values of `RegId` or `SpanId` have breaking
changes but who outside nushell itself even uses these?

# After Submitting
The IDs will be streamlined and all type-safe.
This commit is contained in:
Piepmatz
2024-10-01 13:23:27 +02:00
committed by GitHub
parent 46589faaca
commit b2d0d9cf13
8 changed files with 66 additions and 41 deletions

View File

@@ -120,7 +120,7 @@ pub const ENV_VARIABLE_ID: VarId = VarId::new(2);
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
// The first span is unknown span
pub const UNKNOWN_SPAN_ID: SpanId = SpanId(0);
pub const UNKNOWN_SPAN_ID: SpanId = SpanId::new(0);
impl EngineState {
pub fn new() -> Self {
@@ -1027,12 +1027,15 @@ impl EngineState {
/// Add new span and return its ID
pub fn add_span(&mut self, span: Span) -> SpanId {
self.spans.push(span);
SpanId(self.num_spans() - 1)
SpanId::new(self.num_spans() - 1)
}
/// Find ID of a span (should be avoided if possible)
pub fn find_span_id(&self, span: Span) -> Option<SpanId> {
self.spans.iter().position(|sp| sp == &span).map(SpanId)
self.spans
.iter()
.position(|sp| sp == &span)
.map(SpanId::new)
}
}
@@ -1041,7 +1044,7 @@ impl<'a> GetSpan for &'a EngineState {
fn get_span(&self, span_id: SpanId) -> Span {
*self
.spans
.get(span_id.0)
.get(span_id.get())
.expect("internal error: missing span")
}
}

View File

@@ -1037,20 +1037,20 @@ impl<'a> StateWorkingSet<'a> {
pub fn add_span(&mut self, span: Span) -> SpanId {
let num_permanent_spans = self.permanent_state.spans.len();
self.delta.spans.push(span);
SpanId(num_permanent_spans + self.delta.spans.len() - 1)
SpanId::new(num_permanent_spans + self.delta.spans.len() - 1)
}
}
impl<'a> GetSpan for &'a StateWorkingSet<'a> {
fn get_span(&self, span_id: SpanId) -> Span {
let num_permanent_spans = self.permanent_state.num_spans();
if span_id.0 < num_permanent_spans {
if span_id.get() < num_permanent_spans {
self.permanent_state.get_span(span_id)
} else {
*self
.delta
.spans
.get(span_id.0 - num_permanent_spans)
.get(span_id.get() - num_permanent_spans)
.expect("internal error: missing span")
}
}