Struct cpp_demangle::Symbol

source ·
pub struct Symbol<T> { /* private fields */ }
Expand description

A mangled symbol that has been parsed into an AST.

This is generic over some storage type T which can be either owned or borrowed. See the OwnedSymbol and BorrowedSymbol type aliases.

Implementations§

source§

impl<T> Symbol<T>
where T: AsRef<[u8]>,

source

pub fn new(raw: T) -> Result<Symbol<T>>

Given some raw storage, parse the mangled symbol from it with the default options.

use cpp_demangle::Symbol;
use std::string::ToString;

// First, something easy :)

let mangled = b"_ZN5space3fooEibc";

let sym = Symbol::new(&mangled[..])
    .expect("Could not parse mangled symbol!");

let demangled = sym.to_string();
assert_eq!(demangled, "space::foo(int, bool, char)");

// Now let's try something a little more complicated!

let mangled =
    b"__Z28JS_GetPropertyDescriptorByIdP9JSContextN2JS6HandleIP8JSObjectEENS2_I4jsidEENS1_13MutableHandleINS1_18PropertyDescriptorEEE";

let sym = Symbol::new(&mangled[..])
    .expect("Could not parse mangled symbol!");

let demangled = sym.to_string();
assert_eq!(
    demangled,
    "JS_GetPropertyDescriptorById(JSContext*, JS::Handle<JSObject*>, JS::Handle<jsid>, JS::MutableHandle<JS::PropertyDescriptor>)"
);
source

pub fn new_with_options(raw: T, options: &ParseOptions) -> Result<Symbol<T>>

Given some raw storage, parse the mangled symbol from it.

use cpp_demangle::{ParseOptions, Symbol};
use std::string::ToString;

// First, something easy :)

let mangled = b"_ZN5space3fooEibc";

let parse_options = ParseOptions::default()
    .recursion_limit(1024);

let sym = Symbol::new_with_options(&mangled[..], &parse_options)
    .expect("Could not parse mangled symbol!");

let demangled = sym.to_string();
assert_eq!(demangled, "space::foo(int, bool, char)");

// Now let's try something a little more complicated!

let mangled =
    b"__Z28JS_GetPropertyDescriptorByIdP9JSContextN2JS6HandleIP8JSObjectEENS2_I4jsidEENS1_13MutableHandleINS1_18PropertyDescriptorEEE";

let sym = Symbol::new(&mangled[..])
    .expect("Could not parse mangled symbol!");

let demangled = sym.to_string();
assert_eq!(
    demangled,
    "JS_GetPropertyDescriptorById(JSContext*, JS::Handle<JSObject*>, JS::Handle<jsid>, JS::MutableHandle<JS::PropertyDescriptor>)"
);
source

pub fn demangle(&self, options: &DemangleOptions) -> Result<String, Error>

Demangle the symbol and return it as a String.

Unlike the ToString implementation, this function allows options to be specified.

use cpp_demangle::{DemangleOptions, Symbol};
use std::string::ToString;

let mangled = b"_ZN5space3fooEibc";

let sym = Symbol::new(&mangled[..])
    .expect("Could not parse mangled symbol!");

let demangled = sym.to_string();
let options = DemangleOptions::default();
let demangled_again = sym.demangle(&options).unwrap();
assert_eq!(demangled_again, demangled);
source

pub fn structured_demangle<W: DemangleWrite>( &self, out: &mut W, options: &DemangleOptions, ) -> Result

Demangle the symbol to a DemangleWrite, which lets the consumer be informed about syntactic structure.

source§

impl<'a, T> Symbol<&'a T>
where T: AsRef<[u8]> + ?Sized,

source

pub fn with_tail(input: &'a T) -> Result<(BorrowedSymbol<'a>, &'a [u8])>

Parse a mangled symbol from input and return it and the trailing tail of bytes that come after the symbol, with the default options.

While Symbol::new will return an error if there is unexpected trailing bytes, with_tail simply returns the trailing bytes along with the parsed symbol.

use cpp_demangle::BorrowedSymbol;
use std::string::ToString;

let mangled = b"_ZN5space3fooEibc and some trailing junk";

let (sym, tail) = BorrowedSymbol::with_tail(&mangled[..])
    .expect("Could not parse mangled symbol!");

assert_eq!(tail, b" and some trailing junk");

let demangled = sym.to_string();
assert_eq!(demangled, "space::foo(int, bool, char)");
source

pub fn with_tail_and_options( input: &'a T, options: &ParseOptions, ) -> Result<(BorrowedSymbol<'a>, &'a [u8])>

Parse a mangled symbol from input and return it and the trailing tail of bytes that come after the symbol.

While Symbol::new_with_options will return an error if there is unexpected trailing bytes, with_tail_and_options simply returns the trailing bytes along with the parsed symbol.

use cpp_demangle::{BorrowedSymbol, ParseOptions};
use std::string::ToString;

let mangled = b"_ZN5space3fooEibc and some trailing junk";

let parse_options = ParseOptions::default()
    .recursion_limit(1024);

let (sym, tail) = BorrowedSymbol::with_tail_and_options(&mangled[..], &parse_options)
    .expect("Could not parse mangled symbol!");

assert_eq!(tail, b" and some trailing junk");

let demangled = sym.to_string();
assert_eq!(demangled, "space::foo(int, bool, char)");

Trait Implementations§

source§

impl<T: Clone> Clone for Symbol<T>

source§

fn clone(&self) -> Symbol<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Symbol<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Display for Symbol<T>
where T: AsRef<[u8]>,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: PartialEq> PartialEq for Symbol<T>

source§

fn eq(&self, other: &Symbol<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> StructuralPartialEq for Symbol<T>

Auto Trait Implementations§

§

impl<T> Freeze for Symbol<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Symbol<T>
where T: RefUnwindSafe,

§

impl<T> Send for Symbol<T>
where T: Send,

§

impl<T> Sync for Symbol<T>
where T: Sync,

§

impl<T> Unpin for Symbol<T>
where T: Unpin,

§

impl<T> UnwindSafe for Symbol<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.