1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
// #![deny(missing_docs, missing_debug_implementations)] //! Returns type names with a specifiable number of module segments as a `String`. //! //! ```rust //! // === std library === // //! assert_eq!( //! std::any::type_name::<Option<String>>(), //! "core::option::Option<alloc::string::String>", //! ); //! //! // === tynm === // //! // Simple type name: //! assert_eq!(tynm::type_name::<Option<String>>(), "Option<String>",); //! //! // Type name with 1 module segment, starting from the most significant module. //! assert_eq!( //! tynm::type_namem::<Option<String>>(1), //! "core::..::Option<alloc::..::String>", //! ); //! //! // Type name with 1 module segment, starting from the least significant module. //! assert_eq!( //! tynm::type_namen::<Option<String>>(1), //! "..::option::Option<..::string::String>", //! ); //! //! // Type name with 1 module segment from both the most and least significant modules. //! #[rustfmt::skip] //! mod rust_out { pub mod two { pub mod three { pub struct Struct; } } } //! assert_eq!( //! tynm::type_namemn::<rust_out::two::three::Struct>(1, 1), //! "rust_out::..::three::Struct", //! ); //! ``` //! //! # Motivation //! //! The [`std::any::type_name`] function stabilized in Rust 1.38 returns the fully qualified type //! name with all module segments. This can be difficult to read in error messages, especially for //! type-parameterized types. //! //! Often, the simple type name is more readable, and enough to distinguish the type referenced in //! an error. //! //! [`std::any::type_name`]: https://doc.rust-lang.org/std/any/fn.type_name.html pub use crate::types::TypeName; mod parser; mod types; /// Returns the simple type name. /// /// # Type Parameters /// /// * `T`: Type whose simple type name should be returned. /// /// # Examples /// /// ```rust /// assert_eq!(tynm::type_name::<Option<String>>(), "Option<String>",); /// ``` pub fn type_name<T>() -> String { type_namemn::<T>(0, 0) } /// Returns the type name with at most `m` most significant module path segments. /// /// # Parameters /// /// * `m`: Number of most significant module path segments to include. /// /// # Type Parameters /// /// * `T`: Type whose simple type name should be returned. /// /// # Examples /// /// ```rust /// assert_eq!( /// tynm::type_namem::<Option<String>>(1), /// "core::..::Option<alloc::..::String>", /// ); /// ``` pub fn type_namem<T>(m: usize) -> String { type_namemn::<T>(m, 0) } /// Returns the type name with at most `n` least significant module path segments. /// /// # Parameters /// /// * `n`: Number of least significant module path segments to include. /// /// # Type Parameters /// /// * `T`: Type whose simple type name should be returned. /// /// # Examples /// /// ```rust /// assert_eq!( /// tynm::type_namen::<Option<String>>(1), /// "..::option::Option<..::string::String>", /// ); /// ``` pub fn type_namen<T>(n: usize) -> String { type_namemn::<T>(0, n) } /// Returns the type name with `m` most significant, and `n` least significant module path segments. /// /// # Parameters /// /// * `m`: Number of most significant module path segments to include. /// * `n`: Number of least significant module path segments to include. /// /// # Type Parameters /// /// * `T`: Type whose simple type name should be returned. /// /// # Examples /// /// ```rust /// assert_eq!( /// tynm::type_namen::<Option<String>>(1), /// "..::option::Option<..::string::String>", /// ); /// ``` pub fn type_namemn<T>(m: usize, n: usize) -> String { let type_name_qualified = std::any::type_name::<T>(); let type_name = TypeName::from(type_name_qualified); type_name.as_str_mn(m, n) }