use std::any::{TypeId, type_name};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::hash::{Hash, Hasher};
#[derive(Clone, Copy)]
pub struct TypeName {
id: TypeId,
name: &'static str,
}
impl TypeName {
pub fn of<T: ?Sized + 'static>() -> TypeName {
TypeName { id: TypeId::of::<T>(), name: type_name::<T>() }
}
pub fn colliding(names: impl IntoIterator<Item = TypeName>) -> HashSet<TypeName> {
let mut by_text: HashMap<String, Vec<TypeName>> = HashMap::new();
for name in names {
let alike = by_text.entry(name.to_string()).or_default();
if !alike.contains(&name) {
alike.push(name);
}
}
by_text.into_values().filter(|alike| alike.len() > 1).flatten().collect()
}
pub(crate) fn from_parts(id: TypeId, name: &'static str) -> TypeName {
TypeName { id, name }
}
pub(crate) fn id(&self) -> TypeId {
self.id
}
pub(crate) fn full(&self) -> &'static str {
self.name
}
pub(crate) fn written(&self, full: &HashSet<TypeName>) -> String {
if full.contains(self) { self.name.to_owned() } else { short_type_name(self.name) }
}
}
impl PartialEq for TypeName {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for TypeName {}
impl Hash for TypeName {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl fmt::Display for TypeName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() { f.pad(self.name) } else { f.pad(&short_type_name(self.name)) }
}
}
impl fmt::Debug for TypeName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
pub(crate) fn short_type_name(full: &'static str) -> String {
let mut out = String::with_capacity(full.len());
let mut rest = full;
while let Some(c) = rest.chars().next() {
if is_path_char(c) {
let end = rest.find(|c: char| !is_path_char(c)).unwrap_or(rest.len());
push_last_segment(&mut out, &rest[..end]);
rest = &rest[end..];
} else {
out.push(c);
rest = &rest[c.len_utf8()..];
}
}
out
}
fn is_path_char(c: char) -> bool {
c.is_alphanumeric() || matches!(c, '_' | ':' | '{' | '}')
}
fn push_last_segment(out: &mut String, path: &str) {
let path = match path.strip_prefix("::") {
Some(tail) => {
out.push_str("::");
tail
}
None => path,
};
let mut segments = path.rsplit("::");
let last = segments.next().unwrap_or("");
if last.starts_with('{') {
if let Some(enclosing) = segments.next() {
out.push_str(enclosing);
out.push_str("::");
}
}
out.push_str(last);
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use std::sync::Arc;
use super::*;
mod a {
pub struct Config;
pub struct Wrapper<T>(pub T);
}
mod b {
pub struct Config;
}
struct Solo;
trait Repo {}
type AliasOfConfig = a::Config;
fn name<T: ?Sized + 'static>() -> TypeName {
TypeName::of::<T>()
}
fn set(names: impl IntoIterator<Item = TypeName>) -> HashSet<TypeName> {
TypeName::colliding(names)
}
#[test]
fn display_cuts_every_path_segment_and_the_alternate_form_keeps_them() {
assert_eq!(name::<a::Config>().to_string(), "Config");
let full = format!("{:#}", name::<a::Config>());
assert!(full.ends_with("tests::a::Config") && full.len() > "Config".len(), "{full}");
}
#[test]
fn generic_arguments_are_cut_too() {
assert_eq!(name::<Arc<a::Config>>().to_string(), "Arc<Config>");
assert_eq!(name::<a::Wrapper<Arc<b::Config>>>().to_string(), "Wrapper<Arc<Config>>");
assert_eq!(name::<Vec<(a::Config, &'static b::Config)>>().to_string(), "Vec<(Config, &Config)>");
}
#[test]
fn the_punctuation_around_paths_is_copied_as_written() {
assert_eq!(name::<dyn Repo + Send + Sync>().to_string(), "dyn Repo + Send + Sync");
assert_eq!(name::<[a::Config; 3]>().to_string(), "[Config; 3]");
assert_eq!(name::<fn(a::Config) -> b::Config>().to_string(), "fn(Config) -> Config");
}
#[test]
fn a_closure_segment_keeps_the_item_that_encloses_it() {
fn enclosing() -> TypeName {
fn name_of<T: 'static>(_: &T) -> TypeName {
TypeName::of::<T>()
}
name_of(&|| ())
}
assert_eq!(enclosing().to_string(), "enclosing::{{closure}}");
}
#[test]
fn two_names_are_equal_when_their_types_are_not_when_their_spellings_are() {
assert_eq!(name::<AliasOfConfig>(), name::<a::Config>());
assert_ne!(name::<a::Config>(), name::<b::Config>());
assert_ne!(name::<dyn Repo>(), name::<dyn Repo + Send>());
}
#[test]
fn colliding_finds_equal_short_names_of_different_types() {
let found = set([name::<a::Config>(), name::<b::Config>()]);
assert_eq!(found, HashSet::from([name::<a::Config>(), name::<b::Config>()]));
}
#[test]
fn colliding_ignores_the_same_type_listed_twice_and_names_that_differ() {
assert!(set([name::<a::Config>(), name::<a::Config>(), name::<AliasOfConfig>()]).is_empty());
assert!(set([name::<a::Config>(), name::<Solo>()]).is_empty());
assert!(set([]).is_empty());
}
#[test]
fn colliding_returns_only_the_names_that_collide() {
let found = set([name::<a::Config>(), name::<Solo>(), name::<b::Config>(), name::<Solo>()]);
assert!(found.contains(&name::<a::Config>()) && found.contains(&name::<b::Config>()));
assert!(!found.contains(&name::<Solo>()));
assert_eq!(found.len(), 2);
}
#[test]
fn colliding_compares_the_cut_form_so_generic_arguments_count() {
let found = set([name::<Arc<a::Config>>(), name::<Arc<b::Config>>(), name::<a::Config>()]);
assert!(found.contains(&name::<Arc<a::Config>>()) && found.contains(&name::<Arc<b::Config>>()));
assert!(!found.contains(&name::<a::Config>()), "`Config` and `Arc<Config>` print apart");
}
#[test]
fn written_gives_the_full_path_to_the_names_in_the_set_only() {
let full = set([name::<a::Config>(), name::<b::Config>()]);
assert_eq!(name::<a::Config>().written(&full), format!("{:#}", name::<a::Config>()));
assert_eq!(name::<Solo>().written(&full), "Solo");
assert_eq!(name::<a::Config>().written(&HashSet::new()), "Config");
}
}