Skip to content

Commit

Permalink
cleanup dead_code around cpp_demangle feature
Browse files Browse the repository at this point in the history
  • Loading branch information
klensy committed May 7, 2024
1 parent 7d26334 commit 3493d2c
Showing 1 changed file with 32 additions and 67 deletions.
99 changes: 32 additions & 67 deletions src/symbolize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,32 +292,15 @@ cfg_if::cfg_if! {
OptionCppSymbol(None)
}
}
} else {
use core::marker::PhantomData;

// Make sure to keep this zero-sized, so that the `cpp_demangle` feature
// has no cost when disabled.
struct OptionCppSymbol<'a>(PhantomData<&'a ()>);

impl<'a> OptionCppSymbol<'a> {
fn parse(_: &'a [u8]) -> OptionCppSymbol<'a> {
OptionCppSymbol(PhantomData)
}

fn none() -> OptionCppSymbol<'a> {
OptionCppSymbol(PhantomData)
}
}
}
}

/// A wrapper around a symbol name to provide ergonomic accessors to the
/// demangled name, the raw bytes, the raw string, etc.
// Allow dead code for when the `cpp_demangle` feature is not enabled.
#[allow(dead_code)]
pub struct SymbolName<'a> {
bytes: &'a [u8],
demangled: Option<Demangle<'a>>,
#[cfg(feature = "cpp_demangle")]
cpp_demangled: OptionCppSymbol<'a>,
}

Expand All @@ -327,6 +310,7 @@ impl<'a> SymbolName<'a> {
let str_bytes = str::from_utf8(bytes).ok();
let demangled = str_bytes.and_then(|s| try_demangle(s).ok());

#[cfg(feature = "cpp_demangle")]
let cpp = if demangled.is_none() {
OptionCppSymbol::parse(bytes)
} else {
Expand All @@ -336,6 +320,7 @@ impl<'a> SymbolName<'a> {
SymbolName {
bytes: bytes,
demangled: demangled,
#[cfg(feature = "cpp_demangle")]
cpp_demangled: cpp,
}
}
Expand Down Expand Up @@ -380,65 +365,45 @@ fn format_symbol_name(
Ok(())
}

cfg_if::cfg_if! {
if #[cfg(feature = "cpp_demangle")] {
impl<'a> fmt::Display for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref s) = self.demangled {
s.fmt(f)
} else if let Some(ref cpp) = self.cpp_demangled.0 {
cpp.fmt(f)
} else {
format_symbol_name(fmt::Display::fmt, self.bytes, f)
}
}
impl<'a> fmt::Display for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref s) = self.demangled {
return s.fmt(f);
}
} else {
impl<'a> fmt::Display for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref s) = self.demangled {
s.fmt(f)
} else {
format_symbol_name(fmt::Display::fmt, self.bytes, f)
}

#[cfg(feature = "cpp_demangle")]
{
if let Some(ref cpp) = self.cpp_demangled.0 {
return cpp.fmt(f);
}
}

format_symbol_name(fmt::Display::fmt, self.bytes, f)
}
}

cfg_if::cfg_if! {
if #[cfg(all(feature = "std", feature = "cpp_demangle"))] {
impl<'a> fmt::Debug for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use std::fmt::Write;

if let Some(ref s) = self.demangled {
return s.fmt(f)
}

// This may to print if the demangled symbol isn't actually
// valid, so handle the error here gracefully by not propagating
// it outwards.
if let Some(ref cpp) = self.cpp_demangled.0 {
let mut s = String::new();
if write!(s, "{cpp}").is_ok() {
return s.fmt(f)
}
}

format_symbol_name(fmt::Debug::fmt, self.bytes, f)
}
impl<'a> fmt::Debug for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref s) = self.demangled {
return s.fmt(f);
}
} else {
impl<'a> fmt::Debug for SymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref s) = self.demangled {
s.fmt(f)
} else {
format_symbol_name(fmt::Debug::fmt, self.bytes, f)

#[cfg(all(feature = "std", feature = "cpp_demangle"))]
{
use std::fmt::Write;

// This may to print if the demangled symbol isn't actually
// valid, so handle the error here gracefully by not propagating
// it outwards.
if let Some(ref cpp) = self.cpp_demangled.0 {
let mut s = String::new();
if write!(s, "{cpp}").is_ok() {
return s.fmt(f);
}
}
}

format_symbol_name(fmt::Debug::fmt, self.bytes, f)
}
}

Expand Down

0 comments on commit 3493d2c

Please sign in to comment.