Skip to content

Commit bf0563d

Browse files
a1phyrkhuey
authored andcommitted
Fix #[no_std] build
1 parent 43f2d0c commit bf0563d

File tree

6 files changed

+50
-68
lines changed

6 files changed

+50
-68
lines changed

Cargo.toml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@ diff = "0.1.11"
3838
# Default features.
3939
default = ["std"]
4040

41-
# Build using the `std` library. Disabling this and enabling the `alloc` feature
42-
# enables `no_std` support.
43-
std = []
41+
# Build using the `std` library. Disabling this enables `no_std` support.
42+
std = ["alloc"]
4443

45-
# Use collections from the `alloc` crate rather than from `std`. Combined with
46-
# disabling `std`, this enables `no_std` support.
44+
# Use collections from the `alloc` crate rather than from `std`.
4745
alloc = []
4846

4947
# Enable copious amounts of logging. This is for internal use only, and is only
5048
# useful for hacking on `cpp_demangle` itself.
51-
logging = []
49+
logging = ["std"]
5250

5351
# Run all libiberty tests, even the ones that are known not to pass yet. This is
5452
# for internal use only.

src/ast.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
//! Abstract syntax tree types for mangled symbols.
22
33
use super::{DemangleNodeType, DemangleOptions, DemangleWrite, ParseOptions};
4-
use crate::boxed::Box;
54
use crate::error::{self, Result};
65
use crate::index_str::IndexStr;
7-
use crate::string::String;
86
use crate::subs::{Substitutable, SubstitutionTable};
9-
use crate::vec::Vec;
10-
use std::cell::Cell;
7+
use alloc::boxed::Box;
8+
use alloc::string::String;
9+
use alloc::vec::Vec;
10+
use core::cell::Cell;
1111
#[cfg(feature = "logging")]
12-
use std::cell::RefCell;
13-
use std::fmt::{self, Write};
14-
use std::hash::{Hash, Hasher};
15-
use std::mem;
16-
use std::ops;
17-
use std::ptr;
12+
use core::cell::RefCell;
13+
use core::fmt::{self, Write};
14+
use core::hash::{Hash, Hasher};
15+
use core::mem;
16+
use core::ops;
17+
use core::ptr;
18+
use core::str;
1819

1920
struct AutoLogParse;
2021

@@ -454,21 +455,21 @@ struct AutoParseDemangle<'a, 'b, W: 'a + DemangleWrite>(&'b mut DemangleContext<
454455

455456
impl<'a, 'b, W: 'a + DemangleWrite> AutoParseDemangle<'a, 'b, W> {
456457
#[inline]
457-
fn new(ctx: &'b mut DemangleContext<'a, W>) -> std::result::Result<Self, fmt::Error> {
458+
fn new(ctx: &'b mut DemangleContext<'a, W>) -> core::result::Result<Self, fmt::Error> {
458459
ctx.enter_recursion()?;
459460
Ok(AutoParseDemangle(ctx))
460461
}
461462
}
462463

463-
impl<'a, 'b, W: 'a + DemangleWrite> std::ops::Deref for AutoParseDemangle<'a, 'b, W> {
464+
impl<'a, 'b, W: 'a + DemangleWrite> ops::Deref for AutoParseDemangle<'a, 'b, W> {
464465
type Target = DemangleContext<'a, W>;
465466

466467
fn deref(&self) -> &Self::Target {
467468
self.0
468469
}
469470
}
470471

471-
impl<'a, 'b, W: 'a + DemangleWrite> std::ops::DerefMut for AutoParseDemangle<'a, 'b, W> {
472+
impl<'a, 'b, W: 'a + DemangleWrite> ops::DerefMut for AutoParseDemangle<'a, 'b, W> {
472473
fn deref_mut(&mut self) -> &mut Self::Target {
473474
self.0
474475
}
@@ -718,7 +719,7 @@ where
718719

719720
fn set_source_name(&mut self, start: usize, end: usize) {
720721
let ident = &self.input[start..end];
721-
self.source_name = std::str::from_utf8(ident).ok();
722+
self.source_name = str::from_utf8(ident).ok();
722723
}
723724

