Hacker News new | ask | show | jobs
by lor_louis 326 days ago
I do something similar, but I don't implement the logic in a macro, instead I have a Vec struct which looks like

    struct Vec {
        void *data;
        size_t len;
        size_t cap;
        size_t sizeof_ty;
    }
I then use a macro to define a new type

    IntVec {
        struct Vec inner;
        int ty[0];
    }
Using the zero sized filed I can do typeof(*ty) to get some type safety back.

All of the methods are implemented on the base Vec type and have a small wrapper which casts/assets the type of the things you are trying to push.