From b9f4022559b51bdba601f1f4a031f2c046f25b21 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Fri, 24 Jan 2020 14:40:26 -0800 Subject: [PATCH] Add to_string and to_string_alt, along with alloc and std features Fixes #20 --- .github/workflows/main.yml | 23 ++++++++++++++++++++++- Cargo.toml | 8 ++++++++ README.md | 14 +++++++++++++- src/lib.rs | 26 ++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 61465ce..d01f8e1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 0701278..c8ecefc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] \ No newline at end of file diff --git a/README.md b/README.md index 0833e1e..0bc28b7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 2b8684e..a71b96b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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> { @@ -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 { @@ -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); }}; }