const fnify

This commit is contained in:
Corwin 2022-03-05 17:24:23 +00:00
parent 8a6f341421
commit c55ff3f714
6 changed files with 50 additions and 181 deletions

View file

@ -151,23 +151,20 @@ pub fn include_aseprite_inner(input: TokenStream) -> TokenStream {
} }
}); });
let tags = tags let tags = tags.iter().flat_map(|(tag, num_images)| {
.iter() tag.iter().map(move |tag| {
.map(|(tag, num_images)| { let start = tag.from_frame() as usize + num_images;
tag.iter().map(move |tag| { let end = tag.to_frame() as usize + num_images;
let start = tag.from_frame() as usize + num_images; let direction = tag.animation_direction() as usize;
let end = tag.to_frame() as usize + num_images;
let direction = tag.animation_direction() as usize;
let name = tag.name(); let name = tag.name();
assert!(start <= end, "Tag {} has start > end", name); assert!(start <= end, "Tag {} has start > end", name);
quote! { quote! {
#name => Tag::new(SPRITES, #start, #end, #direction) (#name, Tag::new(SPRITES, #start, #end, #direction))
} }
})
}) })
.flatten(); });
let include_paths = filenames.iter().map(|s| { let include_paths = filenames.iter().map(|s| {
let s = s.as_os_str().to_string_lossy(); let s = s.as_os_str().to_string_lossy();
@ -189,9 +186,9 @@ pub fn include_aseprite_inner(input: TokenStream) -> TokenStream {
]; ];
const TAGS: &TagMap = &TagMap::new( const TAGS: &TagMap = &TagMap::new(
phf::phf_map! { &[
#(#tags),* #(#tags),*
} ]
); );
}; };

57
agb/Cargo.lock generated
View file

@ -26,7 +26,6 @@ dependencies = [
"bitflags", "bitflags",
"hashbrown", "hashbrown",
"modular-bitfield", "modular-bitfield",
"phf",
"rustc-hash", "rustc-hash",
] ]
@ -315,50 +314,6 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
[[package]]
name = "phf"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259"
dependencies = [
"phf_macros",
"phf_shared",
"proc-macro-hack",
]
[[package]]
name = "phf_generator"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58fdf3184dd560f160dd73922bea2d5cd6e8f064bf4b13110abd81b03697b4e0"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "png" name = "png"
version = "0.17.5" version = "0.17.5"
@ -377,12 +332,6 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.36" version = "1.0.36"
@ -467,12 +416,6 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "siphasher"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a86232ab60fa71287d7f2ddae4a7073f6b7aac33631c3015abb556f08c6d0a3e"
[[package]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"

View file

@ -28,7 +28,6 @@ bare-metal = "1.0"
hashbrown = "0.12.0" hashbrown = "0.12.0"
modular-bitfield = "0.11.2" modular-bitfield = "0.11.2"
rustc-hash = { version = "1.0", default-features = false } rustc-hash = { version = "1.0", default-features = false }
phf = { version = "0.10", default-features = false, features = ["macros"] }
[package.metadata.docs.rs] [package.metadata.docs.rs]
default-target = "thumbv6m-none-eabi" default-target = "thumbv6m-none-eabi"

View file

@ -69,7 +69,6 @@ macro_rules! include_aseprite {
($($aseprite_path: expr),*) => {{ ($($aseprite_path: expr),*) => {{
use $crate::display::object::{Size, Sprite, Tag, TagMap}; use $crate::display::object::{Size, Sprite, Tag, TagMap};
use $crate::display::palette16::Palette16; use $crate::display::palette16::Palette16;
use $crate::phf;
$crate::include_aseprite_inner!($($aseprite_path),*); $crate::include_aseprite_inner!($($aseprite_path),*);
@ -77,18 +76,44 @@ macro_rules! include_aseprite {
}}; }};
} }
pub struct TagMap(phf::Map<&'static str, Tag>); pub struct TagMap {
tags: &'static [(&'static str, Tag)],
impl TagMap {
pub const fn new(map: phf::Map<&'static str, Tag>) -> Self {
Self(map)
}
} }
impl Deref for TagMap { const fn const_byte_compare(a: &[u8], b: &[u8]) -> bool {
type Target = phf::Map<&'static str, Tag>; if a.len() != b.len() {
fn deref(&self) -> &Self::Target { return false;
&self.0 }
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
impl TagMap {
pub const fn new(tags: &'static [(&'static str, Tag)]) -> TagMap {
Self { tags }
}
pub const fn get(&'static self, tag: &str) -> Option<&'static Tag> {
let mut i = 0;
while i < self.tags.len() {
let s = self.tags[i].0;
if const_byte_compare(s.as_bytes(), tag.as_bytes()) {
return Some(&self.tags[i].1);
}
i += 1;
}
None
}
pub fn values(&self) -> impl Iterator<Item = &'static Tag> {
self.tags.iter().map(|x| &x.1)
} }
} }
@ -317,10 +342,9 @@ impl ObjectController {
index: inner.pop()?, index: inner.pop()?,
free_list: &self.free_objects, free_list: &self.free_objects,
}; };
let p_sprite = sprite.clone();
Some(Object { Some(Object {
previous_sprite: sprite.clone(),
sprite, sprite,
previous_sprite: p_sprite,
loan, loan,
attrs: Attributes::new(), attrs: Attributes::new(),
}) })

View file

@ -113,7 +113,6 @@
pub use agb_image_converter::include_gfx; pub use agb_image_converter::include_gfx;
pub use agb_image_converter::include_aseprite_inner; pub use agb_image_converter::include_aseprite_inner;
pub use phf;
/// This macro declares the entry point to your game written using `agb`. /// This macro declares the entry point to your game written using `agb`.
/// ///

View file

@ -26,7 +26,6 @@ dependencies = [
"bitflags", "bitflags",
"hashbrown", "hashbrown",
"modular-bitfield", "modular-bitfield",
"phf",
"rustc-hash", "rustc-hash",
] ]
@ -322,50 +321,6 @@ version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "phf"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259"
dependencies = [
"phf_macros",
"phf_shared",
"proc-macro-hack",
]
[[package]]
name = "phf_generator"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58fdf3184dd560f160dd73922bea2d5cd6e8f064bf4b13110abd81b03697b4e0"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "png" name = "png"
version = "0.16.8" version = "0.16.8"
@ -378,18 +333,6 @@ dependencies = [
"miniz_oxide 0.3.7", "miniz_oxide 0.3.7",
] ]
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.36" version = "1.0.36"
@ -408,36 +351,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "rustc-hash" name = "rustc-hash"
version = "1.1.0" version = "1.1.0"
@ -481,12 +394,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "siphasher"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a86232ab60fa71287d7f2ddae4a7073f6b7aac33631c3015abb556f08c6d0a3e"
[[package]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"