@@ -841,6 +841,59 @@ fn comp_attrs(ctx: &GenCtx, ci: &CompInfo, name: &str, extra: &mut Vec<P<ast::It
841
841
attrs
842
842
}
843
843
844
+ fn gen_maybe_unsafe_field ( ctx : & mut GenCtx , mut field : ast:: StructField , methods : & mut Vec < ast:: ImplItem > ) -> ast:: StructField {
845
+ use syntax:: visit:: { Visitor , walk_ty} ;
846
+ struct TyVisitor < ' a > {
847
+ search : & ' a [ String ] ,
848
+ found : bool
849
+ } ;
850
+ impl < ' a > Visitor for TyVisitor < ' a > {
851
+ fn visit_ty ( & mut self , t : & ast:: Ty ) {
852
+ if let ast:: TyKind :: Path ( _, ref p) = t. node {
853
+ if p. segments . len ( ) == 1 {
854
+ for name in self . search {
855
+ if p. segments [ 0 ] . identifier . name . as_str ( ) == * * name {
856
+ self . found = true ;
857
+ return ;
858
+ }
859
+ }
860
+ }
861
+ }
862
+ walk_ty ( self , t)
863
+ }
864
+ }
865
+
866
+ if !ctx. options . unsafe_field_types . is_empty ( ) {
867
+ let mut visit = TyVisitor { search : & ctx. options . unsafe_field_types , found : false } ;
868
+ walk_ty ( & mut visit, & field. ty ) ;
869
+ if visit. found {
870
+ field. vis = ast:: Visibility :: Inherited ;
871
+ let ident = field. ident . unwrap ( ) ;
872
+ let mutable_getter_name = ctx. ext_cx . ident_of ( & format ! ( "get_{}_mut" , ident) ) ;
873
+ let getter_name = ctx. ext_cx . ident_of ( & format ! ( "get_{}" , ident) ) ;
874
+ {
875
+ let ref ty = field. ty ;
876
+ let imp = quote_item ! ( & ctx. ext_cx,
877
+ impl X {
878
+ #[ inline]
879
+ pub unsafe fn $getter_name( & self ) -> & $ty {
880
+ & self . $ident
881
+ }
882
+ pub unsafe fn $mutable_getter_name( & mut self ) -> & mut $ty {
883
+ & mut self . $ident
884
+ }
885
+ }
886
+ ) ;
887
+ match imp. unwrap ( ) . node {
888
+ ast:: ItemKind :: Impl ( _, _, _, _, _, ref items) => methods. extend ( items. clone ( ) ) ,
889
+ _ => unreachable ! ( )
890
+ }
891
+ }
892
+ return field;
893
+ }
894
+ }
895
+ field
896
+ }
844
897
845
898
fn cstruct_to_rs ( ctx : & mut GenCtx , name : & str , ci : CompInfo ) -> Vec < P < ast:: Item > > {
846
899
let layout = ci. layout ;
@@ -879,14 +932,15 @@ fn cstruct_to_rs(ctx: &mut GenCtx, name: &str, ci: CompInfo) -> Vec<P<ast::Item>
879
932
} ;
880
933
881
934
if let Some ( ref base) = base_vftable {
882
- vffields . push ( ast:: StructField {
935
+ let field = ast:: StructField {
883
936
span : ctx. span ,
884
937
vis : ast:: Visibility :: Public ,
885
938
ident : Some ( ctx. ext_cx . ident_of ( "_base" ) ) ,
886
939
id : ast:: DUMMY_NODE_ID ,
887
940
ty : P ( mk_ty ( ctx, false , & [ base. clone ( ) ] ) ) ,
888
941
attrs : vec ! [ ] ,
889
- } ) ;
942
+ } ;
943
+ vffields. push ( gen_maybe_unsafe_field ( ctx, field, & mut methods) ) ;
890
944
}
891
945
892
946
for vm in ci. vmethods . iter ( ) {
@@ -1053,15 +1107,15 @@ fn cstruct_to_rs(ctx: &mut GenCtx, name: &str, ci: CompInfo) -> Vec<P<ast::Item>
1053
1107
} else {
1054
1108
rust_ty
1055
1109
} ;
1056
-
1057
- fields. push ( ast:: StructField {
1110
+ let field = ast:: StructField {
1058
1111
span : ctx. span ,
1059
1112
ident : Some ( ctx. ext_cx . ident_of ( & f_name) ) ,
1060
1113
vis : ast:: Visibility :: Public ,
1061
1114
id : ast:: DUMMY_NODE_ID ,
1062
1115
ty : rust_ty,
1063
1116
attrs : mk_doc_attr ( ctx, & f. comment )
1064
- } ) ;
1117
+ } ;
1118
+ fields. push ( gen_maybe_unsafe_field ( ctx, field, & mut methods) ) ;
1065
1119
if bypass {
1066
1120
continue ;
1067
1121
}
0 commit comments