I wanted to use Async MongoDB in a project.
Didn't want to pass around the client because it would need to go around multiple tasks and threads. So I kept a static client using lazy static. However I can't use await in the initialization block.
What can I do to work around this?
Suggestions for doing it completely differently without lazy_static is also welcome.
use std::env;
use futures::stream::StreamExt;
use mongodb::{
bson::{doc, Bson},
options::ClientOptions,
Client,
};
lazy_static! {
static ref MONGO: Option<Client> = {
if let Ok(token) = env::var("MONGO_AUTH") {
if let Ok(client_options) = ClientOptions::parse(&token).await
^^^^^
{
if let Ok(client) = Client::with_options(client_options) {
return Some(client);
}
}
}
return None;
};
}
所有评论(0)