I am writing a generic radix tree. Nodes without children are very frequent in my implementation. So I would like to prevent allocating lots of nodes with empty children.
I came up with this. A generic empty Arc<Vec<T>> that can be used whenever you want one and don't want to allocate.
Is this safe? It seems to me that an empty Vec<T> should have the same in memory repesentation no matter what T is. But I am not sure.
transmuteing Vec (or doing essentially that via pointer casting) is UB because it's repr(Rust), and thus offers no layout guarantees. The compiler is allowed to pick a different ordering for the fields even if just between Vec<i32> and Vec<u32>.
Most standard library types are like this -- not promising their layouts -- and the exceptions discuss it in their documentation.