Skip to content

Support type aliases across bridges using different C++ namespaces #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gen/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ fn parse_args(attr: &Attribute) -> Result<Namespace> {
if attr.tokens.is_empty() {
Ok(Namespace::none())
} else {
attr.parse_args()
attr.parse_args_with(Namespace::parse_bridge_attr_namespace)
}
}
7 changes: 5 additions & 2 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::syntax::file::Module;
use crate::syntax::namespace::Namespace;
use crate::syntax::qualified::QualifiedName;
use proc_macro::TokenStream;
use syn::parse::{Parse, ParseStream, Result};
use syn::parse::{Parse, ParseStream, Parser, Result};
use syn::parse_macro_input;

/// `#[cxx::bridge] mod ffi { ... }`
Expand All @@ -41,7 +41,10 @@ use syn::parse_macro_input;
pub fn bridge(args: TokenStream, input: TokenStream) -> TokenStream {
let _ = syntax::error::ERRORS;

let namespace = parse_macro_input!(args as Namespace);
let namespace = match Namespace::parse_bridge_attr_namespace.parse(args) {
Ok(ns) => ns,
Err(err) => return err.to_compile_error().into(),
};
let mut ffi = parse_macro_input!(input as Module);
ffi.namespace = namespace;

Expand Down
4 changes: 1 addition & 3 deletions syntax/attrs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::syntax::qualified::QualifiedName;
use crate::syntax::report::Errors;
use crate::syntax::Atom::{self, *};
use crate::syntax::{Derive, Doc, Namespace};
Expand Down Expand Up @@ -114,6 +113,5 @@ fn parse_repr_attribute(input: ParseStream) -> Result<Atom> {

fn parse_namespace_attribute(input: ParseStream) -> Result<Namespace> {
input.parse::<Token![=]>()?;
let name = input.call(QualifiedName::parse_quoted_or_unquoted)?;
Ok(Namespace::from(name))
input.parse::<Namespace>()
}
24 changes: 11 additions & 13 deletions syntax/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,24 @@ impl Namespace {
segments.push(ident.to_string());
segments.join("::")
}
}

impl From<QualifiedName> for Namespace {
fn from(value: QualifiedName) -> Namespace {
Namespace {
segments: value.segments,
pub fn parse_bridge_attr_namespace(input: ParseStream) -> Result<Namespace> {
if input.is_empty() {
return Ok(Namespace::none());
}

input.parse::<kw::namespace>()?;
input.parse::<Token![=]>()?;
let ns = input.parse::<Namespace>()?;
input.parse::<Option<Token![,]>>()?;
Ok(ns)
}
}

impl Parse for Namespace {
fn parse(input: ParseStream) -> Result<Self> {
if !input.is_empty() {
input.parse::<kw::namespace>()?;
input.parse::<Token![=]>()?;
let name = input.call(QualifiedName::parse_quoted_or_unquoted)?;
input.parse::<Option<Token![,]>>()?;
return Ok(Namespace::from(name));
}
Ok(Namespace::none())
let segments = QualifiedName::parse_quoted_or_unquoted(input)?.segments;
Ok(Namespace { segments })
}
}

Expand Down