Skip to content

Commit

Permalink
[meta] switch to once_cell impls in std (#7580)
Browse files Browse the repository at this point in the history
`once_cell` is now part of std, so switch over to it almost everywhere,
and add lints for it.

This is purely mechanical -- there are no semantic changes compared to
`once_cell`.

There's one place that still needs `once_cell`'s `try_`, so keep that
around. See rust-lang/rust#109737.
  • Loading branch information
sunshowers authored and hawkw committed Feb 21, 2025
1 parent 3599fa2 commit f8bfbe1
Show file tree
Hide file tree
Showing 44 changed files with 710 additions and 673 deletions.
14 changes: 0 additions & 14 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ disallowed-methods = [
# Instead, the "transaction_retry_wrapper" should be preferred, as it
# automatically retries transactions experiencing contention.
{ path = "async_bb8_diesel::AsyncConnection::transaction_async", reason = "Prefer to use transaction_retry_wrapper, if possible. For tests and nested transactions, use transaction_non_retry_wrapper to at least get dtrace probes" },

# We use disallowed-methods for these rather than disallowed-types, because
# there's still one legitimate use for `once_cell`'s types:
# `get_or_try_init`, which isn't stablet yet.
# https://github.com/rust-lang/rust/issues/109737
{ path = "once_cell::unsync::OnceCell::get_or_init", reason = "use `std::cell::OnceCell` instead, unless you need get_or_try_init in which case #[expect] this lint" },
{ path = "once_cell::sync::OnceCell::get_or_init", reason = "use `std::sync::OnceLock` instead, unless you need get_or_try_init in which case #[expect] this lint" },
]
disallowed-types = [
{ path = "once_cell::unsync::Lazy", reason = "use `std::cell::LazyCell` instead" },
{ path = "once_cell::unsync::LazyCell", reason = "use `std::cell::LazyCell` instead" },
]
1 change: 0 additions & 1 deletion cockroach-admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ http.workspace = true
illumos-utils.workspace = true
omicron-common.workspace = true
omicron-uuid-kinds.workspace = true
once_cell.workspace = true
# See omicron-rpaths for more about the "pq-sys" dependency.
pq-sys = "*"
schemars.workspace = true
Expand Down
1 change: 0 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ uuid.workspace = true
parse-display.workspace = true
progenitor-client.workspace = true
omicron-workspace-hack.workspace = true
once_cell.workspace = true
regress.workspace = true

[dev-dependencies]
Expand Down
26 changes: 14 additions & 12 deletions common/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use crate::api::external::{self, Error};
use crate::policy::INTERNAL_DNS_REDUNDANCY;
use ipnetwork::Ipv6Network;
use once_cell::sync::Lazy;
use oxnet::{Ipv4Net, Ipv6Net};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV6};
use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV6},
sync::LazyLock,
};

