我有一个大项目,我用它lazy_static
来创建一个singleton
. 我认为 crate 中存在错误lazy_static
(仅出现在大型项目中)或者我做错了什么,因为必须调用一次才能创建单例的初始化函数被调用了两次。
项目结构如下
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
s 和 2 INIT
s 而INIT
必须只打印一次。当我更改架构时,该函数被调用一次,但我需要这个架构。当我将foo::high::some_fun();
(在 auto_fun 中)更改为 时super::super::layer::some_fun();
,该函数也被调用了一次。我无法理解这种行为。我once_cell
也用过,但我得到了相同的结果