Since Infallible is an empty type, I expect (T, Infallible) also be an empty type, thus they should have the same size. But I noticed that (T, Infallible) have the same size as T, so currently, we have
mem::size_of::<Option<Infallible>>() == 0
mem::size_of::<Option<(u32, Infallible)>>() = 8
I guess this is because Rust treats empty type to have size 0, so the size of (T, Infallible) is size of T plus size of Infallible equals size of T. May be we can use something like Option<usize> to represent the size of a type internally, where None means the size of an empty type, in this way, we can distinguish empty types from zero sized types. For example, internally, we can have a function like:
fn internal_size_of<T>() -> Option<usize> { ... }
To keep the current behavior of mem::size_of, it can be implemented as:
fn size_of<T>() {
internal_size_of::<T>().unwrap_or(0)
}
Since
Infallibleis an empty type, I expect(T, Infallible)also be an empty type, thus they should have the same size. But I noticed that(T, Infallible)have the same size asT, so currently, we havemem::size_of::<Option<Infallible>>() == 0mem::size_of::<Option<(u32, Infallible)>>() = 8I guess this is because Rust treats empty type to have size 0, so the size of
(T, Infallible)is size ofTplus size ofInfallibleequals size ofT. May be we can use something likeOption<usize>to represent the size of a type internally, whereNonemeans the size of an empty type, in this way, we can distinguish empty types from zero sized types. For example, internally, we can have a function like:To keep the current behavior of
mem::size_of, it can be implemented as: