如何解决初始化函数在 `lazy_static` 块中被调用两次
我有一个大项目,我使用 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"]
当我使用 some_fun_test_runner
运行 -- --no-capture
测试时,我看到 3 个 GET
和 2 个 INIT
,而 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;
}
此外,单元测试通过全局路径引用项目的情况并不常见。最好把东西放在本地。否则,最好编写集成测试。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。