Skip to content

Commit

Permalink
Replace once_cell by std::sync::OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
YgorSouza committed Sep 9, 2024
1 parent b84ac85 commit 984a8ce
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,6 @@ dependencies = [
"color-hex",
"document-features",
"emath",
"once_cell",
"serde",
]

Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ home = "0.5.9"
image = { version = "0.25", default-features = false }
log = { version = "0.4", features = ["std"] }
nohash-hasher = "0.2"
once_cell = "1.19.0"
parking_lot = "0.12"
puffin = "0.19"
puffin_http = "0.16"
Expand Down
1 change: 0 additions & 1 deletion crates/ecolor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ default = []

[dependencies]
emath.workspace = true
once_cell.workspace = true

#! ### Optional dependencies

Expand Down
10 changes: 5 additions & 5 deletions crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use once_cell::sync::Lazy;

use crate::{fast_round, linear_f32_from_linear_u8, Rgba};

/// This format is used for space-efficient color representation (32 bits).
Expand Down Expand Up @@ -95,13 +93,15 @@ impl Color32 {
/// From `sRGBA` WITHOUT premultiplied alpha.
#[inline]
pub fn from_rgba_unmultiplied(r: u8, g: u8, b: u8, a: u8) -> Self {
use std::sync::OnceLock;
match a {
// common-case optimization
0 => Self::TRANSPARENT,
// common-case optimization
255 => Self::from_rgb(r, g, b),
a => {
static LOOKUP_TABLE: Lazy<[u8; 256 * 256]> = Lazy::new(|| {
static LOOKUP_TABLE: OnceLock<[u8; 256 * 256]> = OnceLock::new();
let lut = LOOKUP_TABLE.get_or_init(|| {
use crate::{gamma_u8_from_linear_f32, linear_f32_from_gamma_u8};
core::array::from_fn(|i| {
let [value, alpha] = (i as u16).to_ne_bytes();
Expand All @@ -111,8 +111,8 @@ impl Color32 {
})
});

let [r, g, b] = [r, g, b]
.map(|value| LOOKUP_TABLE[usize::from(u16::from_ne_bytes([value, a]))]);
let [r, g, b] =
[r, g, b].map(|value| lut[usize::from(u16::from_ne_bytes([value, a]))]);
Self::from_rgba_premultiplied(r, g, b, a)
}
}
Expand Down

0 comments on commit 984a8ce

Please sign in to comment.