From 9546acbc28f023ee09577c7c1273020814b99df8 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Fri, 24 Jun 2022 16:11:24 +0000 Subject: [PATCH] add no_std support --- Cargo.toml | 7 +++++++ src/lib.rs | 22 +++++++++++++++++++--- src/nfa.rs | 15 ++++++++++++--- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d47f998..d5d87af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,10 @@ edition = "2018" version = "0.3.1" authors = ["wycats", "rustasync"] + +[features] +default = ["std"] +std = [] + +[dependencies] +cfg-if = "1.0.0" diff --git a/src/lib.rs b/src/lib.rs index 0df75b0..217c105 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,10 +43,20 @@ #![doc(test(attr(allow(unused_extern_crates, unused_variables))))] #![doc(html_favicon_url = "https://yoshuawuyts.com/assets/http-rs/favicon.ico")] #![doc(html_logo_url = "https://yoshuawuyts.com/assets/http-rs/logo-rounded.png")] +#![cfg_attr(not(feature = "std"), no_std)] -use std::cmp::Ordering; -use std::collections::{btree_map, BTreeMap}; -use std::ops::Index; +#![cfg_attr(not(feature = "std"), macro_use)] +extern crate alloc; + +use core::cmp::Ordering; +use alloc::collections::{btree_map, BTreeMap}; +use core::ops::Index; + +#[cfg(not(feature = "std"))] +use alloc::{ + vec::Vec, vec, + string::{String, ToString} +}; use crate::nfa::{CharacterClass, NFA}; @@ -348,6 +358,12 @@ fn process_star_state(nfa: &mut NFA, mut state: usize) -> usize { #[cfg(test)] mod tests { + #[cfg(not(feature = "std"))] + use alloc::{ + vec::Vec, + string::{String, ToString} + }; + use super::{Params, Router}; #[test] diff --git a/src/nfa.rs b/src/nfa.rs index 7d6df04..a62c7d9 100644 --- a/src/nfa.rs +++ b/src/nfa.rs @@ -1,4 +1,10 @@ -use std::collections::HashSet; +use alloc::collections::BTreeSet; + +#[cfg(not(feature = "std"))] +use alloc::{ + vec::Vec, vec, format, + string::{String, ToString} +}; use self::CharacterClass::{Ascii, InvalidChars, ValidChars}; @@ -6,7 +12,7 @@ use self::CharacterClass::{Ascii, InvalidChars, ValidChars}; pub struct CharSet { low_mask: u64, high_mask: u64, - non_ascii: HashSet, + non_ascii: BTreeSet, } impl CharSet { @@ -14,7 +20,7 @@ impl CharSet { Self { low_mask: 0, high_mask: 0, - non_ascii: HashSet::new(), + non_ascii: BTreeSet::new(), } } @@ -401,6 +407,9 @@ fn capture( #[cfg(test)] mod tests { + #[cfg(not(feature = "std"))] + use alloc::vec; + use super::{CharSet, CharacterClass, NFA}; #[test]