我有一个大项目,我用lazy_static
创建一个singleton
。我认为lazy_static
机箱中有一个错误(只出现在大型项目中(,或者我做错了什么,因为必须调用一次才能创建singleton的初始化函数被调用了两次。
项目结构如下
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/Cocargo.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 = "../" }
食品/货物.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
s和2个INIT
s,而INIT
必须只打印一次。当我更改体系结构时,函数被调用过一次,但我需要这个体系结构。当我将foo::high::some_fun();
(在auto_fun中(更改为super::super::layer::some_fun();
时,函数也被调用了一次。我不能理解这种行为。我也用了once_cell
,但我得到了相同的结果
您与foo-high
->有一个间接循环依赖关系;foo
工作区->CCD_ 14。Rust和Cargo在设计时并没有考虑到依赖循环;您的体系结构应该避免它们。确切地说,问题的出现是因为foo-high
的两个实例都有自己的static
s集。
在单元测试中,super::super::layer::some_fun
表示fn机箱内变体"fn";A";,而CCD_ 18表示fn在板条箱中的变体"fn";B";。前者在您的上下文中运行良好,而后者通过单独的static
s揭示了问题。这种重复在这里发生,但当不同的机箱版本被拉入依赖图的某个位置时也会发生。
我不确定您的机箱体系结构背后的动机,但一个潜在的解决方案是消除对foo
的开发依赖。然后,为了伪造您想要的模块结构,您可能会有以下内容:您在foo-high
的根处添加
mod foo {
mod high {
pub use crate::*;
}
pub use foo_core as core;
}
此外,单元测试通过项目的全局路径引用项目是不常见的。最好保持当地特色。否则,最好编写一个集成测试。