From 9a2ef96710340c4fc6695c503bc6447b339dfc61 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 5 Oct 2019 19:10:56 +0000 Subject: [PATCH] replace try! with ? --- src/legacy.rs | 24 ++-- src/lib.rs | 6 +- src/v0.rs | 318 +++++++++++++++++++++++++------------------------- 3 files changed, 173 insertions(+), 175 deletions(-) diff --git a/src/legacy.rs b/src/legacy.rs index 33803cd..1ccfef1 100644 --- a/src/legacy.rs +++ b/src/legacy.rs @@ -70,7 +70,7 @@ pub fn demangle(s: &str) -> Result<(Demangle, &str), ()> { let mut elements = 0; let mut chars = inner.chars(); - let mut c = try!(chars.next().ok_or(())); + let mut c = chars.next().ok_or(())?; while c != 'E' { // Decode an identifier element's length. if !c.is_digit(10) { @@ -78,17 +78,17 @@ pub fn demangle(s: &str) -> Result<(Demangle, &str), ()> { } let mut len = 0usize; while let Some(d) = c.to_digit(10) { - len = try!(len + len = len .checked_mul(10) .and_then(|len| len.checked_add(d as usize)) - .ok_or(())); - c = try!(chars.next().ok_or(())); + .ok_or(())?; + c = chars.next().ok_or(())?; } // `c` already contains the first character of this identifier, skip it and // all the other characters of this identifier, to reach the next element. for _ in 0..len { - c = try!(chars.next().ok_or(())); + c = chars.next().ok_or(())?; } elements += 1; @@ -126,7 +126,7 @@ impl<'a> fmt::Display for Demangle<'a> { break; } if element != 0 { - try!(f.write_str("::")); + f.write_str("::")?; } if rest.starts_with("_$") { rest = &rest[1..]; @@ -134,10 +134,10 @@ impl<'a> fmt::Display for Demangle<'a> { loop { if rest.starts_with('.') { if let Some('.') = rest[1..].chars().next() { - try!(f.write_str("::")); + f.write_str("::")?; rest = &rest[2..]; } else { - try!(f.write_str(".")); + f.write_str(".")?; rest = &rest[1..]; } } else if rest.starts_with('$') { @@ -171,7 +171,7 @@ impl<'a> fmt::Display for Demangle<'a> { if let (true, Some(c)) = (all_lower_hex, c) { // FIXME(eddyb) do we need to filter out control codepoints? if !c.is_control() { - try!(c.fmt(f)); + c.fmt(f)?; rest = after_escape; continue; } @@ -180,16 +180,16 @@ impl<'a> fmt::Display for Demangle<'a> { break; } }; - try!(f.write_str(unescaped)); + f.write_str(unescaped)?; rest = after_escape; } else if let Some(i) = rest.find(|c| c == '$' || c == '.') { - try!(f.write_str(&rest[..i])); + f.write_str(&rest[..i])?; rest = &rest[i..]; } else { break; } } - try!(f.write_str(rest)); + f.write_str(rest)?; } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 176cb71..49ec429 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -179,9 +179,9 @@ fn is_ascii_punctuation(c: char) -> bool { impl<'a> fmt::Display for Demangle<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.style { - None => try!(f.write_str(self.original)), - Some(DemangleStyle::Legacy(ref d)) => try!(fmt::Display::fmt(d, f)), - Some(DemangleStyle::V0(ref d)) => try!(fmt::Display::fmt(d, f)), + None => f.write_str(self.original)?, + Some(DemangleStyle::Legacy(ref d)) => fmt::Display::fmt(d, f)?, + Some(DemangleStyle::V0(ref d)) => fmt::Display::fmt(d, f)?, } f.write_str(self.suffix) } diff --git a/src/v0.rs b/src/v0.rs index 96ed69d..e83a220 100644 --- a/src/v0.rs +++ b/src/v0.rs @@ -46,12 +46,12 @@ pub fn demangle(s: &str) -> Result<(Demangle, &str), Invalid> { sym: inner, next: 0, }; - try!(parser.skip_path()); + parser.skip_path()?; // Instantiating crate (paths always start with uppercase characters). match parser.sym.as_bytes().get(parser.next) { Some(&(b'A'..=b'Z')) => { - try!(parser.skip_path()); + parser.skip_path()?; } _ => {} } @@ -94,7 +94,7 @@ impl<'s> Ident<'s> { let mut out_len = 0; let r = self.punycode_decode(|i, c| { // Check there's space left for another character. - try!(out.get(out_len).ok_or(())); + out.get(out_len).ok_or(())?; // Move the characters after the insert position. let mut j = out_len; @@ -133,7 +133,7 @@ impl<'s> Ident<'s> { // Populate initial output from ASCII fragment. for c in self.ascii.chars() { - try!(insert(len, c)); + insert(len, c)?; len += 1; } @@ -164,30 +164,28 @@ impl<'s> Ident<'s> { _ => return Err(()), }; let d = d as usize; - delta = try!(delta - .checked_add(try!(d.checked_mul(w).ok_or(()))) - .ok_or(())); + delta = delta.checked_add(d.checked_mul(w).ok_or(())?).ok_or(())?; if d < t { break; } - w = try!(w.checked_mul(base - t).ok_or(())); + w = w.checked_mul(base - t).ok_or(())?; } // Compute the new insert position and character. len += 1; - i = try!(i.checked_add(delta).ok_or(())); - n = try!(n.checked_add(i / len).ok_or(())); + i = i.checked_add(delta).ok_or(())?; + n = n.checked_add(i / len).ok_or(())?; i %= len; let n_u32 = n as u32; let c = if n_u32 as usize == n { - try!(char::from_u32(n_u32).ok_or(())) + char::from_u32(n_u32).ok_or(())? } else { return Err(()); }; // Insert the new character and increment the insert position. - try!(insert(i, c)); + insert(i, c)?; i += 1; // If there are no more deltas, decoding is complete. @@ -214,21 +212,21 @@ impl<'s> Display for Ident<'s> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.try_small_punycode_decode(|chars| { for &c in chars { - try!(c.fmt(f)); + c.fmt(f)?; } Ok(()) }) .unwrap_or_else(|| { if !self.punycode.is_empty() { - try!(f.write_str("punycode{")); + f.write_str("punycode{")?; // Reconstruct a standard Punycode encoding, // by using `-` as the separator. if !self.ascii.is_empty() { - try!(f.write_str(self.ascii)); - try!(f.write_str("-")); + f.write_str(self.ascii)?; + f.write_str("-")?; } - try!(f.write_str(self.punycode)); + f.write_str(self.punycode)?; f.write_str("}") } else { @@ -286,7 +284,7 @@ impl<'s> Parser<'s> { } fn next(&mut self) -> Result { - let b = try!(self.peek().ok_or(Invalid)); + let b = self.peek().ok_or(Invalid)?; self.next += 1; Ok(b) } @@ -294,7 +292,7 @@ impl<'s> Parser<'s> { fn hex_nibbles(&mut self) -> Result<&'s str, Invalid> { let start = self.next; loop { - match try!(self.next()) { + match self.next()? { b'0'..=b'9' | b'a'..=b'f' => {} b'_' => break, _ => return Err(Invalid), @@ -330,9 +328,9 @@ impl<'s> Parser<'s> { let mut x: u64 = 0; while !self.eat(b'_') { - let d = try!(self.digit_62()) as u64; - x = try!(x.checked_mul(62).ok_or(Invalid)); - x = try!(x.checked_add(d).ok_or(Invalid)); + let d = self.digit_62()? as u64; + x = x.checked_mul(62).ok_or(Invalid)?; + x = x.checked_add(d).ok_or(Invalid)?; } x.checked_add(1).ok_or(Invalid) } @@ -341,7 +339,7 @@ impl<'s> Parser<'s> { if !self.eat(tag) { return Ok(0); } - try!(self.integer_62()).checked_add(1).ok_or(Invalid) + self.integer_62()?.checked_add(1).ok_or(Invalid) } fn disambiguator(&mut self) -> Result { @@ -349,7 +347,7 @@ impl<'s> Parser<'s> { } fn namespace(&mut self) -> Result, Invalid> { - match try!(self.next()) { + match self.next()? { // Special namespaces, like closures and shims. ns @ b'A'..=b'Z' => Ok(Some(ns as char)), @@ -362,7 +360,7 @@ impl<'s> Parser<'s> { fn backref(&mut self) -> Result, Invalid> { let s_start = self.next - 1; - let i = try!(self.integer_62()); + let i = self.integer_62()?; if i >= s_start as u64 { return Err(Invalid); } @@ -374,13 +372,13 @@ impl<'s> Parser<'s> { fn ident(&mut self) -> Result, Invalid> { let is_punycode = self.eat(b'u'); - let mut len = try!(self.digit_10()) as usize; + let mut len = self.digit_10()? as usize; if len != 0 { loop { match self.digit_10() { Ok(d) => { - len = try!(len.checked_mul(10).ok_or(Invalid)); - len = try!(len.checked_add(d as usize).ok_or(Invalid)); + len = len.checked_mul(10).ok_or(Invalid)?; + len = len.checked_add(d as usize).ok_or(Invalid)?; } Err(Invalid) => break, } @@ -391,7 +389,7 @@ impl<'s> Parser<'s> { self.eat(b'_'); let start = self.next; - self.next = try!(self.next.checked_add(len).ok_or(Invalid)); + self.next = self.next.checked_add(len).ok_or(Invalid)?; if self.next > self.sym.len() { return Err(Invalid); } @@ -422,40 +420,40 @@ impl<'s> Parser<'s> { } fn skip_path(&mut self) -> Result<(), Invalid> { - match try!(self.next()) { + match self.next()? { b'C' => { - try!(self.disambiguator()); - try!(self.ident()); + self.disambiguator()?; + self.ident()?; } b'N' => { - try!(self.namespace()); - try!(self.skip_path()); - try!(self.disambiguator()); - try!(self.ident()); + self.namespace()?; + self.skip_path()?; + self.disambiguator()?; + self.ident()?; } b'M' => { - try!(self.disambiguator()); - try!(self.skip_path()); - try!(self.skip_type()); + self.disambiguator()?; + self.skip_path()?; + self.skip_type()?; } b'X' => { - try!(self.disambiguator()); - try!(self.skip_path()); - try!(self.skip_type()); - try!(self.skip_path()); + self.disambiguator()?; + self.skip_path()?; + self.skip_type()?; + self.skip_path()?; } b'Y' => { - try!(self.skip_type()); - try!(self.skip_path()); + self.skip_type()?; + self.skip_path()?; } b'I' => { - try!(self.skip_path()); + self.skip_path()?; while !self.eat(b'E') { - try!(self.skip_generic_arg()); + self.skip_generic_arg()?; } } b'B' => { - try!(self.backref()); + self.backref()?; } _ => return Err(Invalid), } @@ -464,7 +462,7 @@ impl<'s> Parser<'s> { fn skip_generic_arg(&mut self) -> Result<(), Invalid> { if self.eat(b'L') { - try!(self.integer_62()); + self.integer_62()?; Ok(()) } else if self.eat(b'K') { self.skip_const() @@ -474,63 +472,63 @@ impl<'s> Parser<'s> { } fn skip_type(&mut self) -> Result<(), Invalid> { - match try!(self.next()) { + match self.next()? { tag if basic_type(tag).is_some() => {} b'R' | b'Q' => { if self.eat(b'L') { - try!(self.integer_62()); + self.integer_62()?; } - try!(self.skip_type()); + self.skip_type()?; } - b'P' | b'O' | b'S' => try!(self.skip_type()), + b'P' | b'O' | b'S' => self.skip_type()?, b'A' => { - try!(self.skip_type()); - try!(self.skip_const()); + self.skip_type()?; + self.skip_const()?; } b'T' => { while !self.eat(b'E') { - try!(self.skip_type()); + self.skip_type()?; } } b'F' => { - let _binder = try!(self.opt_integer_62(b'G')); + let _binder = self.opt_integer_62(b'G')?; let _is_unsafe = self.eat(b'U'); if self.eat(b'K') { let c_abi = self.eat(b'C'); if !c_abi { - let abi = try!(self.ident()); + let abi = self.ident()?; if abi.ascii.is_empty() || !abi.punycode.is_empty() { return Err(Invalid); } } } while !self.eat(b'E') { - try!(self.skip_type()); + self.skip_type()?; } - try!(self.skip_type()); + self.skip_type()?; } b'D' => { - let _binder = try!(self.opt_integer_62(b'G')); + let _binder = self.opt_integer_62(b'G')?; while !self.eat(b'E') { - try!(self.skip_path()); + self.skip_path()?; while self.eat(b'p') { - try!(self.ident()); - try!(self.skip_type()); + self.ident()?; + self.skip_type()?; } } if !self.eat(b'L') { return Err(Invalid); } - try!(self.integer_62()); + self.integer_62()?; } b'B' => { - try!(self.backref()); + self.backref()?; } _ => { // Go back to the tag, so `skip_path` also sees it. self.next -= 1; - try!(self.skip_path()); + self.skip_path()?; } } Ok(()) @@ -538,11 +536,11 @@ impl<'s> Parser<'s> { fn skip_const(&mut self) -> Result<(), Invalid> { if self.eat(b'B') { - try!(self.backref()); + self.backref()?; return Ok(()); } - match try!(self.next()) { + match self.next()? { // Unsigned integer types. b'h' | b't' | b'm' | b'y' | b'o' | b'j' => {} @@ -552,7 +550,7 @@ impl<'s> Parser<'s> { if self.eat(b'p') { return Ok(()); } - try!(self.hex_nibbles()); + self.hex_nibbles()?; Ok(()) } } @@ -612,7 +610,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { /// An index of `0` always refers to `'_`, but starting with `1`, /// indices refer to late-bound lifetimes introduced by a binder. fn print_lifetime_from_index(&mut self, lt: u64) -> fmt::Result { - try!(self.out.write_str("'")); + self.out.write_str("'")?; if lt == 0 { return self.out.write_str("_"); } @@ -624,7 +622,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { c.fmt(self.out) } else { // Use `'_123` after running out of letters. - try!(self.out.write_str("_")); + self.out.write_str("_")?; depth.fmt(self.out) } } @@ -642,15 +640,15 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { let bound_lifetimes = parse!(self, opt_integer_62(b'G')); if bound_lifetimes > 0 { - try!(self.out.write_str("for<")); + self.out.write_str("for<")?; for i in 0..bound_lifetimes { if i > 0 { - try!(self.out.write_str(", ")); + self.out.write_str(", ")?; } self.bound_lifetime_depth += 1; - try!(self.print_lifetime_from_index(1)); + self.print_lifetime_from_index(1)?; } - try!(self.out.write_str("> ")); + self.out.write_str("> ")?; } let r = f(self); @@ -671,9 +669,9 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { let mut i = 0; while self.parser.is_ok() && !self.eat(b'E') { if i > 0 { - try!(self.out.write_str(sep)); + self.out.write_str(sep)?; } - try!(f(self)); + f(self)?; i += 1; } Ok(i) @@ -686,17 +684,17 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { let dis = parse!(self, disambiguator); let name = parse!(self, ident); - try!(name.fmt(self.out)); + name.fmt(self.out)?; if !self.out.alternate() { - try!(self.out.write_str("[")); - try!(fmt::LowerHex::fmt(&dis, self.out)); - try!(self.out.write_str("]")); + self.out.write_str("[")?; + fmt::LowerHex::fmt(&dis, self.out)?; + self.out.write_str("]")?; } } b'N' => { let ns = parse!(self, namespace); - try!(self.print_path(in_value)); + self.print_path(in_value)?; let dis = parse!(self, disambiguator); let name = parse!(self, ident); @@ -704,26 +702,26 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { match ns { // Special namespaces, like closures and shims. Some(ns) => { - try!(self.out.write_str("::{")); + self.out.write_str("::{")?; match ns { - 'C' => try!(self.out.write_str("closure")), - 'S' => try!(self.out.write_str("shim")), - _ => try!(ns.fmt(self.out)), + 'C' => self.out.write_str("closure")?, + 'S' => self.out.write_str("shim")?, + _ => ns.fmt(self.out)?, } if !name.ascii.is_empty() || !name.punycode.is_empty() { - try!(self.out.write_str(":")); - try!(name.fmt(self.out)); + self.out.write_str(":")?; + name.fmt(self.out)?; } - try!(self.out.write_str("#")); - try!(dis.fmt(self.out)); - try!(self.out.write_str("}")); + self.out.write_str("#")?; + dis.fmt(self.out)?; + self.out.write_str("}")?; } // Implementation-specific/unspecified namespaces. None => { if !name.ascii.is_empty() || !name.punycode.is_empty() { - try!(self.out.write_str("::")); - try!(name.fmt(self.out)); + self.out.write_str("::")?; + name.fmt(self.out)?; } } } @@ -735,25 +733,25 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { parse!(self, skip_path); } - try!(self.out.write_str("<")); - try!(self.print_type()); + self.out.write_str("<")?; + self.print_type()?; if tag != b'M' { - try!(self.out.write_str(" as ")); - try!(self.print_path(false)); + self.out.write_str(" as ")?; + self.print_path(false)?; } - try!(self.out.write_str(">")); + self.out.write_str(">")?; } b'I' => { - try!(self.print_path(in_value)); + self.print_path(in_value)?; if in_value { - try!(self.out.write_str("::")); + self.out.write_str("::")?; } - try!(self.out.write_str("<")); - try!(self.print_sep_list(Self::print_generic_arg, ", ")); - try!(self.out.write_str(">")); + self.out.write_str("<")?; + self.print_sep_list(Self::print_generic_arg, ", ")?; + self.out.write_str(">")?; } b'B' => { - try!(self.backref_printer().print_path(in_value)); + self.backref_printer().print_path(in_value)?; } _ => invalid!(self), } @@ -781,48 +779,48 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { match tag { b'R' | b'Q' => { - try!(self.out.write_str("&")); + self.out.write_str("&")?; if self.eat(b'L') { let lt = parse!(self, integer_62); if lt != 0 { - try!(self.print_lifetime_from_index(lt)); - try!(self.out.write_str(" ")); + self.print_lifetime_from_index(lt)?; + self.out.write_str(" ")?; } } if tag != b'R' { - try!(self.out.write_str("mut ")); + self.out.write_str("mut ")?; } - try!(self.print_type()); + self.print_type()?; } b'P' | b'O' => { - try!(self.out.write_str("*")); + self.out.write_str("*")?; if tag != b'P' { - try!(self.out.write_str("mut ")); + self.out.write_str("mut ")?; } else { - try!(self.out.write_str("const ")); + self.out.write_str("const ")?; } - try!(self.print_type()); + self.print_type()?; } b'A' | b'S' => { - try!(self.out.write_str("[")); - try!(self.print_type()); + self.out.write_str("[")?; + self.print_type()?; if tag == b'A' { - try!(self.out.write_str("; ")); - try!(self.print_const()); + self.out.write_str("; ")?; + self.print_const()?; } - try!(self.out.write_str("]")); + self.out.write_str("]")?; } b'T' => { - try!(self.out.write_str("(")); - let count = try!(self.print_sep_list(Self::print_type, ", ")); + self.out.write_str("(")?; + let count = self.print_sep_list(Self::print_type, ", ")?; if count == 1 { - try!(self.out.write_str(",")); + self.out.write_str(",")?; } - try!(self.out.write_str(")")); + self.out.write_str(")")?; } - b'F' => try!(self.in_binder(|this| { + b'F' => self.in_binder(|this| { let is_unsafe = this.eat(b'U'); let abi = if this.eat(b'K') { if this.eat(b'C') { @@ -839,63 +837,63 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { }; if is_unsafe { - try!(this.out.write_str("unsafe ")); + this.out.write_str("unsafe ")?; } match abi { Some(abi) => { - try!(this.out.write_str("extern \"")); + this.out.write_str("extern \"")?; // If the ABI had any `-`, they were replaced with `_`, // so the parts between `_` have to be re-joined with `-`. let mut parts = abi.split('_'); - try!(this.out.write_str(parts.next().unwrap())); + this.out.write_str(parts.next().unwrap())?; for part in parts { - try!(this.out.write_str("-")); - try!(this.out.write_str(part)); + this.out.write_str("-")?; + this.out.write_str(part)?; } - try!(this.out.write_str("\" ")); + this.out.write_str("\" ")?; } None => {} } - try!(this.out.write_str("fn(")); - try!(this.print_sep_list(Self::print_type, ", ")); - try!(this.out.write_str(")")); + this.out.write_str("fn(")?; + this.print_sep_list(Self::print_type, ", ")?; + this.out.write_str(")")?; if this.eat(b'u') { // Skip printing the return type if it's 'u', i.e. `()`. } else { - try!(this.out.write_str(" -> ")); - try!(this.print_type()); + this.out.write_str(" -> ")?; + this.print_type()?; } Ok(()) - })), + })?, b'D' => { - try!(self.out.write_str("dyn ")); - try!(self.in_binder(|this| { - try!(this.print_sep_list(Self::print_dyn_trait, " + ")); + self.out.write_str("dyn ")?; + self.in_binder(|this| { + this.print_sep_list(Self::print_dyn_trait, " + ")?; Ok(()) - })); + })?; if !self.eat(b'L') { invalid!(self); } let lt = parse!(self, integer_62); if lt != 0 { - try!(self.out.write_str(" + ")); - try!(self.print_lifetime_from_index(lt)); + self.out.write_str(" + ")?; + self.print_lifetime_from_index(lt)?; } } b'B' => { - try!(self.backref_printer().print_type()); + self.backref_printer().print_type()?; } _ => { // Go back to the tag, so `print_path` also sees it. let _ = self.parser_mut().map(|p| p.next -= 1); - try!(self.print_path(false)); + self.print_path(false)?; } } Ok(()) @@ -910,35 +908,35 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { if self.eat(b'B') { self.backref_printer().print_path_maybe_open_generics() } else if self.eat(b'I') { - try!(self.print_path(false)); - try!(self.out.write_str("<")); - try!(self.print_sep_list(Self::print_generic_arg, ", ")); + self.print_path(false)?; + self.out.write_str("<")?; + self.print_sep_list(Self::print_generic_arg, ", ")?; Ok(true) } else { - try!(self.print_path(false)); + self.print_path(false)?; Ok(false) } } fn print_dyn_trait(&mut self) -> fmt::Result { - let mut open = try!(self.print_path_maybe_open_generics()); + let mut open = self.print_path_maybe_open_generics()?; while self.eat(b'p') { if !open { - try!(self.out.write_str("<")); + self.out.write_str("<")?; open = true; } else { - try!(self.out.write_str(", ")); + self.out.write_str(", ")?; } let name = parse!(self, ident); - try!(name.fmt(self.out)); - try!(self.out.write_str(" = ")); - try!(self.print_type()); + name.fmt(self.out)?; + self.out.write_str(" = ")?; + self.print_type()?; } if open { - try!(self.out.write_str(">")); + self.out.write_str(">")?; } Ok(()) @@ -958,14 +956,14 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { }; if self.eat(b'p') { - try!(self.out.write_str("_")); + self.out.write_str("_")?; } else { - try!(self.print_const_uint()); + self.print_const_uint()?; } if !self.out.alternate() { - try!(self.out.write_str(": ")); - try!(self.out.write_str(ty)); + self.out.write_str(": ")?; + self.out.write_str(ty)?; } Ok(()) @@ -976,7 +974,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> { // Print anything that doesn't fit in `u64` verbatim. if hex.len() > 16 { - try!(self.out.write_str("0x")); + self.out.write_str("0x")?; return self.out.write_str(hex); }