]>
git.proxmox.com Git - rustc.git/blob - vendor/darling_core/src/util/ident_string.rs
2 use std
::hash
::{Hash, Hasher}
;
4 use proc_macro2
::{Span, TokenStream}
;
6 use syn
::{Ident, Meta}
;
8 use {FromMeta, Result}
;
10 /// A wrapper for an `Ident` which also keeps the value as a string.
12 /// This struct can be used to perform string comparisons and operations.
13 #[derive(Clone, PartialOrd, Ord)]
14 pub struct IdentString
{
20 /// Create a new `IdentString`.
21 pub fn new(ident
: Ident
) -> Self {
23 string
: ident
.to_string(),
28 /// Get the ident as a `proc_macro2::Ident`.
29 pub fn as_ident(&self) -> &Ident
{
33 /// Get the ident as a string.
34 pub fn as_str(&self) -> &str {
38 /// Get the location of this `Ident` in source.
39 pub fn span(&self) -> Span
{
43 /// Apply some transform to the ident's string representation.
46 /// This will panic if the transform produces an invalid ident.
47 pub fn map
<F
, S
>(self, map_fn
: F
) -> Self
49 F
: FnOnce(String
) -> S
,
52 let span
= self.span();
53 let string
= map_fn(self.string
);
54 Ident
::new(string
.as_ref(), span
).into()
58 impl AsRef
<Ident
> for IdentString
{
59 fn as_ref(&self) -> &Ident
{
64 impl AsRef
<str> for IdentString
{
65 fn as_ref(&self) -> &str {
70 impl From
<Ident
> for IdentString
{
71 fn from(ident
: Ident
) -> Self {
72 IdentString
::new(ident
)
76 impl From
<IdentString
> for Ident
{
77 fn from(v
: IdentString
) -> Ident
{
82 impl From
<IdentString
> for String
{
83 fn from(v
: IdentString
) -> String
{
88 impl Eq
for IdentString {}
90 impl PartialEq
for IdentString
{
91 fn eq(&self, rhs
: &Self) -> bool
{
92 self.ident
== rhs
.ident
96 impl PartialEq
<String
> for IdentString
{
97 fn eq(&self, rhs
: &String
) -> bool
{
102 impl<'a
> PartialEq
<&'a
str> for IdentString
{
103 fn eq(&self, rhs
: &&str) -> bool
{
104 self.as_str() == *rhs
108 impl Hash
for IdentString
{
109 fn hash
<H
: Hasher
>(&self, state
: &mut H
) {
110 self.ident
.hash(state
);
114 impl ToTokens
for IdentString
{
115 fn to_tokens(&self, tokens
: &mut TokenStream
) {
116 self.ident
.to_tokens(tokens
);
120 impl fmt
::Debug
for IdentString
{
121 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
122 write
!(f
, "{:?}", self.ident
)
126 impl fmt
::Display
for IdentString
{
127 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
128 write
!(f
, "{}", self.ident
)
132 impl FromMeta
for IdentString
{
133 fn from_meta(item
: &Meta
) -> Result
<Self> {
134 Ident
::from_meta(item
).map(IdentString
::from
)
140 use super::IdentString
;
144 let i_str
= IdentString
::new(parse_quote
!(t
));
145 assert_eq
!(i_str
.as_str(), "t");
150 let i
= IdentString
::new(parse_quote
!(my
));
151 let after
= i
.map(|v
| format
!("var_{}", v
));
152 assert_eq
!(after
, "var_my");
153 assert_eq
!(after
, String
::from("var_my"));