724725
fn push_demangle_node(&mut self, t: DemangleNodeType) {
@@ -4943,7 +4944,7 @@ impl TemplateParam {
49434944
fn resolve<'subs, 'prev>(
49444945
&'subs self,
49454946
scope: Option<ArgScopeStack<'prev, 'subs>>,
4946-
) -> ::std::result::Result<&'subs TemplateArg, fmt::Error> {
4947+
) -> ::core::result::Result<&'subs TemplateArg, fmt::Error> {
49474948
scope
49484949
.get_template_arg(self.0)
49494950
.map_err(|e| {
@@ -6664,7 +6665,7 @@ where
66646665
} else {
66656666
start
66666667
};
6667-
let s = ::std::str::from_utf8(&ctx.input[start..end]).map_err(|e| {
6668+
let s = str::from_utf8(&ctx.input[start..end]).map_err(|e| {
66686669
log!("Error writing literal: {}", e);
66696670
fmt::Error
66706671
})?;
@@ -6718,7 +6719,7 @@ where
67186719
write!(ctx, "[")?;
67196720
start
67206721
};
6721-
let s = ::std::str::from_utf8(&ctx.input[start..end]).map_err(|e| {
6722+
let s = str::from_utf8(&ctx.input[start..end]).map_err(|e| {
67226723
log!("Error writing literal: {}", e);
67236724
fmt::Error
67246725
})?;
@@ -7775,7 +7776,7 @@ fn parse_number(base: u32, allow_signed: bool, mut input: IndexStr) -> Result<(i
77757776
let head = unsafe {
77767777
// Safe because we know we only have valid numeric chars in this
77777778
// slice, which are valid UTF-8.
7778-
::std::str::from_utf8_unchecked(head)
7779+
str::from_utf8_unchecked(head)
77797780
};
77807781

77817782
let mut number = isize::from_str_radix(head, base).map_err(|_| error::Error::Overflow)?;
@@ -7803,13 +7804,13 @@ mod tests {
78037804
WellKnownComponent,
78047805
};
78057806

7806-
use crate::boxed::Box;
78077807
use crate::error::Error;
78087808
use crate::index_str::IndexStr;
7809-
use crate::string::String;
78107809
use crate::subs::{Substitutable, SubstitutionTable};
7811-
use std::fmt::Debug;
7812-
use std::iter::FromIterator;
7810+
use alloc::boxed::Box;
7811+
use alloc::string::String;
7812+
use core::fmt::Debug;
7813+
use core::iter::FromIterator;
78137814

78147815
fn assert_parse_ok<P, S1, S2, I1, I2>(
78157816
production: &'static str,

src/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Custom `Error` and `Result` types for the `cpp_demangle` crate.
22
3+
use core::fmt;
34
#[cfg(feature = "std")]
45
use std::error;
5-
use std::fmt;
66

77
/// Errors that can occur while demangling a symbol.
88
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -43,9 +43,8 @@ pub enum Error {
4343

4444
#[test]
4545
fn size_of_error() {
46-
use std::mem;
4746
assert_eq!(
48-
mem::size_of::<Error>(),
47+
core::mem::size_of::<Error>(),
4948
1,
5049
"We should keep the size of our Error type in check"
5150
);
@@ -122,4 +121,4 @@ impl error::Error for Error {
122121
}
123122

124123
/// A demangling result of `T` or a `cpp_demangle::error::Error`.
125-
pub type Result<T> = ::std::result::Result<T, Error>;
124+
pub type Result<T> = ::core::result::Result<T, Error>;

src/index_str.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Provides the `IndexStr` type to keep track of a substring's index into its
22
//! original string is.
33
4-
use crate::string::String;
5-
use std::fmt;
6-
use std::ops::{Range, RangeFrom, RangeTo};
4+
use alloc::string::String;
5+
use core::fmt;
6+
use core::ops::{Range, RangeFrom, RangeTo};
77

88
/// The `IndexStr` type allows us to take substrings from an original input and
99
/// keep track of what index the substring is at in the original input.

src/lib.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,14 @@
3434
#![allow(unknown_lints)]
3535
#![allow(clippy::inline_always)]
3636
#![allow(clippy::redundant_field_names)]
37-
#![cfg_attr(all(not(feature = "std"), feature = "alloc"), no_std)]
38-
#![cfg_attr(all(not(feature = "std"), feature = "alloc"), feature(alloc))]
37+
#![cfg_attr(not(feature = "std"), no_std)]
3938

39+
#[cfg(feature = "alloc")]
4040
#[macro_use]
41-
extern crate cfg_if;
42-
43-
cfg_if! {
44-
if #[cfg(all(not(feature = "std"), feature = "alloc"))] {
45-
extern crate core as std;
46-
#[macro_use]
47-
extern crate alloc;
48-
mod imports {
49-
pub use alloc::boxed;
50-
pub use alloc::vec;
51-
pub use alloc::string;
52-
}
53-
} else {
54-
mod imports {
55-
pub use std::boxed;
56-
pub use std::vec;
57-
pub use std::string;
58-
}
59-
}
60-
}
41+
extern crate alloc;
6142

62-
use imports::*;
63-
64-
use string::String;
65-
use vec::Vec;
43+
#[cfg(not(feature = "alloc"))]
44+
compile_error!("`alloc` or `std` feature is required for this crate");
6645

6746
#[macro_use]
6847
mod logging;
@@ -72,11 +51,13 @@ pub mod error;
7251
mod index_str;
7352
mod subs;
7453

54+
use alloc::string::String;
55+
use alloc::vec::Vec;
7556
use ast::{Demangle, Parse, ParseContext};
57+
use core::fmt;
58+
use core::num::NonZeroU32;
7659
use error::{Error, Result};
7760
use index_str::IndexStr;
78-
use std::fmt;
79-
use std::num::NonZeroU32;
8061

8162
/// Options to control the parsing process.
8263
#[derive(Clone, Copy, Debug, Default)]
@@ -291,7 +272,10 @@ substitutions = {:#?}",
291272
/// assert_eq!(demangled_again, demangled);
292273
/// ```
293274
#[allow(clippy::trivially_copy_pass_by_ref)]
294-
pub fn demangle(&self, options: &DemangleOptions) -> ::std::result::Result<String, fmt::Error> {
275+
pub fn demangle(
276+
&self,
277+
options: &DemangleOptions,
278+
) -> ::core::result::Result<String, fmt::Error> {
295279
let mut out = String::new();
296280
{
297281
let mut ctx = ast::DemangleContext::new(

src/subs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
use super::DemangleWrite;
44
use crate::ast;
5-
use crate::vec::Vec;
6-
use std::fmt;
7-
use std::iter::FromIterator;
8-
use std::ops::Deref;
5+
use alloc::vec::Vec;
6+
use core::fmt;
7+
use core::iter::FromIterator;
8+
use core::ops::Deref;
99

1010
/// An enumeration of all of the types that can end up in the substitution
1111
/// table.

0 commit comments

Comments
 (0)