- Feature Name: type_name
- Start Date: 2018-04-21
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)
Make the output of type_name
structured and convenient and put the intrinsic on the path to
sabilisation.
type_name
intrinsic is useful for many purposes – from logging and diagnostics to useful default
trait implementations such as for Error::description()
. Current type_name
intrinsic is not
flexible enough in its presentation of the type, forcing users’ hand in clumsy parsing of the
resulting string to cut out the undesired components.
Stabilising the functionality in some form or another will enable use of this functionality to improve diagnostics in the wider ecosystem.
The “guide” necessary for this feature will be handled by the API documentation.
To implement this feature, both core
and std
will gain the following items:
struct Type {
pub type_kind: TypeKind,
// Private fields
}
/// Structured representation of a type returned by `type_name`.
enum TypeKind {
/// A `(T…)`.
Tuple(&'static [Type]),
/// A `[T]`.
Slice(Type),
/// A `[T; n]`.
Array(Type, &'static str),
/// A `*const T`.
ConstPtr(Type),
/// A `*mut T`.
MutPtr(Type),
/// A `&T`.
Reference(Type),
/// A `&mut T`.
MutReference(Type),
/// An `extern "X" fn(A…) -> R`.
Function {
extern: &'static str,
args: &'static [Type],
return: Type,
},
/// All other types.
Named {
/// Path to the type (e.g. `std::option`)
path: &'static str,
/// Name of the type (e.g. `Option`)
name: &'static str,
/// Generics for the type.
generics: &'static [Type]
}
}
impl Display for Type {
// Optimised implementation of Type that displays a static string representing this `Type`.
}
Here is an example of a complicated type and its structured representation (with Type
wrapper
omitted):
// std::option::Option<(&u64, extern "C" fn(mycrate::mymodule::ADT<*const u64>) -> !)>
TypeKind::Named {
path: "std::option",
name: "Option",
generics: &[
TypeKind::Tuple(&[
TypeKind::Reference(TypeKind::Named {
path: "",
name: "u64",
generics: &[],
}),
TypeKind::Function {
extern: "C",
args: &[
TypeKind::Named {
path: "mycrate::mymodule",
name: "ADT",
generics: &[
TypeKind::ConstPtr(TypeKind::Named {
path: "",
name: "u64",
generics: &[],
}),
],
},
],
return: TypeKind::Named {
path: "",
name: "!",
generics: &[],
},
},
])
]
}
For majority of the use-cases this representation is irrelevant, as the Display
implementation,
which prints the fully qualified type, will be sufficient. For those, who want a precise display of
the type, the structured representation should be good enough to achieve most of the desired
outputs.
As various features in are added in the future, they might increase the size of TypeKind
enumeration. To ensure such extensibility, TypeKind
may not be exhaustively matched.
Permanently unspecified are the exact output of the Display
and similar traits as well as the
exact contents referenced by the string slices. It is expected, however, that certain ways to
concatenate the components shall result in strings resembling a rust type. That is, given a
TypeKind::Named { name, generics, ... }
, format!("{}<{}>", name, comma_separated(generics))
should always yield a sensible type.
Finally, to obtain a Type
, the following function is added to the standard library (and core
):
/// Returns a type representing the name of the `T`.
///
/// To obtain the textual representation of type `T`, use `Type`’s `Display` implementation, as
/// such:
///
/// ```rust
/// assert_eq!(format!("{}", type_name::<bool>()), "bool");
/// ```
pub const fn type_name<T>() -> Type;
First, this opens up some reflection-like capabilities. Alas, it is unclear how to prevent this
from happening and there are a number of other functions that behave the same way (e.g. size_of
).
- Why is this design the best in the space of possible designs?
- What other designs have been considered and what is the rationale for not choosing them?
- What is the impact of not doing this?
Discuss prior art, both the good and the bad, in relation to this proposal. A few examples of what this can include are:
- For language, library, cargo, tools, and compiler proposals: Does this feature exist in other programming languages and what experience have their community had?
- For community proposals: Is this done by some other community and what were their experiences with it?
- For other teams: What lessons can we learn from what other communities have done here?
- Papers: Are there any published papers or great posts that discuss this? If you have some relevant papers to refer to, this can serve as a more detailed theoretical background.
This section is intended to encourage you as an author to think about the lessons from other languages, provide readers of your RFC with a fuller picture. If there is no prior art, that is fine - your ideas are interesting to us whether they are brand new or if it is an adaptation from other languages.
Note that while precedent set by other languages is some motivation, it does not on its own motivate an RFC. Please also take into consideration that rust sometimes intentionally diverges from common language features.
- What parts of the design do you expect to resolve through the RFC process before this gets merged?
- What parts of the design do you expect to resolve through the implementation of this feature before stabilization?
- What related issues do you consider out of scope for this RFC that could be addressed in the future independently of the solution that comes out of this RFC?