Skip to content

Commit

Permalink
chore: use ArcArray instead of Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Mar 31, 2024
1 parent 4ac6276 commit 41bf146
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
12 changes: 7 additions & 5 deletions crates/els/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,30 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
lsp_log!("packages must be an array: {pkgs}");
return cfg;
};
let mut packages = vec![];
for rec in arr.iter() {
let ast::Expr::Record(rec) = rec else {
lsp_log!("packages must be records: {rec}");
return cfg;
break;
};
let Some(ast::Expr::Literal(name)) =
rec.get("name").and_then(|name| name.body.block.first())
else {
return cfg;
break;
};
let name = name.token.content.replace('\"', "");
let Some(ast::Expr::Literal(as_name)) = rec
.get("as_name")
.and_then(|as_name| as_name.body.block.first())
else {
return cfg;
break;
};
let as_name = as_name.token.content.replace('\"', "");
let Some(ast::Expr::Literal(version)) = rec
.get("version")
.and_then(|version| version.body.block.first())
else {
return cfg;
break;
};
let version = version.token.content.replace('\"', "");
let path = rec.get("path").and_then(|path| {
Expand All @@ -314,8 +315,9 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
version.leak(),
path.map(|p| &*p.leak()),
);
cfg.packages.push(package);
packages.push(package);
}
cfg.packages = Arc::from(packages);
cfg
}

Expand Down
23 changes: 14 additions & 9 deletions crates/erg_common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::levenshtein::get_similar_name;
use crate::normalize_path;
use crate::python_util::{detect_magic_number, get_python_version, PythonVersion};
use crate::serialize::{get_magic_num_from_bytes, get_ver_from_magic_num};
use crate::ArcArray;

#[cfg(not(feature = "pylib"))]
use erg_proc_macros::{new, pyclass, pymethods};
Expand Down Expand Up @@ -167,8 +168,8 @@ pub struct ErgConfig {
/// needed for `jupyter-erg`
pub ps1: &'static str,
pub ps2: &'static str,
pub runtime_args: Vec<&'static str>,
pub packages: Vec<Package>,
pub runtime_args: ArcArray<&'static str>,
pub packages: ArcArray<Package>,
pub effect_check: bool,
pub ownership_check: bool,
}
Expand All @@ -194,8 +195,8 @@ impl Default for ErgConfig {
verbose: 1,
ps1: ">>> ",
ps2: "... ",
runtime_args: vec![],
packages: vec![],
runtime_args: ArcArray::from([]),
packages: ArcArray::from([]),
effect_check: true,
ownership_check: true,
}
Expand Down Expand Up @@ -269,13 +270,15 @@ impl ErgConfig {
let mut args = env::args();
args.next(); // "ergc"
let mut cfg = Self::default();
let mut runtime_args: Vec<&'static str> = vec![];
let mut packages = vec![];
// not `for` because we need to consume the next argument
while let Some(arg) = args.next() {
match &arg[..] {
/* Options */
"--" => {
for arg in args {
cfg.runtime_args.push(Box::leak(arg.into_boxed_str()));
runtime_args.push(Box::leak(arg.into_boxed_str()));
}
break;
}
Expand Down Expand Up @@ -332,7 +335,7 @@ impl ErgConfig {
.next()
.expect("`version` of `--use-package` is not passed")
.into_boxed_str();
cfg.packages.push(Package::new(
packages.push(Package::new(
Box::leak(name),
Box::leak(as_name),
Box::leak(version),
Expand All @@ -356,7 +359,7 @@ impl ErgConfig {
.next()
.expect("`path` of `--use-package` is not passed")
.into_boxed_str();
cfg.packages.push(Package::new(
packages.push(Package::new(
Box::leak(name),
Box::leak(as_name),
Box::leak(version),
Expand Down Expand Up @@ -505,7 +508,7 @@ USAGE:
cfg.mode = mode;
if cfg.mode == ErgMode::Pack {
for arg in args {
cfg.runtime_args.push(Box::leak(arg.into_boxed_str()));
runtime_args.push(Box::leak(arg.into_boxed_str()));
}
break;
}
Expand All @@ -517,7 +520,7 @@ USAGE:
match args.next().as_ref().map(|s| &s[..]) {
Some("--") => {
for arg in args {
cfg.runtime_args.push(Box::leak(arg.into_boxed_str()));
runtime_args.push(Box::leak(arg.into_boxed_str()));
}
}
Some(some) => {
Expand All @@ -543,6 +546,8 @@ USAGE:
};
cfg.input = input;
}
cfg.runtime_args = ArcArray::from(runtime_args);
cfg.packages = ArcArray::from(packages);
cfg
}
}
2 changes: 1 addition & 1 deletion crates/erg_compiler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl _Compiler {
#[new]
fn new(deps: Vec<Package>) -> Self {
let cfg = ErgConfig {
packages: deps,
packages: erg_common::ArcArray::from(deps),
..ErgConfig::default()
};
Self {
Expand Down
2 changes: 1 addition & 1 deletion src/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl PackageManagerRunner {
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.args(&cfg.runtime_args)
.args(cfg.runtime_args.as_ref())
.output()
{
Ok(out) => ExitStatus::new(out.status.code().unwrap_or(0), 0, 0),
Expand Down

0 comments on commit 41bf146

Please sign in to comment.