-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Open
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.
Description
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>>() == 0mem::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)
}kierdavis and Scripter17
Metadata
Metadata
Assignees
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.