Skip to main content

DnyRun

Struct DnyRun 

Source
pub struct DnyRun {
    pub main_code: String,
    pub deps: String,
    pub is_release: bool,
    /* private fields */
}
Available on crate feature dyntest only.
Expand description

单个临时 Cargo 工程的句柄:在 target/dyn_tests/dny_<id>/ 下生成工程并编译执行,Drop 时释放心跳锁。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Fields§

§main_code: String

待编译运行的 main.rs 源码。

§deps: String

[dependencies] 依赖声明片段。

§is_release: bool

是否以 --release 编译运行。

Implementations§

Source§

impl DnyRun

Source

pub fn new(main_code: &str, deps: &str) -> Self

在 target/dyn_tests 下创建以纳秒时间戳 + 随机数命名的临时工程并同步文件。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Examples
use lib_unknown::dyntest::DnyRun;

let runner = DnyRun::new("fn main() {}", "");
let res = runner.build(None);
assert!(res.ok);
§Panics
  • 系统时钟早于 UNIX 纪元、无法获取当前目录或工程目录/文件创建失败时 panic。
Source

pub fn release(&mut self, release: bool) -> &mut Self

设置是否以 --release 编译运行。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn env( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self

注入单个子进程环境变量。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn envs<I, K, V>(&mut self, envs: I) -> &mut Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

批量注入子进程环境变量。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn clear_envs(&mut self) -> &mut Self

清空已注入的环境变量。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn cargo_config(&mut self, config: impl Into<String>) -> &mut Self

设置 .cargo/config.toml 内容并立即同步到工程目录。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • .cargo 目录或配置文件写入失败时 panic。
Source

pub fn clear_cargo_config(&mut self) -> &mut Self

清除 cargo 配置(内存值置空并删除已写入的配置文件,忽略删除失败)。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn sync_files(&self)

将当前 Cargo.toml / main.rs / cargo 配置同步到工程目录。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • 文件写入失败(磁盘不可写、权限不足)时 panic。
Source

pub fn build(&self, timeout: Option<Duration>) -> DnyResult

执行 cargo build(is_release 为真时加 --release),超时则终止并记为 SystemError::Timeout;自身不 panic,失败均体现在返回的 DnyResult 中。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Examples
use lib_unknown::dyntest::DnyRun;

let res = DnyRun::new("fn main() {}", "").build(None);
assert!(res.ok);
Source

pub fn run(&self, timeout: Option<Duration>) -> DnyResult

先编译后直接运行产物;编译失败时直接返回编译结果,不启动进程。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Examples
use lib_unknown::dyntest::DnyRun;

let res = DnyRun::new(r#"fn main() { println!("hi"); }"#, "").run(None);
assert!(res.ok);
Source

pub fn run_no_build( &self, timeout: Option<Duration>, build_result: Option<DnyResult>, ) -> DnyResult

不重新编译,直接运行已有产物(产物缺失时返回 SystemError::ProcessSpawnFailed 结果)。 build_result 用于透传编译耗时,无则置零。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn reset_main_code(&mut self, main_code: &str)

替换 main.rs 内容并同步到工程目录(供缓存复用时复写任务代码)。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • 文件写入失败时 panic。
Source

pub fn reset_deps(&mut self, deps: &str)

替换依赖声明并同步到工程目录。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • 文件写入失败时 panic。

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.