@@ -4,9 +4,10 @@ use crate::syntax::namespace::Namespace;
4
4
use crate :: syntax:: report:: Errors ;
5
5
use crate :: syntax:: symbol:: Symbol ;
6
6
use crate :: syntax:: {
7
- self , check, mangle, Api , Enum , ExternFn , ExternType , Signature , Struct , Type , TypeAlias , Types ,
7
+ self , check, mangle, Api , Enum , ExternFn , ExternType , Impl , Signature , Struct , Type , TypeAlias ,
8
+ Types ,
8
9
} ;
9
- use proc_macro2:: { Ident , TokenStream } ;
10
+ use proc_macro2:: { Ident , Span , TokenStream } ;
10
11
use quote:: { format_ident, quote, quote_spanned, ToTokens } ;
11
12
use std:: mem;
12
13
use syn:: { parse_quote, Result , Token } ;
@@ -62,6 +63,7 @@ fn expand(ffi: Module, apis: &[Api], types: &Types) -> TokenStream {
62
63
}
63
64
64
65
for ty in types {
66
+ let explicit_impl = types. explicit_impls . get ( ty) ;
65
67
if let Type :: RustBox ( ty) = ty {
66
68
if let Type :: Ident ( ident) = & ty. inner {
67
69
if Atom :: from ( ident) . is_none ( ) {
@@ -77,20 +79,20 @@ fn expand(ffi: Module, apis: &[Api], types: &Types) -> TokenStream {
77
79
} else if let Type :: UniquePtr ( ptr) = ty {
78
80
if let Type :: Ident ( ident) = & ptr. inner {
79
81
if Atom :: from ( ident) . is_none ( )
80
- && ( !types . aliases . contains_key ( ident ) || types. explicit_impls . contains ( ty ) )
82
+ && ( explicit_impl . is_some ( ) || ! types. aliases . contains_key ( ident ) )
81
83
{
82
- expanded. extend ( expand_unique_ptr ( namespace, ident, types) ) ;
84
+ expanded. extend ( expand_unique_ptr ( namespace, ident, types, explicit_impl ) ) ;
83
85
}
84
86
}
85
87
} else if let Type :: CxxVector ( ptr) = ty {
86
88
if let Type :: Ident ( ident) = & ptr. inner {
87
89
if Atom :: from ( ident) . is_none ( )
88
- && ( !types . aliases . contains_key ( ident ) || types. explicit_impls . contains ( ty ) )
90
+ && ( explicit_impl . is_some ( ) || ! types. aliases . contains_key ( ident ) )
89
91
{
90
92
// Generate impl for CxxVector<T> if T is a struct or opaque
91
93
// C++ type. Impl for primitives is already provided by cxx
92
94
// crate.
93
- expanded. extend ( expand_cxx_vector ( namespace, ident) ) ;
95
+ expanded. extend ( expand_cxx_vector ( namespace, ident, explicit_impl ) ) ;
94
96
}
95
97
}
96
98
}
@@ -784,7 +786,12 @@ fn expand_rust_vec(namespace: &Namespace, elem: &Ident) -> TokenStream {
784
786
}
785
787
}
786
788
787
- fn expand_unique_ptr ( namespace : & Namespace , ident : & Ident , types : & Types ) -> TokenStream {
789
+ fn expand_unique_ptr (
790
+ namespace : & Namespace ,
791
+ ident : & Ident ,
792
+ types : & Types ,
793
+ explicit_impl : Option < & Impl > ,
794
+ ) -> TokenStream {
788
795
let name = ident. to_string ( ) ;
789
796
let prefix = format ! ( "cxxbridge04$unique_ptr${}{}$" , namespace, ident) ;
790
797
let link_null = format ! ( "{}null" , prefix) ;
@@ -810,8 +817,13 @@ fn expand_unique_ptr(namespace: &Namespace, ident: &Ident, types: &Types) -> Tok
810
817
None
811
818
} ;
812
819
813
- quote ! {
814
- unsafe impl :: cxx:: private:: UniquePtrTarget for #ident {
820
+ let begin_span =
821
+ explicit_impl. map_or_else ( Span :: call_site, |explicit| explicit. impl_token . span ) ;
822
+ let end_span = explicit_impl. map_or_else ( Span :: call_site, |explicit| explicit. brace_token . span ) ;
823
+ let unsafe_token = format_ident ! ( "unsafe" , span = begin_span) ;
824
+
825
+ quote_spanned ! { end_span=>
826
+ #unsafe_token impl :: cxx:: private:: UniquePtrTarget for #ident {
815
827
const __NAME: & ' static dyn :: std:: fmt:: Display = & #name;
816
828
fn __null( ) -> * mut :: std:: ffi:: c_void {
817
829
extern "C" {
@@ -857,7 +869,12 @@ fn expand_unique_ptr(namespace: &Namespace, ident: &Ident, types: &Types) -> Tok
857
869
}
858
870
}
859
871
860
- fn expand_cxx_vector ( namespace : & Namespace , elem : & Ident ) -> TokenStream {
872
+ fn expand_cxx_vector (
873
+ namespace : & Namespace ,
874
+ elem : & Ident ,
875
+ explicit_impl : Option < & Impl > ,
876
+ ) -> TokenStream {
877
+ let _ = explicit_impl;
861
878
let name = elem. to_string ( ) ;
862
879
let prefix = format ! ( "cxxbridge04$std$vector${}{}$" , namespace, elem) ;
863
880
let link_size = format ! ( "{}size" , prefix) ;
@@ -869,8 +886,13 @@ fn expand_cxx_vector(namespace: &Namespace, elem: &Ident) -> TokenStream {
869
886
let link_unique_ptr_release = format ! ( "{}release" , unique_ptr_prefix) ;
870
887
let link_unique_ptr_drop = format ! ( "{}drop" , unique_ptr_prefix) ;
871
888
872
- quote ! {
873
- unsafe impl :: cxx:: private:: VectorElement for #elem {
889
+ let begin_span =
890
+ explicit_impl. map_or_else ( Span :: call_site, |explicit| explicit. impl_token . span ) ;
891
+ let end_span = explicit_impl. map_or_else ( Span :: call_site, |explicit| explicit. brace_token . span ) ;
892
+ let unsafe_token = format_ident ! ( "unsafe" , span = begin_span) ;
893
+
894
+ quote_spanned ! { end_span=>
895
+ #unsafe_token impl :: cxx:: private:: VectorElement for #elem {
874
896
const __NAME: & ' static dyn :: std:: fmt:: Display = & #name;
875
897
fn __vector_size( v: & :: cxx:: CxxVector <Self >) -> usize {
876
898
extern "C" {
0 commit comments