|
|
|
|
|
by alexvitkov
729 days ago
|
|
Thanks for the write-up. My biggest fear is not references, overloads or memory management, but rather just the layout of their structures. We have this: sizeof(String) == 24
sizeof(Option<String>) == 24
Which is cool. But Option<T> is defined like this: enum Option<T> {
Some(T),
None,
}
I didn't find any "template specialization" tricks that you would see in C++, as far as I can see the compiler figures out some trick to squeeze Option<String> into 24 bytes. Whatever those tricks are, unless rustc has an option to export the layout of a type, you will need to implement yourself. |
|
As for the tricks used to make both 24 bytes, it’s NonNull within String that Option then detects and knows it can represent transparently without any enum tags. For what it’s worth you can do similar tricks in c++ using zero-sized types and tags to declare nullable state (in fact std::option already knows to do this for pointer types if I recall correctly)