pub trait TypeName {
// Required method
fn fmt(f: &mut Formatter<'_>) -> Result;
// Provided methods
fn type_name() -> String { ... }
fn type_name_of(&self) -> String { ... }
}
Expand description
Trait which returns the canonical name of the implementing type.
§Derivable
This trait can be used with #[derive(TypeName)]
. The derived impl will require that
any generic parameters to the type implement TypeName
as well.
§How can I implement TypeName
?
TypeName
requires that the fmt
function formats the type name exactly
the same way as returned by the
std::intrinsics::type_name
intrinsic. It requires that the name contains the fully qualified path to the type
(which can be obtained through the module_path!()
macro), as well as
the concrete values of its generic parameters.
Required Methods§
Provided Methods§
Sourcefn type_name() -> String
fn type_name() -> String
Returns the canoncial, concrete name of a type as a string.
§Examples
use typename::TypeName;
assert_eq!(String::type_name(), "std::string::String");
Sourcefn type_name_of(&self) -> String
fn type_name_of(&self) -> String
Returns the canoncial type of a value as a string.
§Examples
use typename::TypeName;
assert_eq!(vec![0, 1, 2].type_name_of(), "std::vec::Vec<i32>");
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.