Skip to content

Commit

Permalink
this is meaningless
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Feb 23, 2024
1 parent d901d0a commit 106c090
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 72 deletions.
40 changes: 8 additions & 32 deletions crates/core/block/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
//! Minecraft Block primitives.
use rimecraft_registry::Reg;
use rimecraft_state::StatesMut;
use rimecraft_state::{States, StatesMut};

use std::marker::PhantomData;

mod pos;

pub use pos::BlockPos;

pub use rimecraft_state as state;

/// Block containing settings and the state manager.
#[derive(Debug)]
pub struct RawBlock<'a, P> {
settings: Settings,
states: rimecraft_state::States<'a>,
states: States<'a>,
_marker: PhantomData<P>,
}

impl<'a, P> RawBlock<'a, P> {
/// Creates a new block with the given settings.
#[inline]
pub fn new<B>(settings: Settings, p: B) -> Self
where
B: AppendProperties<'a>,
{
let mut states = StatesMut::new();
p.append_properties(&mut states);
pub const fn new(settings: Settings, states: States<'a>) -> Self {
Self {
settings,
states: states.freeze(),
states,
_marker: PhantomData,
}
}
Expand All @@ -41,15 +38,15 @@ impl<'a, P> RawBlock<'a, P> {

/// Returns the state manager of the block.
#[inline]
pub fn states(&self) -> &rimecraft_state::States<'a> {
pub fn states(&self) -> &States<'a> {
&self.states
}
}

impl<P> From<Settings> for RawBlock<'_, P> {
#[inline]
fn from(settings: Settings) -> Self {
Self::new(settings, ())
Self::new(settings, StatesMut::new().freeze())
}
}

Expand All @@ -75,24 +72,3 @@ pub struct Settings {

#[doc(alias = "BlockProperties")]
pub use Settings as BlockSettings;

/// Types that can appends properties to the block.
pub trait AppendProperties<'a> {
/// Appends properties to the block.
fn append_properties(self, settings: &mut StatesMut<'a>);
}

impl AppendProperties<'_> for () {
#[inline(always)]
fn append_properties(self, _: &mut StatesMut<'_>) {}
}

impl<'a, F> AppendProperties<'a> for F
where
F: for<'s> FnOnce(&'s mut StatesMut<'a>),
{
#[inline]
fn append_properties(self, settings: &mut StatesMut<'a>) {
self(settings)
}
}
5 changes: 3 additions & 2 deletions crates/core/state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ maintenance = { status = "passively-maintained" }
[dependencies]
regex = { version = "1.10", optional = true }
serde = { version = "1.0", optional = true }
rimecraft-serde-update = { path = "../../util/serde-update", optional = true }

[features]
# default = ["serde"]
serde = ["dep:serde"]
default = ["serde"]
serde = ["dep:serde", "dep:rimecraft-serde-update"]
# use `regex` crate instead of `regex-lite`.
regex = ["dep:regex"]

Expand Down
61 changes: 23 additions & 38 deletions crates/core/state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,33 +435,28 @@ impl<T> Deref for MaybeArc<'_, T> {
}
}

/// Trait for providing [`States`].
pub trait ProvideStates<'a, 's> {
/// Gets the states.
fn states() -> &'s States<'a>;
}

#[cfg(feature = "serde")]
pub mod serde {
//! Serde support for state.
use std::marker::PhantomData;
use std::sync::Arc;

use ::serde::{ser::SerializeMap, Deserialize, Serialize};
use rimecraft_serde_update::Update;
use serde::{ser::SerializeMap, Serialize};

use super::*;
use crate::State;

impl Serialize for State<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
S: serde::Serializer,
{
let mut map = serializer.serialize_map(Some(self.entries.len()))?;
for (prop, val) in &self.entries {
map.serialize_entry(
prop.name,
&prop.wrap.erased_to_name(*val).ok_or_else(|| {
::serde::ser::Error::custom(format!(
serde::ser::Error::custom(format!(
"invalid value {val} in property {}",
prop.name
))
Expand All @@ -472,42 +467,32 @@ pub mod serde {
}
}

/// A wrapper type for deserializing a state based on a default value
/// and updates it.
#[derive(Debug, Clone)]
pub struct FromDefault<T, P>(pub Arc<T>, PhantomData<P>);

impl<T, P> FromDefault<T, P> {
/// Creates a new instance with given default value.
#[inline]
pub const fn new(value: Arc<T>) -> Self {
Self(value, PhantomData)
}
}
/// Updatable state newtype wrapper.
#[derive(Debug)]
pub struct Upd<'a>(pub Arc<State<'a>>);

impl<P> Serialize for FromDefault<State<'_>, P> {
impl Serialize for Upd<'_> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

impl<'de, 's, 'a, P> Deserialize<'de> for FromDefault<State<'a>, P>
where
'a: 's,
P: ProvideStates<'a, 's> + 's,
{
impl<'de> Update<'de> for Upd<'_> {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
fn update<D>(
&mut self,
deserializer: D,
) -> Result<(), <D as serde::Deserializer<'de>>::Error>
where
D: ::serde::Deserializer<'de>,
D: serde::Deserializer<'de>,
{
struct Visitor<'a>(Arc<State<'a>>);

impl<'de, 'a> ::serde::de::Visitor<'de> for Visitor<'a> {
impl<'de, 'a> serde::de::Visitor<'de> for Visitor<'a> {
type Value = Arc<State<'a>>;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -516,7 +501,7 @@ pub mod serde {

fn visit_map<A>(mut self, mut map: A) -> Result<Self::Value, A::Error>
where
A: ::serde::de::MapAccess<'de>,
A: serde::de::MapAccess<'de>,
{
while let Some((prop, val)) = map.next_entry::<String, isize>()? {
self.0 = self
Expand All @@ -526,11 +511,11 @@ pub mod serde {
.expect("state not initialized")
.get(prop.as_str())
.ok_or_else(|| {
::serde::de::Error::custom(format!("property {prop} not found"))
serde::de::Error::custom(format!("property {prop} not found"))
})?
.get(&val)
.ok_or_else(|| {
::serde::de::Error::custom(format!(
serde::de::Error::custom(format!(
"value {val} not found in property {prop}"
))
})?
Expand All @@ -542,8 +527,8 @@ pub mod serde {
}

deserializer
.deserialize_map(Visitor(P::states().default_state().clone()))
.map(Self::new)
.deserialize_map(Visitor(self.0.clone()))
.map(|state| self.0 = state)
}
}
}
Expand Down

0 comments on commit 106c090

Please sign in to comment.