Debian Codegraph @

debian/rust-vendor/proc-macro-crate/README.md

raw
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
6 Providing support for `$crate` in procedural macros.
7
8 * [Introduction](#introduction)
9 * [Example](#example)
10 * [License](#license)
11
12 ### Introduction
13
14 In `macro_rules!` `$crate` is used to get the path of the crate where a macro is declared in. In
15 procedural macros there is currently no easy way to get this path. A common hack is to import the
16 desired 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.
18 However, this breaks importing the crate, as the proc-macro developer does not know the renamed
19 name of the crate that should be imported.
20
21 This crate provides a way to get the name of a crate, even if it renamed in `Cargo.toml`. For this
22 purpose a single function `crate_name` is provided. This function needs to be called in the context
23 of a proc-macro with the name of the desired crate. `CARGO_MANIFEST_DIR` will be used to find the
24 current active `Cargo.toml` and this `Cargo.toml` is searched for the desired crate.
25
26 ### Example
27
28 ```rust
29 use quote::quote;
30 use syn::Ident;
31 use proc_macro2::Span;
32 use proc_macro_crate::{crate_name, FoundCrate};
33
34 fn import_my_crate() {
35 let found_crate = crate_name("my-crate").expect("my-crate is present in `Cargo.toml`");
36
37 match found_crate {
38 FoundCrate::Itself => quote!( crate::Something ),
39 FoundCrate::Name(name) => {
40 let ident = Ident::new(&name, Span::call_site());
41 quote!( #ident::Something )
42 }
43 };
44 }
45
46 ```
47
48 ### Edge cases
49
50 There are multiple edge cases when it comes to determining the correct crate. If you for example
51 import a crate as its own dependency, like this:
52
53 ```toml
54 [package]
55 name = "my_crate"
56
57 [dev-dependencies]
58 my_crate = { version = "0.1", features = [ "test-feature" ] }
59 ```
60
61 The crate will return `FoundCrate::Itself` and you will not be able to find the other instance
62 of your crate in `dev-dependencies`. Other similar cases are when one crate is imported multiple
63 times:
64
65 ```toml
66 [package]
67 name = "my_crate"
68
69 [dependencies]
70 some-crate = { version = "0.5" }
71 some-crate-old = { package = "some-crate", version = "0.1" }
72 ```
73
74 When searching for `some-crate` in this `Cargo.toml` it will return `FoundCrate::Name("some_old_crate")`,
75 aka the last definition of the crate in the `Cargo.toml`.
76
77 ### License
78
79 Licensed under either of
80
81 * [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
82
83 * [MIT license](https://opensource.org/licenses/MIT)
84
85 at your option.
86
87 License: MIT OR Apache-2.0