Skip to content

Commit

Permalink
id tests + use std mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Aug 9, 2023
1 parent e0891f0 commit 3275060
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ impl<T: Deref<Target = State> + 'static> States<T> {
/// A shared state with states reference count and the index
/// which is cheap to clone.
pub struct Shared<T: Deref<Target = State> + 'static> {
pub entries: crate::util::StaticRef<crate::state::States<T>>,
pub value: crate::util::StaticRef<T>,
pub entries: crate::util::Ref<'static, crate::state::States<T>>,
pub value: crate::util::Ref<'static, T>,
}

impl<T: Deref<Target = State>> Deref for Shared<T> {
Expand Down
35 changes: 25 additions & 10 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
))
}
}
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -137,8 +154,6 @@ pub trait EnumValues<const N: usize>: Sized + Clone + Copy + PartialEq + Eq {
fn values() -> [Self; N];
}

pub type StaticRef<T> = Ref<'static, T>;

/// Represents a reference with enhancements based on `&'a`.
pub struct Ref<'a, T: 'a + ?Sized>(pub &'a T);

Expand All @@ -158,7 +173,7 @@ impl<'a, T: 'a + ?Sized> Deref for Ref<'a, T> {
}
}

impl<'a, T: 'a> From<T> for Ref<'a, T> {
impl<T> From<T> for Ref<'static, T> {
fn from(value: T) -> Self {
Self(Box::leak(Box::new(value)))
}
Expand All @@ -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.
Expand All @@ -197,14 +212,14 @@ where
{
immutable: once_cell::sync::OnceCell<I>,
/// The mutable instance.
pub mutable: parking_lot::Mutex<Option<M>>,
pub mutable: std::sync::Mutex<Option<M>>,
}

impl<I, M: Freeze<I>> Freezer<I, M> {
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)),
}
}

Expand All @@ -213,7 +228,7 @@ impl<I, M: Freeze<I>> Freezer<I, M> {
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.
Expand Down
4 changes: 2 additions & 2 deletions src/world/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down

0 comments on commit 3275060

Please sign in to comment.