Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 03-May-2022 | - | ||||
src/ | H | 03-May-2022 | - | 275 | 164 | |
.cargo-checksum.json | H A D | 03-May-2022 | 89 | 1 | 1 | |
.cargo_vcs_info.json | H A D | 10-Jul-2020 | 74 | 6 | 5 | |
.gitignore | H A D | 10-Jul-2020 | 45 | 6 | 5 | |
.travis.yml | H A D | 10-Jul-2020 | 68 | 9 | 6 | |
Cargo.toml | H A D | 10-Jul-2020 | 1.1 KiB | 35 | 31 | |
Cargo.toml.orig-cargo | H A D | 10-Jul-2020 | 583 | 23 | 20 | |
LICENSE-APACHE | H A D | 10-Jul-2020 | 10.6 KiB | 202 | 169 | |
LICENSE-MIT | H A D | 10-Jul-2020 | 1,023 | 24 | 21 | |
README.md | H A D | 10-Jul-2020 | 2.1 KiB | 54 | 36 |
README.md
1# proc-macro-crate 2 3 4[![](https://docs.rs/proc-macro-crate/badge.svg)](https://docs.rs/proc-macro-crate/) [![](https://img.shields.io/crates/v/proc-macro-crate.svg)](https://crates.io/crates/proc-macro-crate) [![](https://img.shields.io/crates/d/proc-macro-crate.png)](https://crates.io/crates/proc-macro-crate) [![Build Status](https://travis-ci.org/bkchr/proc-macro-crate.png?branch=master)](https://travis-ci.org/bkchr/proc-macro-crate) 5 6Providing support for `$crate` in procedural macros. 7 8* [Introduction](#introduction) 9* [Example](#example) 10* [License](#license) 11 12### Introduction 13 14In `macro_rules!` `$crate` is used to get the path of the crate where a macro is declared in. In 15procedural macros there is currently no easy way to get this path. A common hack is to import the 16desired crate with a know name and use this. However, with rust edition 2018 and dropping 17`extern crate` declarations from `lib.rs`, people start to rename crates in `Cargo.toml` directly. 18However, this breaks importing the crate, as the proc-macro developer does not know the renamed 19name of the crate that should be imported. 20 21This crate provides a way to get the name of a crate, even if it renamed in `Cargo.toml`. For this 22purpose a single function `crate_name` is provided. This function needs to be called in the context 23of a proc-macro with the name of the desired crate. `CARGO_MANIFEST_DIR` will be used to find the 24current active `Cargo.toml` and this `Cargo.toml` is searched for the desired crate. The returned 25name of `crate_name` is either the given original rename (crate not renamed) or the renamed name. 26 27### Example 28 29```rust 30use quote::quote; 31use syn::Ident; 32use proc_macro2::Span; 33use proc_macro_crate::crate_name; 34 35fn import_my_crate() { 36 let name = crate_name("my-crate").expect("my-crate is present in `Cargo.toml`"); 37 let ident = Ident::new(&name, Span::call_site()); 38 quote!( extern crate #ident as my_crate_known_name ); 39} 40 41``` 42 43### License 44 45Licensed under either of 46 47 * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) 48 49 * [MIT license](http://opensource.org/licenses/MIT) 50 51at your option. 52 53License: Apache-2.0/MIT 54