Giter Club home page Giter Club logo

tynm's Introduction

🪶 Tynm -- Type Name

Crates.io docs.rs CI Coverage Status

Returns type names with a specifiable number of module segments as a String.

Usage

Add the following to Cargo.toml

tynm = "0.1.10"

In code:

#[rustfmt::skip]
assert_eq!(
    core::any::type_name::<Option<String>>(), "core::option::Option<alloc::string::String>"
);

#[rustfmt::skip]
let tuples = vec![
    (tynm::type_name::<Option<String>>(),    "Option<String>"),
    (tynm::type_namem::<Option<String>>(1),  "core::..::Option<alloc::..::String>"),
    (tynm::type_namen::<Option<String>>(1),  "..::option::Option<..::string::String>"),
    // 1 segment from most and least significant modules.
    (tynm::type_namemn::<rust_out::two::three::Struct>(1, 1), "rust_out::..::three::Struct"),
    // traits
    (tynm::type_name::<dyn core::fmt::Debug>(), "dyn Debug"),
];

tuples
    .iter()
    .for_each(|(left, right)| assert_eq!(left, right));

Motivation

The core::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.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

tynm's People

Contributors

azriel91 avatar cryptjar avatar diggsey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tynm's Issues

Suggestions `#![no_std]` Use `core` and `alloc` rather than `std`

This would greatly help for no std environment or for ensuring that this crate does not perform IO, which is useful for wasm support.

I think this would be accomplished by adding #![no_std] and switching imports and importing things such as String and Vec which are no longer the prelude.

I have used this crate inside flax to greatly reduce error message noise due to heavy use of generics in Queries. Unfortunately I had to temporarily remove tynm in order to support no_std, but it would be great to see this crate back.

Thank you.

Relax `Sized` bound

Currently it's not possible to get the name of unsized types, eg. dyn Trait.

Skips first type parameter if it's a primitive type

I'm trying to use tynm for governor, and noticed behavior that looks like a bug:

assert_eq!(tynm::type_name::<std::collections::HashMap<u32, String>>(), "HashMap<u32, String>",);

fails:

  left: `"HashMap<, String>"`,
 right: `"HashMap<u32, String>"`', src/lib.rs:6:1

I guess somehow the first parameter gets dropped when the type is a builtin? It does work for HashMap<String, String>.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.