Skip to content

Commit

Permalink
fix palette edcode
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Mar 7, 2024
1 parent e7eab73 commit 00b24cc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 0 additions & 1 deletion crates/core/world/palette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ serde = { version = "1.0", features = ["derive"], optional = true }
rimecraft-serde-update = { path = "../../../util/serde-update", optional = true }

[features]
# default = ["edcode", "serde"]
edcode = ["dep:rimecraft-edcode"]
serde = ["dep:serde", "dep:rimecraft-serde-update"]

Expand Down
12 changes: 8 additions & 4 deletions crates/core/world/palette/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ mod _serde {

use super::*;

use rimecraft_maybe::SimpleOwned;
use rimecraft_serde_update::Update;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -468,7 +469,7 @@ mod _serde {

impl<L, T, P> Serialize for PalettedContainer<L, T, P>
where
L: Clone + for<'a> IndexToRaw<&'a T> + for<'s> IndexFromRaw<'s, &'s T>,
L: Clone + for<'a> IndexToRaw<&'a T> + for<'s> IndexFromRaw<'s, Maybe<'s, T>>,
for<'a> &'a L: IntoIterator,
for<'a> <&'a L as IntoIterator>::IntoIter: ExactSizeIterator,
T: Clone + Hash + Eq + Serialize,
Expand Down Expand Up @@ -497,7 +498,10 @@ mod _serde {
}
apply_each(&mut is, |id| {
pal.get(id as usize)
.cloned()
.map(|obj| match obj {
Maybe::Borrowed(obj) => obj.clone(),
Maybe::Owned(SimpleOwned(obj)) => obj,
})
.and_then(|obj| pal.index_or_insert(obj).ok())
.map_or(-1i32 as u32, |n| n as u32)
});
Expand Down Expand Up @@ -535,7 +539,7 @@ mod _serde {

impl<'de, L, T, P> Update<'de> for PalettedContainer<L, T, P>
where
L: Clone + for<'a> IndexToRaw<&'a T> + for<'s> IndexFromRaw<'s, &'s T>,
L: Clone + for<'a> IndexToRaw<&'a T> + for<'s> IndexFromRaw<'s, Maybe<'s, T>>,
T: Deserialize<'de> + Clone + Hash + Eq,
P: ProvidePalette<L, T>,
{
Expand Down Expand Up @@ -566,7 +570,7 @@ mod _serde {
write_palette_indices(&array, &mut is);
apply_each(&mut is, |id| {
pal.get(id as usize)
.and_then(|obj| self.list.raw_id(obj))
.and_then(|obj| self.list.raw_id(&obj))
.map_or(-1i32 as u32, |n| n as u32)
});

Expand Down
17 changes: 17 additions & 0 deletions crates/util/maybe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

#![no_std]

extern crate alloc;

use core::ops::Deref;

use alloc::borrow::ToOwned;

/// Cells that stores a type of data with a reference
/// variant or an owned variant.
#[allow(clippy::exhaustive_enums)]
Expand All @@ -16,6 +20,19 @@ pub enum Maybe<'a, T: ?Sized, Owned = SimpleOwned<T>> {
Owned(Owned),
}

impl<T: ?Sized, Owned> Maybe<'_, T, Owned>
where
T: ToOwned<Owned = Owned>,
{
/// Converts the cell into an owned value.
pub fn into_owned(self) -> Owned {
match self {
Maybe::Borrowed(val) => val.to_owned(),
Maybe::Owned(owned) => owned,
}
}
}

impl<T: ?Sized, Owned> Deref for Maybe<'_, T, Owned>
where
Owned: Deref<Target = T>,
Expand Down

0 comments on commit 00b24cc

Please sign in to comment.