Note: See the discussion at Should we have another non-type never? - #2 by beepster - that is the original thread
I am making a crate like serde for deserializing arbitrary formats using a derive macro.
In my crate, I have the trait Value which has to be implemented in order to deserialize any structure. This trait comes in handy because the formats my crate supports can all be described by a HashMap<String, impl Value>
Some formats support things others don't. For example, TOML has Datetime and KDL has Hybrid which has both indexed keys like an array, but also named keys like an object.
All of the methods on trait Value return an Option<T>. One of the methods describes the Hybrid data structure, but since most formats don't support it the default implementation just returns None:
trait Value {
fn as_hybrid<'a>(&'a self) -> Option<impl Iterator<Item = (Option<Key<'a>>, impl Value)>> {
None::<std::iter::Empty<(Option<Key<'a>>, std::convert::Infallible)>>
}
}
As you can see, I need to specify the exact arbitrary type that None is.
In order to specify an arbitrary value for impl Value, In my crate I have an impl Value for Infallible. My crate has no other impls of Value. Instead, downstream crates would implement my_crate::Value for TomlDocument for example.
Since all methods on Value take self as an argument, I was able to provide an impl Value for Infallible like this:
impl Value for Infallible {
fn as_list(&self) -> Option<impl Iterator<Item = impl Value>> {
None::<std::iter::Empty<std::convert::Infallible>>
}
fn as_map<'a>(&'a self) -> Option<impl Iterator<Item = (Spanned<Key<'a>>, impl Value)>> {
None::<std::iter::Empty<(Spanned<Key<'a>>, std::convert::Infallible)>>
}
}
As you can see, In order to provide this impl I once again need to specify a completely arbitrary type that implements those traits for None.
But because the body of these functions can never be reached, it would be nice if I did not have to specify the full explicit type of None here.
What if there was a magic type super ! which implements every trait? I could simplify the impl then:
impl Value for Infallible {
fn as_list(&self) -> Option<impl Iterator<Item = impl Value>> {
None::<super !>
}
fn as_map<'a>(&'a self) -> Option<impl Iterator<Item = (Spanned<Key<'a>>, impl Value)>> {
None::<super !>
}
}
But can such a type even exist? It does not make sense for a type to implement every trait, what about traits such as AlwaysPanic which has the appropriate function?
Therefore, we could have several rules.
Rule 1: You are not allowed to refer to any associated items of a trait implemented by super !. So this would be a compile error, no matter what trait or associated item you reference:
<super ! as Default>::default()
As shown in Should we have another non-type never? - #2 by beepster it is possible to use the return type of a free function if it is never called, so:
Rule 2: You can only use the super ! type in the bodies of trait methods, where the body is unreachable. This should be OK, because unlike free functions: you cannot just use the return type of a particular implementation of a trait method
What do you think? Could super ! actually exist, or is there some fundamental reason this type is impossible due to something I missed?