mindustry logic execution, map- and schematic- parsing and rendering
make Block smaller
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/access.rs | 100 | ||||
| -rw-r--r-- | src/block/drills.rs | 2 | ||||
| -rw-r--r-- | src/block/liquid.rs | 4 | ||||
| -rw-r--r-- | src/block/mod.rs | 31 | ||||
| -rw-r--r-- | src/data/renderer.rs | 6 | ||||
| -rw-r--r-- | src/lib.rs | 1 |
7 files changed, 20 insertions, 126 deletions
@@ -1,6 +1,6 @@ [package] name = "mindus" -version = "2.0.1" +version = "2.0.2" edition = "2021" description = "A library for working with mindustry data formats (eg schematics and maps) (fork of plandustry)" authors = [ diff --git a/src/access.rs b/src/access.rs deleted file mode 100644 index 561d82c..0000000 --- a/src/access.rs +++ /dev/null @@ -1,100 +0,0 @@ -//! Similar to Cow but doesn't require ToOwned -use std::borrow::Borrow; -use std::cmp::Ordering; -use std::fmt; -use std::hash::{Hash, Hasher}; - -use std::ops::Deref; - -pub type BoxAccess<'a, D> = Access<'a, Box<D>, D>; - -#[derive(Clone, Debug)] -/// Similar to Cow but doesn't require ToOwned -pub enum Access<'a, T: AsRef<B>, B: ?Sized> { - Borrowed(&'a B), - Owned(T), -} - -impl<'a, T: AsRef<B>, B> Access<'a, T, B> { - pub const fn is_borrowed(&self) -> bool { - matches!(self, Self::Borrowed(..)) - } - - pub const fn is_owned(&self) -> bool { - matches!(self, Self::Owned(..)) - } -} - -impl<'a, T: AsRef<B>, B: ?Sized> From<T> for Access<'a, T, B> { - fn from(value: T) -> Self { - Self::Owned(value) - } -} - -impl<'a, T: AsRef<B>, B: ?Sized> AsRef<B> for Access<'a, T, B> { - fn as_ref(&self) -> &B { - self - } -} - -impl<'a, T: AsRef<B>, B: ?Sized> Borrow<B> for Access<'a, T, B> { - fn borrow(&self) -> &B { - match self { - Self::Borrowed(r) => r, - Self::Owned(v) => v.as_ref(), - } - } -} - -impl<'a, T: AsRef<B> + Default, B: ?Sized> Default for Access<'a, T, B> { - fn default() -> Self { - Self::Owned(T::default()) - } -} - -impl<'a, T: AsRef<B>, B: ?Sized> Deref for Access<'a, T, B> { - type Target = B; - - fn deref(&self) -> &Self::Target { - match self { - Self::Borrowed(r) => r, - Self::Owned(v) => v.as_ref(), - } - } -} - -impl<'a, T: AsRef<B>, B: ?Sized + fmt::Display> fmt::Display for Access<'a, T, B> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - B::fmt(self, f) - } -} - -impl<'a, T: AsRef<B>, B: ?Sized + Eq> Eq for Access<'a, T, B> {} - -impl<'a, T: AsRef<B>, B: ?Sized + Hash> Hash for Access<'a, T, B> { - fn hash<H: Hasher>(&self, state: &mut H) { - B::hash(self, state); - } -} - -impl<'a, T: AsRef<B>, B: ?Sized + Ord> Ord for Access<'a, T, B> { - fn cmp(&self, other: &Self) -> Ordering { - B::cmp(self, other) - } -} - -impl<'a, 'b, T: AsRef<B>, B: ?Sized + PartialEq<C>, U: AsRef<C>, C: ?Sized> - PartialEq<Access<'b, U, C>> for Access<'a, T, B> -{ - fn eq(&self, other: &Access<'b, U, C>) -> bool { - B::eq(self, other) - } -} - -impl<'a, 'b, T: AsRef<B>, B: ?Sized + PartialOrd<C>, U: AsRef<C>, C: ?Sized> - PartialOrd<Access<'b, U, C>> for Access<'a, T, B> -{ - fn partial_cmp(&self, other: &Access<'b, U, C>) -> Option<Ordering> { - B::partial_cmp(self, other) - } -} diff --git a/src/block/drills.rs b/src/block/drills.rs index 7d09941..8e5c20e 100644 --- a/src/block/drills.rs +++ b/src/block/drills.rs @@ -11,7 +11,7 @@ make_simple!( let mut top = load!(concat top => name which is ["large-plasma-bore" | "plasma-bore" | "cliff-crusher"], s); top.rotate(rot.rotated(false).count()); base.overlay(&top); - return base; + base }, |_, _, _, buff: &mut DataRead| read_drill(buff) ); diff --git a/src/block/liquid.rs b/src/block/liquid.rs index 3869971..5044e3a 100644 --- a/src/block/liquid.rs +++ b/src/block/liquid.rs @@ -17,9 +17,9 @@ make_simple!( let ctx = ctx.unwrap(); let mask = mask(ctx, rot, name); let (index, rot, flip) = mask2rotations(mask, rot); - let tile = rotations2tile((index, rot, flip), "conduit", s); + // TODO caps. stopped trying bcz too complex - tile + rotations2tile((index, rot, flip), "conduit", s) }, true ); diff --git a/src/block/mod.rs b/src/block/mod.rs index 49d095d..66ea69e 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -5,12 +5,10 @@ //! with the exception of sandbox, that is. use bobbin_bits::U4::{self, *}; use std::any::Any; -use std::borrow::Cow; use std::error::Error; use std::fmt; use std::sync::LazyLock; -use crate::access::BoxAccess; use crate::data::dynamic::{DynData, DynType}; use crate::data::map::{Build, EntityMapping}; use crate::data::{self, renderer::*, CompressError}; @@ -165,8 +163,8 @@ impl SerializeError { /// a block. put it in stuff! pub struct Block { image: Option<[&'static LazyLock<RgbaImage>; 3]>, - name: Cow<'static, str>, - pub(crate) logic: BoxAccess<'static, dyn BlockLogic + Sync>, + name: &'static str, + pub(crate) logic: &'static (dyn BlockLogic + Sync), } impl PartialEq for Block { @@ -179,8 +177,8 @@ impl Block { #[must_use] /// create a new block pub const fn new( - name: Cow<'static, str>, - logic: BoxAccess<'static, dyn BlockLogic + Sync>, + name: &'static str, + logic: &'static (dyn BlockLogic + Sync), image: Option<[&'static LazyLock<RgbaImage>; 3]>, ) -> Self { Self { name, logic, image } @@ -190,13 +188,13 @@ impl Block { /// ``` /// assert!(mindus::block::distribution::DISTRIBUTOR.name() == "distributor") /// ``` - pub fn name(&self) -> &str { - &self.name + pub fn name(&self) -> &'static str { + self.name } /// should you send context to [`image`]? pub fn wants_context(&self) -> bool { - self.logic.as_ref().want_context() + self.logic.want_context() } /// draw this block, with this state @@ -212,9 +210,7 @@ impl Block { imgs.get_unchecked(scale as usize) })); } - self.logic - .as_ref() - .draw(&self.name, state, context, rot, scale) + self.logic.draw(self.name, state, context, rot, scale) } /// size. @@ -229,7 +225,7 @@ impl Block { /// cost pub fn get_build_cost(&self) -> Option<ItemStorage> { - self.logic.as_ref().create_build_cost() + self.logic.create_build_cost() } pub(crate) fn data_from_i32( @@ -266,14 +262,13 @@ impl Block { impl fmt::Debug for Block { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let name: &str = &self.name; - write!(f, "Block<{name:?}>") + write!(f, "Block<{:?}>", self.name) } } impl RegistryEntry for Block { fn get_name(&self) -> &str { - &self.name + self.name } } @@ -451,12 +446,12 @@ macro_rules! make_register { }}; (impl $field: literal => $logic: expr) => { paste::paste! { pub static [<$field:snake:upper>]: $crate::block::Block = $crate::block::Block::new( - std::borrow::Cow::Borrowed($field), $crate::access::Access::Borrowed(&$logic), None + $field, &$logic, None ); } }; (impl $field: literal -> $logic: expr) => { paste::paste! { pub static [<$field:snake:upper>]: $crate::block::Block = $crate::block::Block::new( - std::borrow::Cow::Borrowed($field), $crate::access::Access::Borrowed(&$logic), Some(crate::data::renderer::load!($field)) + $field, &$logic, Some(crate::data::renderer::load!($field)) ); } } } diff --git a/src/data/renderer.rs b/src/data/renderer.rs index c06fb44..5234b60 100644 --- a/src/data/renderer.rs +++ b/src/data/renderer.rs @@ -114,9 +114,9 @@ impl std::ops::Mul<u32> for Scale { macro_rules! load { ($name:literal, $scale:ident) => { paste::paste! { ImageHolder::from(std::sync::LazyLock::force(match $scale { - crate::data::renderer::Scale::Quarter => crate::data::renderer::quar::[<$name:snake:upper>], - crate::data::renderer::Scale::Eigth => crate::data::renderer::eigh::[<$name:snake:upper>], - crate::data::renderer::Scale::Full => crate::data::renderer::full::[<$name:snake:upper>], + $crate::data::renderer::Scale::Quarter => $crate::data::renderer::quar::[<$name:snake:upper>], + crate::data::renderer::Scale::Eigth => $crate::data::renderer::eigh::[<$name:snake:upper>], + crate::data::renderer::Scale::Full => $crate::data::renderer::full::[<$name:snake:upper>], })) } }; ($name: literal) => { paste::paste! { @@ -1,6 +1,5 @@ //! crate for dealing with mindustry #![feature(lazy_cell, array_chunks)] -mod access; pub mod block; mod content; pub mod data; |