silentbyte / nameof Goto Github PK
View Code? Open in Web Editor NEWProvides a Rust macro to determine the string name of a binding, type, or function.
Home Page: https://crates.io/crates/nameof
License: MIT License
Provides a Rust macro to determine the string name of a binding, type, or function.
Home Page: https://crates.io/crates/nameof
License: MIT License
Given this code:
static mut TEST: u8 = 0;
fn main() {
println!("{}", ::nameof::name_of!(TEST));
}
the compiler fails with this error:
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> src/main.rs:4:20
|
4 | println!("{}", ::nameof::name_of!(TEST));
| ^^^^^^^^^^^^^^^^^^^^^^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: this error originates in the macro `::nameof::name_of` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0133`.
error: could not compile `bug-repro-rust-nameof-unsafe` due to previous error
It's currently not possible to use name_of_type!(Self)
to get the actual type name. If there is a way to support this, that would be great, but until then, it would be nice to automatically suppress clippy::use_self
warning in the macro.
return crate::test &str
The release should have been a feature release, because a crate might depend on the new functionality introduced with 1.0.2 (being able to use name_of!(type ...)
without importing name_of_type!
).
This might be an issue in the following situation:
Given that crate A depends on crate B and the following versions:
crate | Cargo.toml | Cargo.lock |
---|---|---|
crate A | - | 1.0.1 |
crate B | 1.0 | - |
When crate B uses the new feature from 1.0.2, crate A fails to build.
I recommend releasing two new versions:
Also 1.0.2 should be yanked from crates.io.
Co-written-by: @bash
Originally posted by @jeremystucki in #1 (comment)
warning: unused borrow that must be used
--> /home/julian/.cargo/registry/src/github.com-1ecc6299db9ec823/nameof-1.2.1/src/lib.rs:86:13
|
82 | / macro_rules! name_of {
83 | | // Covers Bindings
84 | | ($n: ident) => {{
85 | | let _ = || {
86 | | &$n;
| | ^^^ the borrow produces a value
... |
110 | | }};
111 | | }
| |_- in this expansion of `name_of!`
|
::: src/keyboard.rs:202:45
|
202 | debug_tuple = f.debug_tuple(name_of!(XkbSym));
| ---------------- in this macro invocation
In new nightly name_of!
macro generates "unused closure that must be used" warning.
I think, let _ =
should be added to suppress the warning.
Hi,
I'm trying to implement Debug or Format for enums (yes, I know about the derive) and
don't want to hardcode enum tag names as strings.
Potentially, enum tags can change names in future and I want to be sure that print-ed values will follow names.
Was thinking about using nameof
for this. For instance:
enum Color {
Red(i32),
Green(i32),
Blue(i32),
}
name_of!(enum Color::Green(42));
// or
tag_of!(Color::Green(42));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.