Skip to content

Commit

Permalink
Removed lazy_static!{} in favor of LazyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniD3v committed Aug 31, 2024
1 parent 070a508 commit 19aa1fa
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
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 pumpkin-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ thiserror = "1.0.63"
futures = "0.3.30"
flate2 = "1.0.33"
serde = { version = "1.0", features = ["derive"] }
lazy_static = "1.5.0"
serde_json = "1.0"
static_assertions = "1.1.0"
log.workspace = true
Expand Down
12 changes: 5 additions & 7 deletions pumpkin-world/src/block/block_registry.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::collections::HashMap;
use std::{collections::HashMap, sync::LazyLock};

use lazy_static::lazy_static;
use serde::Deserialize;

use super::block_id::BlockId;

lazy_static! {
pub static ref BLOCKS: HashMap<String, RegistryBlockType> =
serde_json::from_str(include_str!("../../assets/blocks.json"))
.expect("Could not parse block.json registry.");
}
pub static BLOCKS: LazyLock<HashMap<String, RegistryBlockType>> = LazyLock::new(|| {
serde_json::from_str(include_str!("../../assets/blocks.json"))
.expect("Could not parse block.json registry.")
});

#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct RegistryBlockDefinition {
Expand Down
11 changes: 4 additions & 7 deletions pumpkin-world/src/global_registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::HashMap;

use lazy_static::lazy_static;
use std::{collections::HashMap, sync::LazyLock};

pub const ITEM_REGISTRY: &str = "minecraft:item";

Expand All @@ -12,10 +10,9 @@ pub struct RegistryElement {
pub entries: HashMap<String, HashMap<String, u32>>,
}

lazy_static! {
pub static ref REGISTRY: HashMap<String, RegistryElement> =
serde_json::from_str(REGISTRY_JSON).expect("Could not parse registry.json registry.");
}
pub static REGISTRY: LazyLock<HashMap<String, RegistryElement>> = LazyLock::new(|| {
serde_json::from_str(REGISTRY_JSON).expect("Could not parse registry.json registry.")
});

pub fn get_protocol_id(category: &str, entry: &str) -> u32 {
*REGISTRY
Expand Down
16 changes: 6 additions & 10 deletions pumpkin-world/src/item/item_registry.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::collections::HashMap;

use lazy_static::lazy_static;

use crate::global_registry::{self, ITEM_REGISTRY};
use std::{collections::HashMap, sync::LazyLock};

use super::Rarity;
use crate::global_registry::{self, ITEM_REGISTRY};

const ITEMS_JSON: &str = include_str!("../../assets/items.json");

pub static ITEMS: LazyLock<HashMap<String, ItemElement>> = LazyLock::new(|| {
serde_json::from_str(ITEMS_JSON).expect("Could not parse items.json registry.")
});

#[derive(serde::Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ItemComponents {
// TODO: attribute_modifiers
Expand All @@ -27,11 +28,6 @@ pub struct ItemElement {
components: ItemComponents,
}

lazy_static! {
pub static ref ITEMS: HashMap<String, ItemElement> =
serde_json::from_str(ITEMS_JSON).expect("Could not parse items.json registry.");
}

#[allow(dead_code)]
pub fn get_item_element(item_id: &str) -> &ItemComponents {
&ITEMS.get(item_id).expect("Item not found").components
Expand Down

0 comments on commit 19aa1fa

Please sign in to comment.