Hacker News new | past | comments | ask | show | jobs | submit login

Note, you have to be quite careful with typeid - you can end up with types not matching because you have two different versions of a crate.

Normally this would be a compile time error, but using typeid can turn this into a runtime error.

https://docs.rs/assert-type-eq/0.1.0/assert_type_eq/ allows you to assert that two types are the same, forcing a compile time error for this scenario.




Off-topic mostly, but is this sort of "single line" function/macro typical for Rust crates? This seems like something I would expect from a Node library, not a serious programming language community that cares about robust software.


Macros can be quite hard to understand, so having a simple crate wrapping it with extensive documentation and tests can be quite beneficial. And yes, this also applies to node. The attack is entirely unnecessary.


This crate has no tests, and no documentation beyond a usage example; it would do much better as a copy/paste than an entire dependency, in any ecosystem. It would be great to have as part of some extended "utils" package if it can't be included in the standard library (something like C++'s boost or Java's Apache Commons).


I think this is a valid question. C++ provides std::is_same<U, T>, why do Rust users need an external dependency (especially given the fact that the type system is one of Rust's major strengths)?

My best guess, going by a comment in the source code, is that this macro will not be necessary in the future, and therefore makes less sense to put in the standard library:

> Until RFC 1977 (public dependencies) is accepted, the situation where multiple different versions of the same crate are present is possible.

https://github.com/rust-lang/rfcs/pull/1977


Rust is pretty conservative with the standard library, and macros have a really high bar historically, because they were not namespaced.


Well, being conservative[a] with the standard library is why JavaScript’s is so small (leading to things like the `is-array` package)

[a]: Not so much conservative, but just not caring


That isn't a single line macro. Did you mean a single macro crate? Most crates in rust have more than a single macro/function, but some do. Regardless, it's not at all related to how "robust" or not the language ecosystem is.


It's 17 lines of code (11 if you don't count the white space). If you find it somewhere and copy it to your utils package, that's great! If you add a dependency for every little utility you may need, you're going to end up with huge space for ecosystem attacks, as we're seeing in the Node ecosystem. Especially for something like this, where people can add it as a dependency because they don't want to understand macro code (there is another comment saying just that).


Take a look at the code: https://github.com/Metaswitch/assert-type-eq-rs/blob/master/...

I'd rather not write that myself.


I've looked at it, that is why I knew it is very little code. It's definitely something I'd copy off StackOverflow if I really needed it, but to include it as a dependency is just weird.


It is relatively common to have narrow scope crates in Rust.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: