Skip to content

Commit 2116b9e

Browse files
committed
Replace LazyLock with OnceLock
LazyLock only stabilizes in rustc 1.80 and causes a CI test error. OnceLock is stable since 1.70 and has the required functionality. Use OnceLock instead of LazyLock.
1 parent 435f6e8 commit 2116b9e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

bitreq/src/connection/rustls_stream.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use alloc::sync::Arc;
55
use core::convert::TryFrom;
66
use std::io::{self, Write};
77
use std::net::TcpStream;
8+
use std::sync::OnceLock;
89

910
use rustls::{self, ClientConfig, ClientConnection, RootCertStore, ServerName, StreamOwned};
1011
#[cfg(feature = "rustls-webpki")]
@@ -16,7 +17,9 @@ use crate::Error;
1617
pub type SecuredStream = StreamOwned<ClientConnection, TcpStream>;
1718

1819
#[allow(clippy::incompatible_msrv)] // We only guarantee MSRV for a subset of features.
19-
static CONFIG: std::sync::LazyLock<Arc<ClientConfig>> = std::sync::LazyLock::new(|| {
20+
static CONFIG: OnceLock<Arc<ClientConfig>> = OnceLock::new();
21+
22+
fn build_client_config() -> Arc<ClientConfig> {
2023
let mut root_certificates = RootCertStore::empty();
2124

2225
// Try to load native certs
@@ -44,7 +47,7 @@ static CONFIG: std::sync::LazyLock<Arc<ClientConfig>> = std::sync::LazyLock::new
4447
.with_root_certificates(root_certificates)
4548
.with_no_client_auth();
4649
Arc::new(config)
47-
});
50+
}
4851

4952
pub fn create_secured_stream(conn: &Connection) -> Result<HttpStream, Error> {
5053
// Rustls setup
@@ -54,8 +57,8 @@ pub fn create_secured_stream(conn: &Connection) -> Result<HttpStream, Error> {
5457
Ok(result) => result,
5558
Err(err) => return Err(Error::IoError(io::Error::new(io::ErrorKind::Other, err))),
5659
};
57-
let sess =
58-
ClientConnection::new(CONFIG.clone(), dns_name).map_err(Error::RustlsCreateConnection)?;
60+
let sess = ClientConnection::new(CONFIG.get_or_init(build_client_config).clone(), dns_name)
61+
.map_err(Error::RustlsCreateConnection)?;
5962

6063
// Connect
6164
#[cfg(feature = "log")]

0 commit comments

Comments
 (0)