Skip to content

Commit

Permalink
Add to_string and to_string_alt, along with alloc and std features
Browse files Browse the repository at this point in the history
  • Loading branch information
programmerjake committed Jan 24, 2020
1 parent c4e3ab0 commit b9f4022
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
23 changes: 22 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ jobs:
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: cargo build --all
- run: cargo test --all
- run: cargo build --all --features=alloc
- run: cargo test --all --features=alloc
- run: cargo build --all --features=std
- run: cargo test --all --features=std
- run: cargo build --all --features=alloc,std
- run: cargo test --all --features=alloc,std

test-old:
name: Test on old releases of Rust
runs-on: ubuntu-latest
strategy:
matrix:
rust: [1.31.1]
steps:
- uses: actions/checkout@master
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: cargo build --all
- run: cargo test --all
- run: cargo build --all --features=std
- run: cargo test --all --features=std

rustfmt:
name: Rustfmt
Expand All @@ -32,7 +53,7 @@ jobs:
- name: Install Rust
run: rustup update stable && rustup default stable
- name: Build documentation
run: cargo doc --no-deps
run: cargo doc --no-deps --features alloc
- name: Publish documentation
run: |
cd target/doc
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ compiler_builtins = { version = '0.1.2', optional = true }

[features]
rustc-dep-of-std = ['core', 'compiler_builtins']
alloc = []
std = []

[profile.release]
lto = true

[package.metadata.docs.rs]
features = ["alloc"]

[package.metadata.playground]
features = ["alloc"]
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ You can add this as a dependency via your `Cargo.toml`

```toml
[dependencies]
rustc-demangle = "0.1"
rustc-demangle = { version = "0.1", features = ["alloc"] }
```

and then be sure to check out the [crate
documentation](https://docs.rs/rustc-demangle) for usage.

### Features

The `alloc` feature enables the functionality that requires a memory allocator
(the [`alloc`] crate).

The `std` feature enables the functionality that requires the [`std`] crate
(currently doesn't enable any more than the `alloc` feature, but works on old
versions of Rust from before the [`alloc`] crate was stabilized).

[`alloc`]: https://doc.rust-lang.org/alloc/index.html
[`std`]: https://doc.rust-lang.org/std/index.html

## Usage from non-Rust languages

You can also use this crate from other languages via the C API wrapper in the
Expand Down
26 changes: 24 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@
#![no_std]
#![deny(missing_docs)]

#[cfg(test)]
#[cfg(any(test, feature = "std"))]
#[macro_use]
extern crate std;

#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
#[macro_use]
extern crate alloc;

mod legacy;
mod v0;

#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
use alloc::string::{String, ToString};
use core::fmt;
#[cfg(any(feature = "std", test))]
use std::string::{String, ToString};

/// Representation of a demangled symbol name.
pub struct Demangle<'a> {
Expand Down Expand Up @@ -147,6 +155,20 @@ impl<'a> Demangle<'a> {
pub fn as_str(&self) -> &'a str {
self.original
}
/// Returns the demangled symbol as a `String`.
///
/// The same as `ToString::to_string(self)`
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn to_string(&self) -> String {
ToString::to_string(self)
}
/// Returns the demangled symbol as a `String`, in alternate format.
///
/// The same as `format!("{:#}", self)`
#[cfg(any(feature = "alloc", feature = "std", test))]
pub fn to_string_alt(&self) -> String {
format!("{:#}", self)
}
}

fn is_symbol_like(s: &str) -> bool {
Expand Down Expand Up @@ -211,7 +233,7 @@ mod tests {

macro_rules! t_nohash {
($a:expr, $b:expr) => {{
assert_eq!(format!("{:#}", super::demangle($a)), $b);
assert_eq!(super::demangle($a).to_string_alt(), $b);
}};
}

Expand Down

0 comments on commit b9f4022

Please sign in to comment.