diff --git a/src/state/mod.rs b/src/state/mod.rs index bb90060..7aa808b 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -169,8 +169,8 @@ impl + 'static> States { /// A shared state with states reference count and the index /// which is cheap to clone. pub struct Shared + 'static> { - pub entries: crate::util::StaticRef>, - pub value: crate::util::StaticRef, + pub entries: crate::util::Ref<'static, crate::state::States>, + pub value: crate::util::Ref<'static, T>, } impl> Deref for Shared { diff --git a/src/util/mod.rs b/src/util/mod.rs index 6f099f6..646d819 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -27,7 +27,7 @@ impl Id { }) } else { Err(anyhow::anyhow!( - "Non [a-z0-9/._-] character in id: {namespace}:{path}" + "Non [a-z0-9/._-] character in id {namespace}:{path}" )) } } @@ -86,8 +86,7 @@ impl std::fmt::Display for Id { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(&self.namespace)?; f.write_str(":")?; - f.write_str(&self.path)?; - std::fmt::Result::Ok(()) + f.write_str(&self.path) } } @@ -117,6 +116,24 @@ impl<'de> serde::Deserialize<'de> for Id { } } +#[cfg(test)] +mod id_tests { + use crate::Id; + + #[test] + fn to_str() { + let id = Id::new("modid", "example_path").unwrap(); + assert_eq!(id.to_string(), "modid:example_path"); + } + + #[test] + fn parse_str() { + let raw = "modid:example_path"; + let id = Id::parse(raw); + assert_eq!(id.to_string(), raw); + } +} + /// Describes a var int. pub struct VarInt(pub i32); @@ -137,8 +154,6 @@ pub trait EnumValues: Sized + Clone + Copy + PartialEq + Eq { fn values() -> [Self; N]; } -pub type StaticRef = Ref<'static, T>; - /// Represents a reference with enhancements based on `&'a`. pub struct Ref<'a, T: 'a + ?Sized>(pub &'a T); @@ -158,7 +173,7 @@ impl<'a, T: 'a + ?Sized> Deref for Ref<'a, T> { } } -impl<'a, T: 'a> From for Ref<'a, T> { +impl From for Ref<'static, T> { fn from(value: T) -> Self { Self(Box::leak(Box::new(value))) } @@ -184,7 +199,7 @@ impl<'a, T: 'a> Hash for Ref<'a, T> { } } -/// A static instance that can be created with a type in a [`parking_lot::Mutex`] +/// A static instance that can be created with a type in a [`std::sync::Mutex`] /// to be mutable and be freezed into (maybe) another type inside a once cell. /// Which the freezed instance can be accessed without a lock and be borrowed /// outlives static. @@ -197,14 +212,14 @@ where { immutable: once_cell::sync::OnceCell, /// The mutable instance. - pub mutable: parking_lot::Mutex>, + pub mutable: std::sync::Mutex>, } impl> Freezer { pub const fn new(mutable: M) -> Self { Self { immutable: once_cell::sync::OnceCell::new(), - mutable: parking_lot::Mutex::new(Some(mutable)), + mutable: std::sync::Mutex::new(Some(mutable)), } } @@ -213,7 +228,7 @@ impl> Freezer { assert!(!self.is_freezed()); let _ = self .immutable - .set(self.mutable.lock().take().unwrap().build(opts)); + .set(self.mutable.lock().unwrap().take().unwrap().build(opts)); } /// Whether this instance has been already freezed. diff --git a/src/world/chunk.rs b/src/world/chunk.rs index b5f4a9e..3df4f58 100644 --- a/src/world/chunk.rs +++ b/src/world/chunk.rs @@ -57,7 +57,7 @@ pub struct Section<'w> { pub biome_container: palette::Container<'w, biome::Shared<'w>>, pub block_state_container: palette::Container<'static, block::SharedBlockState>, - lock: parking_lot::Mutex<()>, + lock: std::sync::Mutex<()>, non_empty_block_count: atomic::AtomicU16, non_empty_fluid_count: atomic::AtomicU16, @@ -77,7 +77,7 @@ impl<'w> Section<'w> { todo!(), palette::Provider::Biome, ), - lock: parking_lot::Mutex::new(()), + lock: std::sync::Mutex::new(()), non_empty_block_count: atomic::AtomicU16::new(0), non_empty_fluid_count: atomic::AtomicU16::new(0), random_tickable_block_count: atomic::AtomicU16::new(0),