#First proc_macro, Struct of Arrays helper!

3 messages · Page 1 of 1 (latest)

sleek turtle
#

Hey all, this is my first proc_macro, I've done some macro_rules macros before.

Mainly looking for feedback on

  • Is there a better way to do the destructuring? It feels like a lot of typing for unpacking a relatively small amount of structure.
  • Is there a way to get more formatting for repeated expansion in quote? I'm creating iterators quoting for each entry, then including that interator in the final quote to get around it
#

Here is an example struct using it and the expansion

#[derive(SoA, Resource, Reflect, Debug, Default)]
pub struct BuildingRegistry {
    ids: Vec<BuildingId>,
    scopes: Vec<ScopeId>,
    sids: Vec<String>,
    names: Vec<String>,
    costs: Vec<Vec<(ItemId, u32)>>,
}

// Recursive expansion of SoA macro
// =================================

struct BuildingRegistryEntry {
    ids: BuildingId,
    scopes: ScopeId,
    sids: String,
    names: String,
    costs: Vec<(ItemId, u32)>,
}
impl BuildingRegistry {
    #[inline]
    fn push(&mut self, entry: BuildingRegistryEntry) {
        self.ids.push(entry.ids);
        self.scopes.push(entry.scopes);
        self.sids.push(entry.sids);
        self.names.push(entry.names);
        self.costs.push(entry.costs);
    }
    #[inline]
    fn len(&self) -> usize {
        self.ids.len()
    }
}
impl FromIterator<BuildingRegistryEntry> for BuildingRegistry {
    fn from_iter<T: IntoIterator<Item = BuildingRegistryEntry>>(iter: T) -> Self {
        let mut collection = Self::default();
        iter.into_iter().for_each(|entry| collection.push(entry));
        collection
    }
}
timber hemlock
#

Don't you generally want to go from AoS to SoA, not the other way around?