初始化函数在 `lazy_static` 块中被调用两次

asm*_*mmo 3 singleton rust lazy-static

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

项目结构如下


Foo
|__foo-core
|  |__src
|  |  |__lib.rs
|  |__Cargo.toml
|
|__foo-high
|  |__src
|  |  |__lib.rs
|  |__Cargo.toml
|
|__src
|  |__lib.rs
|__Cargo.toml
Run Code Online (Sandbox Code Playgroud)

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_ })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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::*;
}
Run Code Online (Sandbox Code Playgroud)

Foo/foo-core/Cargo.toml

[package]
name = "foo-core"
version = "0.1.0"
edition = "2018"
Run Code Online (Sandbox Code Playgroud)

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 = "../" }
Run Code Online (Sandbox Code Playgroud)

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"]
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*son 5

您与foo-high->foo工作区 ->foo-high再次具有间接循环依赖关系。Rust 和 Cargo 的设计没有考虑到依赖循环;你的架构应该避免它们。准确地说,您的问题出现是因为 的两个实例foo-high都有自己的一组statics。

在单元测试中,super::super::layer::some_fun表示在 crate 变体“A”中foo::high::some_fun的 fn ,而在 crate 变体“B”中表示 fn。前者在您的上下文中工作正常,而后者用单独的statics揭示了问题。这种重复发生在这里,而且当不同的 crate 版本被拉入依赖关系图的某个地方时。

我不确定您的 crate 架构背后的动机,但一种潜在的解决方案是消除对foo. 然后,为了伪造你想要的模块结构,你可能有以下内容: 你在根目录添加foo-high

mod foo {
    mod high {
        pub use crate::*;
    }
    pub use foo_core as core;
}
Run Code Online (Sandbox Code Playgroud)

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