gpt4 book ai didi

rust - 初始化函数在 `lazy_static` block 中被调用两次

转载 作者:行者123 更新时间:2023-12-03 11:25:50 24 4
gpt4 key购买 nike

我有一个大项目,我在其中使用 lazy_static 创建一个 singleton。我认为 lazy_static crate 中存在错误(仅出现在大型项目中)或者我做错了什么,因为必须调用一次以创建单例的初始化函数被调用了两次。

项目结构如下


Foo
|__foo-core
| |__src
| | |__lib.rs
| |__Cargo.toml
|
|__foo-high
| |__src
| | |__lib.rs
| |__Cargo.toml
|
|__src
| |__lib.rs
|__Cargo.toml

Foo/foo-core/src/lib.rs

pub mod my_file {
pub struct MyFile {
file: std::fs::File,
}

impl MyFile {
pub fn open(
path: &'static str,
) -> Result<MyFile, Box<dyn std::error::Error + Send + Sync>> {
let file_ = std::fs::File::create(path)?;
Ok(MyFile { file: file_ })
}
}
}

Foo/foo-high/src/lib.rs

mod high {
mod high_child {
#[cfg(test)]
mod high_child_unit_tests {
use crate::high::my_file::*;

#[test]
fn some_fun_test_runner() {
MyFile::get();
auto_fun();
MyFile::get();
}

fn auto_fun() {
// super::super::layer ::some_fun();
foo::high::some_fun();
}
}
}

pub mod layer {
use crate::high::my_file::*;

pub fn some_fun() {
MyFile::get();
}
}

mod my_file {
pub use foo_core::my_file as core_my_file;
use std::sync::{Mutex, MutexGuard};
lazy_static::lazy_static! {static ref INSTANCE: Mutex<core_my_file::MyFile> = init_fun();}

fn init_fun() -> Mutex<core_my_file::MyFile> {
println!("INIT");
let location = "location.txt";
Mutex::new(core_my_file::MyFile::open(location).expect("\nSome Error has occurred."))
}

pub struct MyFile {}

impl MyFile {
pub fn get() -> MutexGuard<'static, core_my_file::MyFile> {
println!("GET");
INSTANCE.lock().expect("The mutex has been poisoned")
}
}
}
}

pub mod layer {
pub use crate::high::layer::*;
}

Foo/foo-core/Cargo.toml

[package]
name = "foo-core"
version = "0.1.0"
edition = "2018"

Foo/foo-high/Cargo.toml

[package]
name = "foo-high"
version = "0.1.0"
edition = "2018"

[dependencies]
foo-core = { version = "0.1.0", path = "../foo-core" }
lazy_static = "1.4.0"

[dev-dependencies]
foo = { version = "0.1.0", path = "../" }

Foo/Cargo.toml

[package]
name = "foo"
version = "0.1.0"
edition = "2018"

[dependencies]
foo-high = { version = "0.1.0", path = "foo-high" }

[workspace]
members = ["foo-high", "foo-core"]

当我使用 -- --no-capture 运行 some_fun_test_runner 测试时,我看到 3 个 GET 和 2 个 INIT s 而 INIT 必须只打印一次。当我改变架构时,函数被调用了一次,但我需要这个架构。当我将 foo::high::some_fun();(在 auto_fun 中)更改为 super::super::layer::some_fun(); 时,函数是也叫过一次。我无法理解这种行为。我也使用了 once_cell 但我得到了相同的结果

最佳答案

您对 foo-high -> foo 工作空间 -> foo-high 有间接循环依赖。 Rust 和 Cargo 在设计时并未考虑依赖周期;你的架构应该避免它们。准确地说,您的问题出现是因为 foo-high 的两个实例都有自己的一组 static

在单元测试中,super::super::layer::some_fun 表示 crate 变体“A”中的 fn,而 foo::high::some_fun表示 crate 变体“B”中的 fn。前者在您的上下文中运行良好,而后者通过单独的 static 发现问题。这种重复发生在这里,但也发生在不同的 crate 版本被拉入依赖关系图下方的某个地方时。

我不确定你的 crate 架构背后的动机,但一个可能的解决方案是删除对 foo 的开发依赖。然后,为了伪造你想要的模块结构,你可能有以下内容:你在 foo-high

的根添加
mod foo {
mod high {
pub use crate::*;
}
pub use foo_core as core;
}

此外,单元测试通过全局路径引用项的情况并不常见。最好把东西放在本地。否则,最好改为编写集成测试。

关于rust - 初始化函数在 `lazy_static` block 中被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65380042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com