Skip to content

Commit 6dc0d20

Browse files
authored
Merge pull request #6 from fitzgen/try-demangle
Add the `try_demangle` function
2 parents 58b4123 + ee05445 commit 6dc0d20

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ pub fn demangle(s: &str) -> Demangle {
127127
}
128128
}
129129

130+
/// The same as `demangle`, except return an `Err` if the string does not appear
131+
/// to be a Rust symbol, rather than "demangling" the given string as a no-op.
132+
///
133+
/// ```
134+
/// extern crate rustc_demangle;
135+
///
136+
/// let not_a_rust_symbol = "la la la";
137+
///
138+
/// // The `try_demangle` function will reject strings which are not Rust symbols.
139+
/// assert!(rustc_demangle::try_demangle(not_a_rust_symbol).is_err());
140+
///
141+
/// // While `demangle` will just pass the non-symbol through as a no-op.
142+
/// assert_eq!(rustc_demangle::demangle(not_a_rust_symbol).as_str(), not_a_rust_symbol);
143+
/// ```
144+
pub fn try_demangle(s: &str) -> Result<Demangle, ()> {
145+
let sym = demangle(s);
146+
if sym.valid {
147+
Ok(sym)
148+
} else {
149+
Err(())
150+
}
151+
}
152+
130153
impl<'a> Demangle<'a> {
131154
/// Returns the underlying string that's being demangled.
132155
pub fn as_str(&self) -> &'a str {

0 commit comments

Comments
 (0)