3

我有一个大项目,我用它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 GETs 和 2 INITs 而INIT必须只打印一次。当我更改架构时,该函数被调用一次,但我需要这个架构。当我将foo::high::some_fun();(在 auto_fun 中)更改为 时super::super::layer::some_fun();,该函数也被调用了一次。我无法理解这种行为。我once_cell也用过,但我得到了相同的结果

4

1 回答 1

5

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

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

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

mod foo {
    mod high {
        pub use crate::*;
    }
    pub use foo_core as core;
}

更重要的是,单元测试通过全局路径引用项目是不常见的。最好保持本地化。否则,最好改为编写集成测试。

于 2020-12-20T16:24:09.057 回答