pub const AZ_PREFIX: u8 = 48;
pub const RACK_PREFIX: u8 = 56;
Expand Down Expand Up @@ -101,7 +103,7 @@ pub const NUM_SOURCE_NAT_PORTS: u16 = 1 << 14;
// prefix range (`fd00::/48`). See `random_vpc_ipv6_prefix`.
// Furthermore, all the below *_OPTE_IPV6_SUBNET constants are
// /64's within this prefix.
pub static SERVICE_VPC_IPV6_PREFIX: Lazy<Ipv6Net> = Lazy::new(|| {
pub static SERVICE_VPC_IPV6_PREFIX: LazyLock<Ipv6Net> = LazyLock::new(|| {
Ipv6Net::new(
Ipv6Addr::new(0xfd77, 0xe9d2, 0x9cd9, 0, 0, 0, 0, 0),
VPC_IPV6_PREFIX_LENGTH,
Expand All @@ -110,11 +112,11 @@ pub static SERVICE_VPC_IPV6_PREFIX: Lazy<Ipv6Net> = Lazy::new(|| {
});

/// The IPv4 subnet for External DNS OPTE ports.
pub static DNS_OPTE_IPV4_SUBNET: Lazy<Ipv4Net> =
Lazy::new(|| Ipv4Net::new(Ipv4Addr::new(172, 30, 1, 0), 24).unwrap());
pub static DNS_OPTE_IPV4_SUBNET: LazyLock<Ipv4Net> =
LazyLock::new(|| Ipv4Net::new(Ipv4Addr::new(172, 30, 1, 0), 24).unwrap());

/// The IPv6 subnet for External DNS OPTE ports.
pub static DNS_OPTE_IPV6_SUBNET: Lazy<Ipv6Net> = Lazy::new(|| {
pub static DNS_OPTE_IPV6_SUBNET: LazyLock<Ipv6Net> = LazyLock::new(|| {
Ipv6Net::new(
Ipv6Addr::new(0xfd77, 0xe9d2, 0x9cd9, 1, 0, 0, 0, 0),
VPC_SUBNET_IPV6_PREFIX_LENGTH,
Expand All @@ -123,11 +125,11 @@ pub static DNS_OPTE_IPV6_SUBNET: Lazy<Ipv6Net> = Lazy::new(|| {
});

/// The IPv4 subnet for Nexus OPTE ports.
pub static NEXUS_OPTE_IPV4_SUBNET: Lazy<Ipv4Net> =
Lazy::new(|| Ipv4Net::new(Ipv4Addr::new(172, 30, 2, 0), 24).unwrap());
pub static NEXUS_OPTE_IPV4_SUBNET: LazyLock<Ipv4Net> =
LazyLock::new(|| Ipv4Net::new(Ipv4Addr::new(172, 30, 2, 0), 24).unwrap());

/// The IPv6 subnet for Nexus OPTE ports.
pub static NEXUS_OPTE_IPV6_SUBNET: Lazy<Ipv6Net> = Lazy::new(|| {
pub static NEXUS_OPTE_IPV6_SUBNET: LazyLock<Ipv6Net> = LazyLock::new(|| {
Ipv6Net::new(
Ipv6Addr::new(0xfd77, 0xe9d2, 0x9cd9, 2, 0, 0, 0, 0),
VPC_SUBNET_IPV6_PREFIX_LENGTH,
Expand All @@ -136,11 +138,11 @@ pub static NEXUS_OPTE_IPV6_SUBNET: Lazy<Ipv6Net> = Lazy::new(|| {
});

/// The IPv4 subnet for Boundary NTP OPTE ports.
pub static NTP_OPTE_IPV4_SUBNET: Lazy<Ipv4Net> =
Lazy::new(|| Ipv4Net::new(Ipv4Addr::new(172, 30, 3, 0), 24).unwrap());
pub static NTP_OPTE_IPV4_SUBNET: LazyLock<Ipv4Net> =
LazyLock::new(|| Ipv4Net::new(Ipv4Addr::new(172, 30, 3, 0), 24).unwrap());

/// The IPv6 subnet for Boundary NTP OPTE ports.
pub static NTP_OPTE_IPV6_SUBNET: Lazy<Ipv6Net> = Lazy::new(|| {
pub static NTP_OPTE_IPV6_SUBNET: LazyLock<Ipv6Net> = LazyLock::new(|| {
Ipv6Net::new(
Ipv6Addr::new(0xfd77, 0xe9d2, 0x9cd9, 3, 0, 0, 0, 0),
VPC_SUBNET_IPV6_PREFIX_LENGTH,
Expand Down
1 change: 0 additions & 1 deletion dev-tools/releng/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ omicron-common.workspace = true
omicron-pins.workspace = true
omicron-workspace-hack.workspace = true
omicron-zone-package.workspace = true
once_cell.workspace = true
reqwest.workspace = true
semver.workspace = true
serde.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions dev-tools/releng/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod job;
mod tuf;

use std::sync::Arc;
use std::sync::LazyLock;
use std::time::Duration;
use std::time::Instant;

Expand All @@ -20,7 +21,6 @@ use clap::Parser;
use fs_err::tokio as fs;
use omicron_zone_package::config::Config;
use omicron_zone_package::config::PackageName;
use once_cell::sync::Lazy;
use semver::Version;
use slog::debug;
use slog::error;
Expand Down Expand Up @@ -88,7 +88,7 @@ const TUF_PACKAGES: [&PackageName; 11] = [

const HELIOS_REPO: &str = "https://pkg.oxide.computer/helios/2/dev/";

static WORKSPACE_DIR: Lazy<Utf8PathBuf> = Lazy::new(|| {
static WORKSPACE_DIR: LazyLock<Utf8PathBuf> = LazyLock::new(|| {
// $CARGO_MANIFEST_DIR is at `.../omicron/dev-tools/releng`
let mut dir =
Utf8PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect(
Expand Down
1 change: 0 additions & 1 deletion gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ hyper.workspace = true
illumos-utils.workspace = true
ipcc.workspace = true
omicron-common.workspace = true
once_cell.workspace = true
schemars.workspace = true
serde.workspace = true
signal-hook.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions gateway/src/management_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use gateway_sp_comms::HostPhase2Provider;
use gateway_sp_comms::SharedSocket;
use gateway_sp_comms::SingleSp;
use gateway_sp_comms::SpRetryConfig;
use once_cell::sync::OnceCell;
use serde::Deserialize;
use serde::Serialize;
use slog::o;
Expand All @@ -39,6 +38,7 @@ use std::collections::HashMap;
use std::net::Ipv6Addr;
use std::net::SocketAddrV6;
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::Duration;
use tokio::net::UdpSocket;
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -162,7 +162,7 @@ pub struct ManagementSwitch {
// When it's dropped, it cancels the background tokio task that loops on
// that socket receiving incoming packets.
_shared_socket: Option<SharedSocket>,
location_map: Arc<OnceCell<Result<LocationMap, String>>>,
location_map: Arc<OnceLock<Result<LocationMap, String>>>,
discovery_task: JoinHandle<()>,
log: Logger,
}
Expand Down Expand Up @@ -290,7 +290,7 @@ impl ManagementSwitch {
// completes (because we won't be able to map "the SP of sled 7" to a
// correct switch port).
let port_to_handle = Arc::new(port_to_handle);
let location_map = Arc::new(OnceCell::new());
let location_map = Arc::new(OnceLock::new());
let discovery_task = {
let log = log.clone();
let port_to_handle = Arc::clone(&port_to_handle);
Expand Down
1 change: 0 additions & 1 deletion nexus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ nexus-networking.workspace = true
nexus-saga-recovery.workspace = true
nexus-test-interface.workspace = true
num-integer.workspace = true
once_cell.workspace = true
openssl.workspace = true
oximeter-client.workspace = true
oximeter-db = { workspace = true, default-features = false, features = [ "oxql" ] }
Expand Down
1 change: 0 additions & 1 deletion nexus/auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ hyper.workspace = true
newtype_derive.workspace = true
# See omicron-rpaths for more about the "pq-sys" dependency.
pq-sys = "*"
once_cell.workspace = true
openssl.workspace = true
oso.workspace = true
samael.workspace = true
Expand Down
15 changes: 8 additions & 7 deletions nexus/auth/src/authn/external/spoof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

//! Custom, test-only authn scheme that trusts whatever the client says
use std::sync::LazyLock;

use super::super::Details;
use super::HttpAuthnScheme;
use super::Reason;
Expand All @@ -16,7 +18,6 @@ use anyhow::Context;
use async_trait::async_trait;
use headers::authorization::{Authorization, Bearer};
use headers::HeaderMapExt;
use once_cell::sync::Lazy;
use slog::debug;
use uuid::Uuid;

Expand Down Expand Up @@ -56,20 +57,20 @@ const SPOOF_RESERVED_BAD_CREDS: &str = "this-fake-ID-it-is-truly-excellent";
const SPOOF_PREFIX: &str = "oxide-spoof-";

/// Actor (id) used for the special "bad credentials" error
static SPOOF_RESERVED_BAD_CREDS_ACTOR: Lazy<Actor> =
Lazy::new(|| Actor::UserBuiltin {
static SPOOF_RESERVED_BAD_CREDS_ACTOR: LazyLock<Actor> =
LazyLock::new(|| Actor::UserBuiltin {
user_builtin_id: "22222222-2222-2222-2222-222222222222"
.parse()
.unwrap(),
});

/// Complete HTTP header value to trigger the "bad actor" error
pub static SPOOF_HEADER_BAD_ACTOR: Lazy<Authorization<Bearer>> =
Lazy::new(|| make_header_value_str(SPOOF_RESERVED_BAD_ACTOR).unwrap());
pub static SPOOF_HEADER_BAD_ACTOR: LazyLock<Authorization<Bearer>> =
LazyLock::new(|| make_header_value_str(SPOOF_RESERVED_BAD_ACTOR).unwrap());

/// Complete HTTP header value to trigger the "bad creds" error
pub static SPOOF_HEADER_BAD_CREDS: Lazy<Authorization<Bearer>> =
Lazy::new(|| make_header_value_str(SPOOF_RESERVED_BAD_CREDS).unwrap());
pub static SPOOF_HEADER_BAD_CREDS: LazyLock<Authorization<Bearer>> =
LazyLock::new(|| make_header_value_str(SPOOF_RESERVED_BAD_CREDS).unwrap());

/// Implements a (test-only) authentication scheme where the client simply
/// provides the actor information in a custom bearer token and we always trust
Expand Down
7 changes: 4 additions & 3 deletions nexus/auth/src/authz/api_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
//!
//! Most `authz` types are generated by the `authz_resource!` macro.
use std::sync::LazyLock;

use super::actor::AnyActor;
use super::context::AuthorizedResource;
use super::oso_generic::Init;
Expand All @@ -40,7 +42,6 @@ use futures::FutureExt;
use nexus_db_fixed_data::FLEET_ID;
use nexus_types::external_api::shared::{FleetRole, ProjectRole, SiloRole};
use omicron_common::api::external::{Error, LookupType, ResourceType};
use once_cell::sync::Lazy;
use oso::PolarClass;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
Expand Down Expand Up @@ -159,8 +160,8 @@ pub struct Fleet;
/// Singleton representing the [`Fleet`] itself for authz purposes
pub const FLEET: Fleet = Fleet;

pub static FLEET_LOOKUP: Lazy<LookupType> =
Lazy::new(|| LookupType::ById(*FLEET_ID));
pub static FLEET_LOOKUP: LazyLock<LookupType> =
LazyLock::new(|| LookupType::ById(*FLEET_ID));

impl Eq for Fleet {}
impl PartialEq for Fleet {
Expand Down
2 changes: 0 additions & 2 deletions nexus/db-fixed-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ workspace = true
omicron-rpaths.workspace = true

[dependencies]
once_cell.workspace = true
# See omicron-rpaths for more about the "pq-sys" dependency.
pq-sys = "*"
strum.workspace = true
Expand All @@ -22,4 +21,3 @@ nexus-db-model.workspace = true
nexus-types.workspace = true
omicron-common.workspace = true
omicron-workspace-hack.workspace = true

Loading

0 comments on commit f8bfbe1

Please sign in to comment.