mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 02:14:56 +02:00
Replace raw usize IDs with new types (#13832)
# Description In this PR I replaced most of the raw usize IDs with [newtypes](https://doc.rust-lang.org/rust-by-example/generics/new_types.html). Some other IDs already started using new types and in this PR I did not want to touch them. To make the implementation less repetitive, I made use of a generic `Id<T>` with marker structs. If this lands I would try to move make other IDs also in this pattern. Also at some places I needed to use `cast`, I'm not sure if the type was incorrect and therefore casting not needed or if actually different ID types intermingle sometimes. # User-Facing Changes Probably few, if you got a `DeclId` via a function and placed it later again it will still work.
This commit is contained in:
@ -183,6 +183,7 @@ mod test {
|
||||
use nu_protocol::{
|
||||
ast::{CellPath, PathMember},
|
||||
engine::Closure,
|
||||
BlockId,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
|
||||
@ -244,7 +245,7 @@ mod test {
|
||||
Value::list(vec![Value::bool(true, span)], span),
|
||||
Value::closure(
|
||||
Closure {
|
||||
block_id: 0,
|
||||
block_id: BlockId::new(0),
|
||||
captures: Vec::new(),
|
||||
},
|
||||
span,
|
||||
|
@ -271,7 +271,7 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
|
||||
.join(" ")
|
||||
),
|
||||
//TODO: It would be good to drill deeper into closures.
|
||||
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id),
|
||||
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id.get()),
|
||||
Value::Nothing { .. } => String::new(),
|
||||
Value::Error { error, .. } => format!("{error:?}"),
|
||||
Value::Binary { val, .. } => format!("{val:?}"),
|
||||
|
@ -1,4 +1,5 @@
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{BlockId, DeclId};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ViewIr;
|
||||
@ -86,7 +87,8 @@ the declaration may not be in scope.
|
||||
let decl_id = val
|
||||
.try_into()
|
||||
.ok()
|
||||
.filter(|id| *id < engine_state.num_decls())
|
||||
.map(DeclId::new)
|
||||
.filter(|id| id.get() < engine_state.num_decls())
|
||||
.ok_or_else(|| ShellError::IncorrectValue {
|
||||
msg: "not a valid decl id".into(),
|
||||
val_span: target.span(),
|
||||
@ -102,11 +104,15 @@ the declaration may not be in scope.
|
||||
})?
|
||||
}
|
||||
// Block by ID - often shows up in IR
|
||||
Value::Int { val, .. } => val.try_into().map_err(|_| ShellError::IncorrectValue {
|
||||
msg: "not a valid block id".into(),
|
||||
val_span: target.span(),
|
||||
call_span: call.head,
|
||||
})?,
|
||||
Value::Int { val, .. } => {
|
||||
val.try_into()
|
||||
.map(BlockId::new)
|
||||
.map_err(|_| ShellError::IncorrectValue {
|
||||
msg: "not a valid block id".into(),
|
||||
val_span: target.span(),
|
||||
call_span: call.head,
|
||||
})?
|
||||
}
|
||||
// Pass through errors
|
||||
Value::Error { error, .. } => return Err(*error),
|
||||
_ => {
|
||||
@ -119,7 +125,7 @@ the declaration may not be in scope.
|
||||
|
||||
let Some(block) = engine_state.try_get_block(block_id) else {
|
||||
return Err(ShellError::GenericError {
|
||||
error: format!("Unknown block ID: {block_id}"),
|
||||
error: format!("Unknown block ID: {}", block_id.get()),
|
||||
msg: "ensure the block ID is correct and try again".into(),
|
||||
span: Some(target.span()),
|
||||
help: None,
|
||||
|
5
crates/nu-command/src/env/source_env.rs
vendored
5
crates/nu-command/src/env/source_env.rs
vendored
@ -2,7 +2,7 @@ use nu_engine::{
|
||||
command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block_with_early_return,
|
||||
redirect_env,
|
||||
};
|
||||
use nu_protocol::engine::CommandType;
|
||||
use nu_protocol::{engine::CommandType, BlockId};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Source a file for environment variables.
|
||||
@ -50,6 +50,7 @@ impl Command for SourceEnv {
|
||||
// Note: this hidden positional is the block_id that corresponded to the 0th position
|
||||
// it is put here by the parser
|
||||
let block_id: i64 = call.req_parser_info(engine_state, caller_stack, "block_id")?;
|
||||
let block_id = BlockId::new(block_id as usize);
|
||||
|
||||
// Set the currently evaluated directory (file-relative PWD)
|
||||
let file_path = if let Some(path) = find_in_dirs_env(
|
||||
@ -78,7 +79,7 @@ impl Command for SourceEnv {
|
||||
);
|
||||
|
||||
// Evaluate the block
|
||||
let block = engine_state.get_block(block_id as usize).clone();
|
||||
let block = engine_state.get_block(block_id).clone();
|
||||
let mut callee_stack = caller_stack
|
||||
.gather_captures(engine_state, &block.captures)
|
||||
.reset_pipes();
|
||||
|
@ -118,7 +118,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
|
||||
.map(|(x, y)| format!("{}: {}", x, local_into_string(y, ", ", config)))
|
||||
.collect::<Vec<_>>()
|
||||
.join(separator),
|
||||
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id),
|
||||
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id.get()),
|
||||
Value::Nothing { .. } => String::new(),
|
||||
Value::Error { error, .. } => format!("{error:?}"),
|
||||
Value::Binary { val, .. } => format!("{val:?}"),
|
||||
|
@ -1,5 +1,5 @@
|
||||
use nu_engine::{command_prelude::*, get_eval_block_with_early_return};
|
||||
use nu_protocol::engine::CommandType;
|
||||
use nu_protocol::{engine::CommandType, BlockId};
|
||||
|
||||
/// Source a file for environment variables.
|
||||
#[derive(Clone)]
|
||||
@ -44,7 +44,8 @@ impl Command for Source {
|
||||
// Note: this hidden positional is the block_id that corresponded to the 0th position
|
||||
// it is put here by the parser
|
||||
let block_id: i64 = call.req_parser_info(engine_state, stack, "block_id")?;
|
||||
let block = engine_state.get_block(block_id as usize).clone();
|
||||
let block_id = BlockId::new(block_id as usize);
|
||||
let block = engine_state.get_block(block_id).clone();
|
||||
|
||||
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
|
||||
|
||||
|
Reference in New Issue
Block a user