Skip to content

Commit 338f6f5

Browse files
committed
Get rid of String_, use Into<String> for CXString.
1 parent fc0f896 commit 338f6f5

File tree

1 file changed

+25
-80
lines changed

1 file changed

+25
-80
lines changed

src/clang.rs

Lines changed: 25 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ impl Cursor {
4545
///
4646
/// The USR can be used to compare entities across translation units.
4747
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();
5249
if s.is_empty() { None } else { Some(s) }
5350
}
5451

@@ -66,35 +63,20 @@ impl Cursor {
6663

6764
/// Get this cursor's referent's spelling.
6865
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() }
7567
}
7668

7769
/// Get this cursor's referent's display name.
7870
///
7971
/// This is not necessarily a valid identifier. It includes extra
8072
/// information, such as parameters for a function, etc.
8173
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() }
8875
}
8976

9077
/// Get the mangled name of this cursor's referent.
9178
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() }
9880
}
9981

10082
/// Get the `Cursor` for this cursor's referent's lexical parent.
@@ -246,12 +228,8 @@ impl Cursor {
246228

247229
/// Get the raw declaration comment for this referent, if one exists.
248230
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() };
255233
if s.is_empty() { None } else { Some(s) }
256234
}
257235

@@ -604,12 +582,7 @@ impl Type {
604582

605583
/// Get a raw display name for this type.
606584
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() }
613586
}
614587

615588
/// Is this type const qualified?
@@ -872,12 +845,7 @@ impl Comment {
872845
/// Given that this comment is the start or end of an HTML tag, get its tag
873846
/// name.
874847
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() }
881849
}
882850

883851
/// Given that this comment is an HTML start tag, get its attributes.
@@ -934,18 +902,12 @@ impl Iterator for CommentAttributesIterator {
934902
let idx = self.index;
935903
self.index += 1;
936904
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+
},
949911
})
950912
} else {
951913
None
@@ -964,29 +926,18 @@ impl File {
964926
if self.x.is_null() {
965927
return None;
966928
}
967-
unsafe {
968-
Some(String_ {
969-
x: clang_getFileName(self.x),
970-
}
971-
.to_string())
972-
}
929+
unsafe { Some(clang_getFileName(self.x).into()) }
973930
}
974931
}
975932

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();
985937
}
986938
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()
990941
}
991942
}
992943
}
@@ -1139,10 +1090,9 @@ impl TranslationUnit {
11391090
num_tokens as usize);
11401091
for &token in token_array.iter() {
11411092
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+
11461096
tokens.push(Token {
11471097
kind: kind,
11481098
spelling: spelling,
@@ -1177,12 +1127,7 @@ impl Diagnostic {
11771127
/// Format this diagnostic message as a string, using the given option bit
11781128
/// flags.
11791129
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() }
11861131
}
11871132

11881133
/// What is the severity of this diagnostic message?

0 commit comments

Comments
 (0)