gpt4 book ai didi

mysql - 如何将 mysql::Conn 包含在 lazy_static 宏中?

转载 作者:行者123 更新时间:2023-11-29 07:35:03 24 4
gpt4 key购买 nike

这段代码:

#[macro_use]
extern crate lazy_static;
extern crate mysql;

use mysql::*;

fn some_fn() {
lazy_static! {
static ref CONNECTION: Conn = Conn::new("mysql://root:password@127.0.0.1:3306/mydb?prefer_socket=false").unwrap();
}
}

生成一条很长的错误信息:

error[E0277]: the trait bound `*mut std::os::raw::c_void: std::marker::Sync` is not satisfied in `winapi::minwinbase::OVERLAPPED`
--> src\main.rs:8:5
|
8 | / lazy_static! {
9 | | static ref CONNECTION: Conn = Conn::new("mysql://root:password@127.0.0.1:3306/mydb?prefer_socket=false").unwrap();
10 | | }
| |_____^ `*mut std::os::raw::c_void` cannot be shared between threads safely
|
= help: within `winapi::minwinbase::OVERLAPPED`, the trait `std::marker::Sync` is not implemented for `*mut std::os::raw::c_void`
= note: required because it appears within the type `winapi::minwinbase::OVERLAPPED`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<winapi::minwinbase::OVERLAPPED>`
= note: required because it appears within the type `std::boxed::Box<winapi::minwinbase::OVERLAPPED>`
= note: required because it appears within the type `named_pipe::Overlapped`
= note: required because it appears within the type `named_pipe::PipeClient`
= note: required because it appears within the type `std::option::Option<named_pipe::PipeClient>`
= note: required because it appears within the type `std::io::BufWriter<named_pipe::PipeClient>`
= note: required because it appears within the type `std::option::Option<std::io::BufWriter<named_pipe::PipeClient>>`
= note: required because it appears within the type `bufstream::InternalBufWriter<named_pipe::PipeClient>`
= note: required because it appears within the type `std::io::BufReader<bufstream::InternalBufWriter<named_pipe::PipeClient>>`
= note: required because it appears within the type `bufstream::BufStream<named_pipe::PipeClient>`
= note: required because it appears within the type `mysql::io::Stream`
= note: required because it appears within the type `std::option::Option<mysql::io::Stream>`
= note: required because it appears within the type `mysql::Conn`
= note: required by `lazy_static::lazy::Lazy`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

是因为 mysql::Conn 不适合在多线程应用程序中使用吗?如果我的程序不是多线程的并且我使用非线程安全类型,我如何使用 lazy_static?

最佳答案

您的代码在 macOS 上工作得很好...。我相信这是 a bug with the implementation of the MySQL crate ,作者同意了,在不到一天的时间内修复了这个错误。您应该能够只升级 crate 并使用您的原始代码。


作为临时解决方法,您可以将 Conn 包装在 Mutex 中:

use mysql::*;
use std::sync::Mutex;

fn some_fn() {
lazy_static! {
static ref CONNECTION: Mutex<Conn> = Mutex::new(Conn::new("mysql://root:password@127.0.0.1:3306/mydb?prefer_socket=false").unwrap());
}
}

if my program is not multithreaded and I use non-thread-safe types?

总是建议不要使用全局变量,无论是否使用线程。相反,在您的程序顶部创建您的 Conn 并将对它的引用传递到您的所有函数中。

关于mysql - 如何将 mysql::Conn 包含在 lazy_static 宏中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49309497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com