Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::sync::OnceLock instead of once_cell::sync::Lazy #959

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ base64 = "0.22.1"
ureq-proto = "0.2.5"
# ureq-proto = { path = "../ureq-proto" }
log = "0.4.22"
once_cell = "1.19.0"
utf-8 = "0.7.6"
percent-encoding = "2.3.1"

Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,10 @@ mk_method!(trace, TRACE, WithoutBody);

#[cfg(test)]
pub(crate) mod test {
use std::io;
use std::{io, sync::OnceLock};

use assert_no_alloc::AllocDisabler;
use config::{Config, ConfigBuilder};
use once_cell::sync::Lazy;
use typestate::AgentScope;

use super::*;
Expand All @@ -610,8 +609,8 @@ pub(crate) mod test {
static A: AllocDisabler = AllocDisabler;

pub fn init_test_log() {
static INIT_LOG: Lazy<()> = Lazy::new(env_logger::init);
*INIT_LOG
static INIT_LOG: OnceLock<()> = OnceLock::new();
INIT_LOG.get_or_init(|| env_logger::init());
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions src/run.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::sync::Arc;
use std::sync::{Arc, OnceLock};
use std::{io, mem};

use http::uri::Scheme;
use http::{header, HeaderValue, Request, Response, Uri};
use once_cell::sync::Lazy;
use ureq_proto::client::flow::state::{Await100, RecvBody, RecvResponse, Redirect, SendRequest};
use ureq_proto::client::flow::state::{Prepare, SendBody as SendBodyState};
use ureq_proto::client::flow::{Await100Result, RecvBodyResult};
Expand Down Expand Up @@ -267,7 +266,9 @@ fn add_headers(
}

{
static ACCEPTS: Lazy<String> = Lazy::new(|| {
static ACCEPTS: OnceLock<String> = OnceLock::new();

let accepts = ACCEPTS.get_or_init(|| {
#[allow(unused_mut)]
let mut value = String::with_capacity(10);
#[cfg(feature = "gzip")]
Expand All @@ -278,8 +279,9 @@ fn add_headers(
value.push_str("br");
value
});

if !has_header_accept_enc {
if let Some(v) = config.accept_encoding().as_str(&ACCEPTS) {
if let Some(v) = config.accept_encoding().as_str(accepts) {
// unwrap is ok because above ACCEPTS will produce a valid value,
// or the value is user provided in which case it must be valid.
let value = HeaderValue::from_str(v).unwrap();
Expand Down
5 changes: 2 additions & 3 deletions src/tls/native_tls.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::convert::TryFrom;
use std::fmt;
use std::io::{Read, Write};
use std::sync::Arc;
use std::sync::{Arc, OnceLock};

use crate::tls::{RootCerts, TlsProvider};
use crate::{transport::*, Error};
use der::pem::LineEnding;
use der::Document;
use native_tls::{Certificate, HandshakeError, Identity, TlsConnector};
use native_tls::{TlsConnectorBuilder, TlsStream};
use once_cell::sync::OnceCell;

use super::TlsConfig;

Expand All @@ -18,7 +17,7 @@ use super::TlsConfig;
/// Requires feature flag **native-tls**.
#[derive(Default)]
pub struct NativeTlsConnector {
connector: OnceCell<Arc<TlsConnector>>,
connector: OnceLock<Arc<TlsConnector>>,
}

impl<In: Transport> Connector<In> for NativeTlsConnector {
Expand Down
5 changes: 2 additions & 3 deletions src/tls/rustls.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::convert::TryInto;
use std::fmt;
use std::io::{Read, Write};
use std::sync::Arc;
use std::sync::{Arc, OnceLock};

use once_cell::sync::OnceCell;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::crypto::CryptoProvider;
use rustls::{ClientConfig, ClientConnection, RootCertStore, StreamOwned, ALL_VERSIONS};
Expand All @@ -23,7 +22,7 @@ use super::TlsConfig;
/// Requires feature flag **rustls**.
#[derive(Default)]
pub struct RustlsConnector {
config: OnceCell<Arc<ClientConfig>>,
config: OnceLock<Arc<ClientConfig>>,
}

impl<In: Transport> Connector<In> for RustlsConnector {
Expand Down
Loading