1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

start work on optimizing block macros

This commit is contained in:
mat 2022-09-18 23:13:45 -05:00
parent 79cf19f93e
commit 4247945df1
3 changed files with 102 additions and 39 deletions

View file

@ -426,45 +426,45 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
quote! { BlockState::#block_name_pascal_case }
};
let block_struct = quote! {
#[derive(Debug)]
pub struct #block_struct_name {
#block_struct_fields
}
let block_struct = quote! {
#[derive(Debug, Copy, Clone)]
pub struct #block_struct_name {
#block_struct_fields
}
impl Block for #block_struct_name {
fn behavior(&self) -> BlockBehavior {
#block_behavior
}
fn id(&self) -> &'static str {
#block_id
impl Block for #block_struct_name {
fn behavior(&self) -> BlockBehavior {
#block_behavior
}
fn id(&self) -> &'static str {
#block_id
}
}
impl From<#block_struct_name> for BlockState {
fn from(b: #block_struct_name) -> Self {
#from_block_to_state_match
}
}
impl Default for #block_struct_name {
fn default() -> Self {
Self {
#block_default_fields
}
}
}
};
impl From<#block_struct_name> for BlockState {
fn from(b: #block_struct_name) -> Self {
#from_block_to_state_match
}
}
impl Default for #block_struct_name {
fn default() -> Self {
Self {
#block_default_fields
}
}
}
};
block_structs.extend(block_struct);
}
block_structs.extend(block_struct);
}
let last_state_id = (state_id - 1) as u32;
let mut generated = quote! {
#property_enums
#[repr(u32)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum BlockState {
#block_state_enum_variants
}
@ -476,21 +476,28 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#last_state_id
}
}
impl std::fmt::Debug for BlockState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// having a big match statement here would take up 700kb
f.write_str("BlockState")
}
}
};
generated.extend(quote! {
#block_structs
generated.extend(quote! {
#block_structs
impl From<BlockState> for Box<dyn Block> {
fn from(b: BlockState) -> Self {
let b = b as usize;
match b {
#from_state_to_block_match
_ => panic!("Invalid block state: {}", b),
}
impl From<BlockState> for Box<dyn Block> {
fn from(b: BlockState) -> Self {
let b = b as usize;
match b {
#from_state_to_block_match
_ => panic!("Invalid block state: {}", b),
}
}
});
}
});
generated.into()
}

View file

@ -63,4 +63,10 @@ mod tests {
assert!(BlockState::try_from(BlockState::max_state()).is_ok());
assert!(BlockState::try_from(BlockState::max_state() + 1).is_err());
}
#[test]
fn test_from_blockstate() {
let box_block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::Air);
assert_eq!(box_block.id(), "air");
}
}

View file

@ -7,6 +7,8 @@ pub struct BitSet {
data: Vec<u64>,
}
const ADDRESS_BITS_PER_WORD: usize = 6;
// the Index trait requires us to return a reference, but we can't do that
impl BitSet {
pub fn new(size: usize) -> Self {
@ -18,6 +20,54 @@ impl BitSet {
pub fn index(&self, index: usize) -> bool {
(self.data[index / 64] & (1u64 << (index % 64))) != 0
}
// private static int wordIndex(int bitIndex) {
// return bitIndex >> ADDRESS_BITS_PER_WORD;
// }
pub fn word_index(bit_index: usize) -> usize {
bit_index >> ADDRESS_BITS_PER_WORD
}
pub fn clear_from_to(&mut self, from: usize, to: usize) {
assert!(from <= to);
assert!(to <= self.data.len() * 64);
assert!(to > 0);
if from == to {
return;
}
// int startWordIndex = wordIndex(fromIndex);
// if (startWordIndex >= wordsInUse)
// return;
// int endWordIndex = wordIndex(toIndex - 1);
// if (endWordIndex >= wordsInUse) {
// toIndex = length();
// endWordIndex = wordsInUse - 1;
// }
// long firstWordMask = WORD_MASK << fromIndex;
// long lastWordMask = WORD_MASK >>> -toIndex;
// if (startWordIndex == endWordIndex) {
// // Case 1: One word
// words[startWordIndex] &= ~(firstWordMask & lastWordMask);
// } else {
// // Case 2: Multiple words
// // Handle first word
// words[startWordIndex] &= ~firstWordMask;
// // Handle intermediate words, if any
// for (int i = startWordIndex+1; i < endWordIndex; i++)
// words[i] = 0;
// // Handle last word
// words[endWordIndex] &= ~lastWordMask;
// }
// recalculateWordsInUse();
// checkInvariants();
}
}
impl McBufReadable for BitSet {