Skip to content

Commit 81dd09a

Browse files
Add to_string and to_string_alt, along with alloc and std features
Fixes rust-lang#20
1 parent c4e3ab0 commit 81dd09a

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

.github/workflows/main.yml

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ jobs:
1414
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
1515
- run: cargo build --all
1616
- run: cargo test --all
17+
- run: cargo build --all --features=alloc
18+
- run: cargo test --all --features=alloc
19+
- run: cargo build --all --features=std
20+
- run: cargo test --all --features=std
21+
- run: cargo build --all --features=alloc,std
22+
- run: cargo test --all --features=alloc,std
23+
24+
test-old:
25+
name: Test on old releases of Rust
26+
runs-on: ubuntu-latest
27+
strategy:
28+
matrix:
29+
rust: [1.31.1]
30+
steps:
31+
- uses: actions/checkout@master
32+
- name: Install Rust
33+
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
34+
- run: cargo build --all
35+
- run: cargo test --all
36+
- run: cargo build --all --features=std
37+
- run: cargo test --all --features=std
1738

1839
rustfmt:
1940
name: Rustfmt
@@ -32,7 +53,7 @@ jobs:
3253
- name: Install Rust
3354
run: rustup update stable && rustup default stable
3455
- name: Build documentation
35-
run: cargo doc --no-deps
56+
run: cargo doc --no-deps --features alloc
3657
- name: Publish documentation
3758
run: |
3859
cd target/doc

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ compiler_builtins = { version = '0.1.2', optional = true }
2020

2121
[features]
2222
rustc-dep-of-std = ['core', 'compiler_builtins']
23+
alloc = []
24+
std = []
2325

2426
[profile.release]
2527
lto = true
28+
29+
[package.metadata.docs.rs]
30+
features = ["alloc"]
31+
32+
[package.metadata.playground]
33+
features = ["alloc"]

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ You can add this as a dependency via your `Cargo.toml`
1010

1111
```toml
1212
[dependencies]
13-
rustc-demangle = "0.1"
13+
rustc-demangle = { version = "0.1", features = ["alloc"] }
1414
```
1515

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

19+
### Features
20+
21+
The `alloc` feature enables the functionality that requires a memory allocator
22+
(the [`alloc`] crate).
23+
24+
The `std` feature enables the functionality that requires the [`std`] crate
25+
(currently doesn't enable any more than the `alloc` feature, but works on old
26+
versions of Rust from before the [`alloc`] crate was stabilized).
27+
28+
[`alloc`]: https://doc.rust-lang.org/alloc/index.html
29+
[`std`]: https://doc.rust-lang.org/std/index.html
30+
1931
## Usage from non-Rust languages
2032

2133
You can also use this crate from other languages via the C API wrapper in the

src/lib.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@
2626
#![no_std]
2727
#![deny(missing_docs)]
2828

29-
#[cfg(test)]
29+
#[cfg(any(test, feature = "std"))]
3030
#[macro_use]
3131
extern crate std;
3232

33+
#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
34+
#[macro_use]
35+
extern crate alloc;
36+
3337
mod legacy;
3438
mod v0;
3539

40+
#[cfg(all(feature = "alloc", not(feature = "std"), not(test)))]
41+
use alloc::string::{String, ToString};
3642
use core::fmt;
43+
#[cfg(any(feature = "std", test))]
44+
use std::string::{String, ToString};
3745

3846
/// Representation of a demangled symbol name.
3947
pub struct Demangle<'a> {
@@ -147,6 +155,20 @@ impl<'a> Demangle<'a> {
147155
pub fn as_str(&self) -> &'a str {
148156
self.original
149157
}
158+
/// Returns the demangled symbol as a `String`.
159+
///
160+
/// The same as `ToString::to_string(self)`
161+
#[cfg(any(feature = "alloc", feature = "std", test))]
162+
pub fn to_string(&self) -> String {
163+
ToString::to_string(self)
164+
}
165+
/// Returns the demangled symbol as a `String`, in alternate format.
166+
///
167+
/// The same as `format!("{:#}", self)`
168+
#[cfg(any(feature = "alloc", feature = "std", test))]
169+
pub fn to_string_alt(&self) -> String {
170+
format!("{:#}", self)
171+
}
150172
}
151173

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

212234
macro_rules! t_nohash {
213235
($a:expr, $b:expr) => {{
214-
assert_eq!(format!("{:#}", super::demangle($a)), $b);
236+
assert_eq!(super::demangle($a).to_string_alt(), $b);
215237
}};
216238
}
217239

0 commit comments

Comments
 (0)