@@ -45,10 +45,7 @@ impl Cursor {
45
45
///
46
46
/// The USR can be used to compare entities across translation units.
47
47
pub fn usr ( & self ) -> Option < String > {
48
- let s = String_ {
49
- x : unsafe { clang_getCursorUSR ( self . x ) } ,
50
- }
51
- . to_string ( ) ;
48
+ let s: String = unsafe { clang_getCursorUSR ( self . x ) } . into ( ) ;
52
49
if s. is_empty ( ) { None } else { Some ( s) }
53
50
}
54
51
@@ -66,35 +63,20 @@ impl Cursor {
66
63
67
64
/// Get this cursor's referent's spelling.
68
65
pub fn spelling ( & self ) -> String {
69
- unsafe {
70
- String_ {
71
- x : clang_getCursorSpelling ( self . x ) ,
72
- }
73
- . to_string ( )
74
- }
66
+ unsafe { clang_getCursorSpelling ( self . x ) . into ( ) }
75
67
}
76
68
77
69
/// Get this cursor's referent's display name.
78
70
///
79
71
/// This is not necessarily a valid identifier. It includes extra
80
72
/// information, such as parameters for a function, etc.
81
73
pub fn display_name ( & self ) -> String {
82
- unsafe {
83
- String_ {
84
- x : clang_getCursorDisplayName ( self . x ) ,
85
- }
86
- . to_string ( )
87
- }
74
+ unsafe { clang_getCursorDisplayName ( self . x ) . into ( ) }
88
75
}
89
76
90
77
/// Get the mangled name of this cursor's referent.
91
78
pub fn mangling ( & self ) -> String {
92
- unsafe {
93
- String_ {
94
- x : clang_Cursor_getMangling ( self . x ) ,
95
- }
96
- . to_string ( )
97
- }
79
+ unsafe { clang_Cursor_getMangling ( self . x ) . into ( ) }
98
80
}
99
81
100
82
/// Get the `Cursor` for this cursor's referent's lexical parent.
@@ -246,12 +228,8 @@ impl Cursor {
246
228
247
229
/// Get the raw declaration comment for this referent, if one exists.
248
230
pub fn raw_comment ( & self ) -> Option < String > {
249
- let s = unsafe {
250
- String_ {
251
- x : clang_Cursor_getRawCommentText ( self . x ) ,
252
- }
253
- . to_string ( )
254
- } ;
231
+ let s: String =
232
+ unsafe { clang_Cursor_getRawCommentText ( self . x ) . into ( ) } ;
255
233
if s. is_empty ( ) { None } else { Some ( s) }
256
234
}
257
235
@@ -604,12 +582,7 @@ impl Type {
604
582
605
583
/// Get a raw display name for this type.
606
584
pub fn spelling ( & self ) -> String {
607
- unsafe {
608
- String_ {
609
- x : clang_getTypeSpelling ( self . x ) ,
610
- }
611
- . to_string ( )
612
- }
585
+ unsafe { clang_getTypeSpelling ( self . x ) . into ( ) }
613
586
}
614
587
615
588
/// Is this type const qualified?
@@ -872,12 +845,7 @@ impl Comment {
872
845
/// Given that this comment is the start or end of an HTML tag, get its tag
873
846
/// name.
874
847
pub fn get_tag_name ( & self ) -> String {
875
- unsafe {
876
- String_ {
877
- x : clang_HTMLTagComment_getTagName ( self . x ) ,
878
- }
879
- . to_string ( )
880
- }
848
+ unsafe { clang_HTMLTagComment_getTagName ( self . x ) . into ( ) }
881
849
}
882
850
883
851
/// Given that this comment is an HTML start tag, get its attributes.
@@ -934,18 +902,12 @@ impl Iterator for CommentAttributesIterator {
934
902
let idx = self . index ;
935
903
self . index += 1 ;
936
904
Some ( CommentAttribute {
937
- name : String_ {
938
- x : unsafe {
939
- clang_HTMLStartTag_getAttrName ( self . x , idx)
940
- } ,
941
- }
942
- . to_string ( ) ,
943
- value : String_ {
944
- x : unsafe {
945
- clang_HTMLStartTag_getAttrValue ( self . x , idx)
946
- } ,
947
- }
948
- . to_string ( ) ,
905
+ name : unsafe {
906
+ clang_HTMLStartTag_getAttrName ( self . x , idx) . into ( )
907
+ } ,
908
+ value : unsafe {
909
+ clang_HTMLStartTag_getAttrValue ( self . x , idx) . into ( )
910
+ } ,
949
911
} )
950
912
} else {
951
913
None
@@ -964,29 +926,18 @@ impl File {
964
926
if self . x . is_null ( ) {
965
927
return None ;
966
928
}
967
- unsafe {
968
- Some ( String_ {
969
- x : clang_getFileName ( self . x ) ,
970
- }
971
- . to_string ( ) )
972
- }
929
+ unsafe { Some ( clang_getFileName ( self . x ) . into ( ) ) }
973
930
}
974
931
}
975
932
976
- /// A Clang string.
977
- pub struct String_ {
978
- x : CXString ,
979
- }
980
-
981
- impl fmt:: Display for String_ {
982
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
983
- if self . x . data . is_null ( ) {
984
- return "" . fmt ( f) ;
933
+ impl Into < String > for CXString {
934
+ fn into ( self ) -> String {
935
+ if self . data . is_null ( ) {
936
+ return "" . to_owned ( ) ;
985
937
}
986
938
unsafe {
987
- let c_str = clang_getCString ( self . x ) as * const c_char ;
988
- let p = c_str as * const _ ;
989
- f. write_str ( & String :: from_utf8_lossy ( CStr :: from_ptr ( p) . to_bytes ( ) ) )
939
+ let c_str = CStr :: from_ptr ( clang_getCString ( self ) as * const _ ) ;
940
+ c_str. to_string_lossy ( ) . into_owned ( )
990
941
}
991
942
}
992
943
}
@@ -1139,10 +1090,9 @@ impl TranslationUnit {
1139
1090
num_tokens as usize ) ;
1140
1091
for & token in token_array. iter ( ) {
1141
1092
let kind = clang_getTokenKind ( token) ;
1142
- let spelling = String_ {
1143
- x : clang_getTokenSpelling ( self . x , token) ,
1144
- }
1145
- . to_string ( ) ;
1093
+ let spelling: String = clang_getTokenSpelling ( self . x , token)
1094
+ . into ( ) ;
1095
+
1146
1096
tokens. push ( Token {
1147
1097
kind : kind,
1148
1098
spelling : spelling,
@@ -1177,12 +1127,7 @@ impl Diagnostic {
1177
1127
/// Format this diagnostic message as a string, using the given option bit
1178
1128
/// flags.
1179
1129
pub fn format ( & self , opts : usize ) -> String {
1180
- unsafe {
1181
- String_ {
1182
- x : clang_formatDiagnostic ( self . x , opts as c_uint ) ,
1183
- }
1184
- . to_string ( )
1185
- }
1130
+ unsafe { clang_formatDiagnostic ( self . x , opts as c_uint ) . into ( ) }
1186
1131
}
1187
1132
1188
1133
/// What is the severity of this diagnostic message?
0 commit comments