Skip to content

Commit 35a5241

Browse files
authored
Merge pull request #336 from stevefan1999-personal/patch-no-std-runtime
Add no_std support for runtime crate
2 parents 182c795 + 64b5c95 commit 35a5241

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ path = "tests/trybuild.rs"
2626
harness = false
2727

2828
[features]
29+
default = ["std"]
2930
trace = ["peg-macros/trace"]
31+
std = ["peg-runtime/std"]
32+
unstable = ["peg-runtime/unstable"]

peg-runtime/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ description = "Runtime support for rust-peg grammars. To use rust-peg, see the `
88
edition = "2018"
99

1010
[lib]
11-
path = "lib.rs"
11+
path = "lib.rs"
12+
13+
[features]
14+
std = []
15+
unstable = []

peg-runtime/error.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
//! Parse error reporting
22
33
use crate::{Parse, RuleResult};
4-
use std::collections::HashSet;
54
use std::fmt::{self, Debug, Display};
65

6+
#[cfg(feature = "std")]
7+
use std::collections::BTreeSet;
8+
9+
#[cfg(not(feature = "std"))]
10+
use {alloc::collections::BTreeSet, alloc::vec::Vec};
11+
712
/// A set of literals or names that failed to match
813
#[derive(PartialEq, Eq, Debug, Clone)]
914
pub struct ExpectedSet {
10-
expected: HashSet<&'static str>,
15+
expected: BTreeSet<&'static str>,
1116
}
1217

1318
impl ExpectedSet {
@@ -58,6 +63,7 @@ impl<L: Display> Display for ParseError<L> {
5863
}
5964
}
6065

66+
#[cfg(any(feature = "std", feature = "unstable"))]
6167
impl<L: Display + Debug> ::std::error::Error for ParseError<L> {
6268
fn description(&self) -> &str {
6369
"parse error"
@@ -88,7 +94,7 @@ impl ErrorState {
8894
suppress_fail: 0,
8995
reparsing_on_error: false,
9096
expected: ExpectedSet {
91-
expected: HashSet::new(),
97+
expected: BTreeSet::new(),
9298
},
9399
}
94100
}

peg-runtime/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
#![cfg_attr(feature = "unstable", feature(error_in_core))]
3+
14
use std::fmt::Display;
25

36
pub mod error;
@@ -50,3 +53,8 @@ pub trait ParseSlice<'input>: Parse {
5053
/// Get a slice of input.
5154
fn parse_slice(&'input self, p1: usize, p2: usize) -> Self::Slice;
5255
}
56+
57+
#[cfg(not(feature = "std"))]
58+
extern crate alloc;
59+
#[cfg(not(feature = "std"))]
60+
extern crate core as std;

0 commit comments

Comments
 (0)