• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-275164

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D10-Jul-202074 65

.gitignoreH A D10-Jul-202045 65

.travis.ymlH A D10-Jul-202068 96

Cargo.tomlH A D10-Jul-20201.1 KiB3531

Cargo.toml.orig-cargoH A D10-Jul-2020583 2320

LICENSE-APACHEH A D10-Jul-202010.6 KiB202169

LICENSE-MITH A D10-Jul-20201,023 2421

README.mdH A D10-Jul-20202.1 KiB5436

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