Skip to content

Commit f9fe3d1

Browse files
authored
Merge pull request #1269 from emilio/so-many-questions. r=nox
Untry.
2 parents d5ca4e7 + cfd153c commit f9fe3d1

File tree

14 files changed

+61
-80
lines changed

14 files changed

+61
-80
lines changed

src/clang.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ impl Type {
903903
/// have a valid layout.
904904
pub fn fallible_layout(&self) -> Result<::ir::layout::Layout, LayoutError> {
905905
use ir::layout::Layout;
906-
let size = try!(self.fallible_size());
907-
let align = try!(self.fallible_align());
906+
let size = self.fallible_size()?;
907+
let align = self.fallible_align()?;
908908
Ok(Layout::new(size, align))
909909
}
910910

src/codegen/struct_layout.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ impl<'a> StructLayoutTracker<'a> {
155155
field_ty: &Type,
156156
field_offset: Option<usize>,
157157
) -> Option<quote::Tokens> {
158-
let mut field_layout = match field_ty.layout(self.ctx) {
159-
Some(l) => l,
160-
None => return None,
161-
};
158+
let mut field_layout = field_ty.layout(self.ctx)?;
162159

163160
if let TypeKind::Array(inner, len) =
164161
*field_ty.canonical_type(self.ctx).kind()

src/ir/comp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ impl CompInfo {
11691169
}
11701170
}
11711171

1172-
let kind = try!(kind);
1172+
let kind = kind?;
11731173

11741174
debug!("CompInfo::from_ty({:?}, {:?})", kind, cursor);
11751175

src/ir/context.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,13 @@ impl<'ctx> Iterator for WhitelistedItemsTraversal<'ctx> {
475475

476476
fn next(&mut self) -> Option<ItemId> {
477477
loop {
478-
match self.traversal.next() {
479-
None => return None,
480-
Some(id) if self.ctx.resolve_item(id).is_blacklisted(self.ctx) => {
481-
continue
482-
}
483-
Some(id) => return Some(id),
478+
let id = self.traversal.next()?;
479+
480+
if self.ctx.resolve_item(id).is_blacklisted(self.ctx) {
481+
continue
484482
}
483+
484+
return Some(id);
485485
}
486486
}
487487
}
@@ -1009,10 +1009,8 @@ impl BindgenContext {
10091009
let comp_item_ids: Vec<ItemId> = self.items
10101010
.iter()
10111011
.filter_map(|(id, item)| {
1012-
if let Some(ty) = item.kind().as_type() {
1013-
if let Some(_comp) = ty.as_comp() {
1014-
return Some(id);
1015-
}
1012+
if item.kind().as_type()?.is_comp() {
1013+
return Some(id);
10161014
}
10171015
None
10181016
})
@@ -1673,10 +1671,7 @@ impl BindgenContext {
16731671
}
16741672
clang_sys::CXCursor_TemplateRef => {
16751673
let (template_decl_cursor, template_decl_id, num_expected_template_args) =
1676-
match self.get_declaration_info_for_template_instantiation(child) {
1677-
Some(info) => info,
1678-
None => return None,
1679-
};
1674+
self.get_declaration_info_for_template_instantiation(child)?;
16801675

16811676
if num_expected_template_args == 0 ||
16821677
child.has_at_least_num_children(

src/ir/dot.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ pub fn write_dot_file<P>(ctx: &BindgenContext, path: P) -> io::Result<()>
2525
where
2626
P: AsRef<Path>,
2727
{
28-
let file = try!(File::create(path));
28+
let file = File::create(path)?;
2929
let mut dot_file = io::BufWriter::new(file);
30-
try!(writeln!(&mut dot_file, "digraph {{"));
30+
writeln!(&mut dot_file, "digraph {{")?;
3131

3232
let mut err: Option<io::Result<_>> = None;
3333

3434
for (id, item) in ctx.items() {
3535
let is_whitelisted = ctx.whitelisted_items().contains(id);
3636

37-
try!(writeln!(
37+
writeln!(
3838
&mut dot_file,
3939
r#"{} [fontname="courier", color={}, label=< <table border="0" align="left">"#,
4040
id.as_usize(),
@@ -43,9 +43,9 @@ where
4343
} else {
4444
"gray"
4545
}
46-
));
47-
try!(item.dot_attributes(ctx, &mut dot_file));
48-
try!(writeln!(&mut dot_file, r#"</table> >];"#));
46+
)?;
47+
item.dot_attributes(ctx, &mut dot_file)?;
48+
writeln!(&mut dot_file, r#"</table> >];"#)?;
4949

5050
item.trace(
5151
ctx,
@@ -79,16 +79,16 @@ where
7979

8080
if let Some(module) = item.as_module() {
8181
for child in module.children() {
82-
try!(writeln!(
82+
writeln!(
8383
&mut dot_file,
8484
"{} -> {} [style=dotted, color=gray]",
8585
item.id().as_usize(),
8686
child.as_usize()
87-
));
87+
)?;
8888
}
8989
}
9090
}
9191

92-
try!(writeln!(&mut dot_file, "}}"));
92+
writeln!(&mut dot_file, "}}")?;
9393
Ok(())
9494
}

src/ir/function.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ impl DotAttributes for Function {
151151
if let Some(ref mangled) = self.mangled_name {
152152
let mangled: String =
153153
mangled.chars().flat_map(|c| c.escape_default()).collect();
154-
try!(writeln!(
154+
writeln!(
155155
out,
156156
"<tr><td>mangled name</td><td>{}</td></tr>",
157157
mangled
158-
));
158+
)?;
159159
}
160160

161161
Ok(())
@@ -422,11 +422,11 @@ impl FunctionSig {
422422
let ty_ret_type = if cursor.kind() == CXCursor_ObjCInstanceMethodDecl ||
423423
cursor.kind() == CXCursor_ObjCClassMethodDecl
424424
{
425-
try!(ty.ret_type().or_else(|| cursor.ret_type()).ok_or(
425+
ty.ret_type().or_else(|| cursor.ret_type()).ok_or(
426426
ParseError::Continue,
427-
))
427+
)?
428428
} else {
429-
try!(ty.ret_type().ok_or(ParseError::Continue))
429+
ty.ret_type().ok_or(ParseError::Continue)?
430430
};
431431
let ret = Item::from_ty_or_ref(ty_ret_type, cursor, None, ctx);
432432
let call_conv = ty.call_conv();
@@ -521,7 +521,7 @@ impl ClangSubItemParser for Function {
521521

522522
// Grab the signature using Item::from_ty.
523523
let sig =
524-
try!(Item::from_ty(&cursor.cur_type(), cursor, None, context));
524+
Item::from_ty(&cursor.cur_type(), cursor, None, context)?;
525525

526526
let mut name = cursor.spelling();
527527
assert!(!name.is_empty(), "Empty function name?");

src/ir/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,13 +1089,13 @@ impl DotAttributes for Item {
10891089
where
10901090
W: io::Write,
10911091
{
1092-
try!(writeln!(
1092+
writeln!(
10931093
out,
10941094
"<tr><td>{:?}</td></tr>
10951095
<tr><td>name</td><td>{}</td></tr>",
10961096
self.id,
10971097
self.name(ctx).get()
1098-
));
1098+
)?;
10991099

11001100
if self.is_opaque(ctx, &()) {
11011101
writeln!(out, "<tr><td>opaque</td><td>true</td></tr>")?;

src/ir/item_kind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ impl DotAttributes for ItemKind {
135135
where
136136
W: io::Write,
137137
{
138-
try!(writeln!(
138+
writeln!(
139139
out,
140140
"<tr><td>kind</td><td>{}</td></tr>",
141141
self.kind_name()
142-
));
142+
)?;
143143

144144
match *self {
145145
ItemKind::Module(ref module) => module.dot_attributes(ctx, out),

src/ir/traversal.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,7 @@ where
469469
type Item = ItemId;
470470

471471
fn next(&mut self) -> Option<Self::Item> {
472-
let id = match self.queue.next() {
473-
None => return None,
474-
Some(id) => id,
475-
};
472+
let id = self.queue.next()?;
476473

477474
let newly_discovered = self.seen.add(None, id);
478475
debug_assert!(

src/ir/ty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,20 +423,20 @@ impl DotAttributes for Type {
423423
W: io::Write,
424424
{
425425
if let Some(ref layout) = self.layout {
426-
try!(writeln!(
426+
writeln!(
427427
out,
428428
"<tr><td>size</td><td>{}</td></tr>
429429
<tr><td>align</td><td>{}</td></tr>",
430430
layout.size,
431431
layout.align
432-
));
432+
)?;
433433
if layout.packed {
434-
try!(writeln!(out, "<tr><td>packed</td><td>true</td></tr>"));
434+
writeln!(out, "<tr><td>packed</td><td>true</td></tr>")?;
435435
}
436436
}
437437

438438
if self.is_const {
439-
try!(writeln!(out, "<tr><td>const</td><td>true</td></tr>"));
439+
writeln!(out, "<tr><td>const</td><td>true</td></tr>")?;
440440
}
441441

442442
self.kind.dot_attributes(ctx, out)
@@ -818,7 +818,7 @@ impl Type {
818818
// trying to see if it has a valid return type.
819819
if ty.ret_type().is_some() {
820820
let signature =
821-
try!(FunctionSig::from_ty(ty, &location, ctx));
821+
FunctionSig::from_ty(ty, &location, ctx)?;
822822
TypeKind::Function(signature)
823823
// Same here, with template specialisations we can safely
824824
// assume this is a Comp(..)
@@ -1122,7 +1122,7 @@ impl Type {
11221122
CXType_FunctionNoProto |
11231123
CXType_FunctionProto => {
11241124
let signature =
1125-
try!(FunctionSig::from_ty(ty, &location, ctx));
1125+
FunctionSig::from_ty(ty, &location, ctx)?;
11261126
TypeKind::Function(signature)
11271127
}
11281128
CXType_Typedef => {

src/ir/var.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ impl DotAttributes for Var {
9898
W: io::Write,
9999
{
100100
if self.is_const {
101-
try!(writeln!(out, "<tr><td>const</td><td>true</td></tr>"));
101+
writeln!(out, "<tr><td>const</td><td>true</td></tr>")?;
102102
}
103103

104104
if let Some(ref mangled) = self.mangled_name {
105-
try!(writeln!(
105+
writeln!(
106106
out,
107107
"<tr><td>mangled name</td><td>{}</td></tr>",
108108
mangled
109-
));
109+
)?;
110110
}
111111

112112
Ok(())
@@ -307,10 +307,7 @@ fn parse_macro(
307307
) -> Option<(Vec<u8>, cexpr::expr::EvalResult)> {
308308
use cexpr::{expr, nom};
309309

310-
let mut cexpr_tokens = match cursor.cexpr_tokens() {
311-
None => return None,
312-
Some(tokens) => tokens,
313-
};
310+
let mut cexpr_tokens = cursor.cexpr_tokens()?;
314311

315312
let parser = expr::IdentifierParser::new(ctx.parsed_macros());
316313

@@ -327,9 +324,7 @@ fn parse_macro(
327324
// See:
328325
// https://bugs.llvm.org//show_bug.cgi?id=9069
329326
// https://reviews.llvm.org/D26446
330-
if cexpr_tokens.pop().is_none() {
331-
return None;
332-
}
327+
cexpr_tokens.pop()?;
333328

334329
match parser.macro_definition(&cexpr_tokens) {
335330
nom::IResult::Done(_, (id, val)) => Some((id.into(), val)),
@@ -341,10 +336,7 @@ fn parse_int_literal_tokens(cursor: &clang::Cursor) -> Option<i64> {
341336
use cexpr::{expr, nom};
342337
use cexpr::expr::EvalResult;
343338

344-
let cexpr_tokens = match cursor.cexpr_tokens() {
345-
None => return None,
346-
Some(tokens) => tokens,
347-
};
339+
let cexpr_tokens = cursor.cexpr_tokens()?;
348340

349341
// TODO(emilio): We can try to parse other kinds of literals.
350342
match expr::expr(&cexpr_tokens) {

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ impl Default for CodegenConfig {
147147
/// use bindgen::builder;
148148
///
149149
/// // Configure and generate bindings.
150-
/// let bindings = try!(builder().header("path/to/input/header")
151-
/// .whitelisted_type("SomeCoolClass")
152-
/// .whitelisted_function("do_some_cool_thing")
153-
/// .generate());
150+
/// let bindings = builder().header("path/to/input/header")
151+
/// .whitelisted_type("SomeCoolClass")
152+
/// .whitelisted_function("do_some_cool_thing")
153+
/// .generate()?;
154154
///
155155
/// // Write the generated bindings to an output file.
156-
/// try!(bindings.write_to_file("path/to/output.rs"));
156+
/// bindings.write_to_file("path/to/output.rs")?;
157157
/// ```
158158
#[derive(Debug, Default)]
159159
pub struct Builder {
@@ -1648,7 +1648,7 @@ impl Bindings {
16481648
{
16491649
let _t = time::Timer::new("parse")
16501650
.with_output(time_phases);
1651-
try!(parse(&mut context));
1651+
parse(&mut context)?;
16521652
}
16531653

16541654
let (items, options) = codegen::codegen(context);

src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ where
545545
}
546546

547547
let output = if let Some(path) = matches.value_of("output") {
548-
let file = try!(File::create(path));
548+
let file = File::create(path)?;
549549
Box::new(io::BufWriter::new(file)) as Box<io::Write>
550550
} else {
551551
Box::new(io::BufWriter::new(io::stdout())) as Box<io::Write>

0 commit comments

Comments
 (0)