diff options
| author | Christian Dürr <102963075+cd-work@users.noreply.github.com> | 2022-10-15 21:19:03 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-15 23:19:03 +0200 |
| commit | 6d2656fd56e8ac0f1b1443e28d474bf3ceca89bb (patch) | |
| tree | 394b01a0fcf33fb684c7048528d02a2f9b3dd0cb /runtime/worker_bootstrap.rs | |
| parent | 872dc9b1df1dec5466970f085875e50b9436e967 (diff) | |
refactor: Add default implementation for WorkerOptions (#14860)
This adds an implementation of `Default` for both `WorkerOptions` and
`BootstrapOptions`. Since both of these structs are rather big, this
should make it easier for people unfamiliar with the internals to focus
on the options relevant to them.
As a user of `deno_runtime` I feel like these should serve as good
defaults, getting people them started without having to tweak the
runtime. Additionally even if some changes are made, the usage of
`..Default::default()` will significantly help with code clarity and
verbosity.
Diffstat (limited to 'runtime/worker_bootstrap.rs')
| -rw-r--r-- | runtime/worker_bootstrap.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index 31e7c4382..8af78c21a 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -1,7 +1,12 @@ -use crate::ops::runtime::ppid; +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::ModuleSpecifier; +use std::thread; + +use crate::colors; +use crate::ops::runtime::ppid; /// Common bootstrap options for MainWorker & WebWorker #[derive(Clone)] @@ -24,6 +29,32 @@ pub struct BootstrapOptions { pub inspect: bool, } +impl Default for BootstrapOptions { + fn default() -> Self { + let cpu_count = thread::available_parallelism() + .map(|p| p.get()) + .unwrap_or(1); + + let runtime_version = env!("CARGO_PKG_VERSION").into(); + let user_agent = format!("Deno/{}", runtime_version); + + Self { + runtime_version, + user_agent, + cpu_count, + no_color: !colors::use_color(), + is_tty: colors::is_tty(), + enable_testing_features: Default::default(), + debug_flag: Default::default(), + ts_version: Default::default(), + location: Default::default(), + unstable: Default::default(), + inspect: Default::default(), + args: Default::default(), + } + } +} + impl BootstrapOptions { pub fn as_json(&self) -> String { let payload = json!({ |
