0

I have a struct generated by a macro that looks something like this:

struct MyStruct([ /* determined by macro - known at compile time */ ]);

How can I find the size of the sliced type? It's unsized so I can't use mem::size_of(), and I can't necessarily construct it because of the unknown type so this doesn't work either: mem::size_of_val(&my_struct.0[0])

to clarify, if the declaration is generated as

struct MyStruct([f32]);

I want to find mem::sizeof::<f32>()

gfaster
  • 157
  • 2
  • 12
  • 4
    Does [this answer](https://stackoverflow.com/a/70224634/6655004) help? You'd replace `|s: the_struct| s.foo` with `s: MyStruct| s.0[0]` and it should work (Also make `T: ?Sized` and `F: FnOnce(&T) -> U` because `T` would be unsized here). [Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=301236d098eaf7f23d95b54b93826b25) – Filipe Rodrigues Jul 26 '23 at 22:34
  • @FilipeRodrigues dang I like that answer, much easier for a layperson to use since pointer-chasing on "fake" objects is hard to get right – kmdreko Jul 26 '23 at 22:39
  • why don't you put an array instead of the slice inside the struct ? – Stargateur Jul 26 '23 at 22:51

0 Answers0