Skip to content

Commit

Permalink
Merge pull request #712 from dtolnay/crate
Browse files Browse the repository at this point in the history
Enable expanding type_id macro in a crate with cxx renamed
  • Loading branch information
dtolnay authored Feb 12, 2021
2 parents 997e874 + 12fced4 commit 87ed594
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
3 changes: 2 additions & 1 deletion macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::syntax::{
self, check, mangle, Api, Doc, Enum, ExternFn, ExternType, Impl, Lifetimes, Pair, Signature,
Struct, Trait, Type, TypeAlias, Types,
};
use crate::type_id::Crate;
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::mem;
Expand Down Expand Up @@ -1095,7 +1096,7 @@ fn type_id(name: &Pair) -> TokenStream {
segments.extend(namespace_segments.cloned());
segments.push(Ident::new(&name.cxx.to_string(), Span::call_site()));
let qualified = QualifiedName { segments };
crate::type_id::expand(qualified)
crate::type_id::expand(Crate::Cxx, qualified)
}

fn expand_rust_box(ident: &Ident, types: &Types, explicit_impl: Option<&Impl>) -> TokenStream {
Expand Down
13 changes: 10 additions & 3 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod type_id;
use crate::syntax::file::Module;
use crate::syntax::namespace::Namespace;
use crate::syntax::qualified::QualifiedName;
use crate::type_id::Crate;
use proc_macro::TokenStream;
use syn::parse::{Parse, ParseStream, Parser, Result};
use syn::parse_macro_input;
Expand Down Expand Up @@ -73,16 +74,22 @@ pub fn bridge(args: TokenStream, input: TokenStream) -> TokenStream {
.into()
}

#[doc(hidden)]
#[proc_macro]
pub fn type_id(input: TokenStream) -> TokenStream {
struct TypeId(QualifiedName);
struct TypeId {
krate: Crate,
path: QualifiedName,
}

impl Parse for TypeId {
fn parse(input: ParseStream) -> Result<Self> {
QualifiedName::parse_quoted_or_unquoted(input).map(TypeId)
let krate = input.parse().map(Crate::DollarCrate)?;
let path = QualifiedName::parse_quoted_or_unquoted(input)?;
Ok(TypeId { krate, path })
}
}

let arg = parse_macro_input!(input as TypeId);
type_id::expand(arg.0).into()
type_id::expand(arg.krate, arg.path).into()
}
24 changes: 19 additions & 5 deletions macro/src/type_id.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
use crate::syntax::qualified::QualifiedName;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use proc_macro2::{TokenStream, TokenTree};
use quote::{format_ident, quote, ToTokens};

pub enum Crate {
Cxx,
DollarCrate(TokenTree),
}

impl ToTokens for Crate {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Crate::Cxx => tokens.extend(quote!(::cxx)),
Crate::DollarCrate(krate) => krate.to_tokens(tokens),
}
}
}

// "folly::File" => `(f, o, l, l, y, (), F, i, l, e)`
pub fn expand(arg: QualifiedName) -> TokenStream {
pub fn expand(krate: Crate, arg: QualifiedName) -> TokenStream {
let mut ids = Vec::new();

for word in arg.segments {
Expand All @@ -14,11 +28,11 @@ pub fn expand(arg: QualifiedName) -> TokenStream {
ids.push(match ch {
'A'..='Z' | 'a'..='z' => {
let t = format_ident!("{}", ch);
quote!(::cxx::#t)
quote!(#krate::#t)
}
'0'..='9' | '_' => {
let t = format_ident!("_{}", ch);
quote!(::cxx::#t)
quote!(#krate::#t)
}
_ => quote!([(); #ch as _]),
});
Expand Down
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ mod shared_ptr;
#[path = "cxx_string.rs"]
mod string;
mod symbols;
mod type_id;
mod unique_ptr;
mod unwind;
#[path = "cxx_vector.rs"]
Expand All @@ -429,11 +430,6 @@ pub use crate::vector::CxxVector;
pub use crate::weak_ptr::WeakPtr;
pub use cxxbridge_macro::bridge;

/// For use in impls of the `ExternType` trait. See [`ExternType`].
///
/// [`ExternType`]: trait.ExternType.html
pub use cxxbridge_macro::type_id;

/// Synonym for `CxxString`.
///
/// To avoid confusion with Rust's standard library string you probably
Expand Down Expand Up @@ -466,6 +462,7 @@ pub mod private {
pub use crate::unwind::catch_unwind;
pub use crate::vector::VectorElement;
pub use crate::weak_ptr::WeakPtrTarget;
pub use cxxbridge_macro::type_id;
}

mod actually_private {
Expand Down
9 changes: 9 additions & 0 deletions src/type_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// For use in impls of the `ExternType` trait. See [`ExternType`].
///
/// [`ExternType`]: trait.ExternType.html
#[macro_export]
macro_rules! type_id {
($($path:tt)*) => {
$crate::private::type_id! { $crate $($path)* }
};
}

0 comments on commit 87ed594

Please sign in to comment.