-
Notifications
You must be signed in to change notification settings - Fork 446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nominal no_std + alloc support to regex-syntax #477
Changes from all commits
12bd4f4
42def74
2cee073
060e9a4
23d0533
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
// except according to those terms. | ||
|
||
use std::fmt; | ||
use prelude::Vec; | ||
|
||
use ast::{self, Ast}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,21 @@ done automatically in the `regex` crate. | |
|
||
#![deny(missing_docs)] | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))] | ||
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(slice_concat_ext))] | ||
|
||
#[cfg(test)] | ||
extern crate std as std_test; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do this? If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here is that we want to be able to run our tests against the library compiled in either mode as a way of confirming that there is no accidental behavioral differences. E.G. IIUIC, because we (now) do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't even know that you do need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree that we could make a decent chunk of the tests run without access to the standard library. That said, the main goal is testing the library code. AFAICT that library code can be compiled in no_std mode and exercised regardless of whether the test code itself has access to standard library. As as we're getting the library code exercised already, I'm not clear on what value add there would be to go to the effort of triaging and modifying the test code to be able to run without the standard lib. |
||
|
||
#[cfg(not(feature = "std"))] | ||
#[macro_use] | ||
extern crate core as std; | ||
|
||
#[cfg(all(feature = "alloc", not(feature = "std")))] | ||
#[macro_use] | ||
extern crate alloc; | ||
|
||
extern crate ucd_util; | ||
|
||
pub use error::{Error, Result}; | ||
|
@@ -115,9 +130,12 @@ mod either; | |
mod error; | ||
pub mod hir; | ||
mod parser; | ||
mod prelude; | ||
mod unicode; | ||
mod unicode_tables; | ||
|
||
use prelude::String; | ||
|
||
/// Escapes all regular expression meta characters in `text`. | ||
/// | ||
/// The string returned may be safely used as a literal in a regular | ||
|
@@ -199,6 +217,7 @@ pub fn is_word_byte(c: u8) -> bool { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use std_test::prelude::v1::*; | ||
use super::*; | ||
|
||
#[test] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! This module provides access to items available in both the standard library | ||
//! and the `alloc` facade crate while concealing the feature-flag based compile | ||
//! time selection between those options. | ||
#![allow(unused_imports)] | ||
|
||
macro_rules! multiplex_alloc { | ||
($($alloc: path, $std: path),*) => { | ||
$( | ||
#[cfg(all(feature = "alloc", not(feature = "std")))] | ||
pub(crate) use $alloc; | ||
#[cfg(feature = "std")] | ||
pub(crate) use $std; | ||
)* | ||
}; | ||
} | ||
|
||
macro_rules! multiplex_core { | ||
($($core: path, $std: path),*) => { | ||
$( | ||
#[cfg(not(feature = "std"))] | ||
pub(crate) use $core; | ||
#[cfg(feature = "std")] | ||
pub(crate) use $std; | ||
)* | ||
}; | ||
} | ||
|
||
macro_rules! alloc_only { | ||
($($alloc: path),*) => { | ||
$( | ||
#[cfg(all(feature = "alloc", not(feature = "std")))] | ||
pub(crate) use $alloc; | ||
)* | ||
}; | ||
} | ||
|
||
multiplex_alloc! { | ||
alloc::borrow::Borrow, ::std::borrow::Borrow, | ||
alloc::borrow::ToOwned, ::std::borrow::ToOwned, | ||
alloc::boxed::Box, ::std::boxed::Box, | ||
alloc::String, ::std::string::String, | ||
alloc::string::ToString, ::std::string::ToString, | ||
alloc::Vec, ::std::vec::Vec | ||
} | ||
|
||
multiplex_core! { | ||
core::fmt, ::std::fmt, | ||
core::slice, ::std::slice | ||
} | ||
|
||
alloc_only! { | ||
alloc::slice::SliceConcatExt | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need an
alloc
feature flag if it will always be required — i.e. unless there are plans for core-only support in the future you could just drop this feature flag and do#![cfg_attr(not(feature = "std"), feature(alloc))]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to leave the door open for that possibility. We can simplify the cfg statements if those plans don't materialize.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trick we do with Rand: make
std
depend onalloc
. This has the advantage that you can use just#[cfg(feature = "alloc")]
to feature-gate modules/items requiring an allocator (though also the disadvantage that sometimes you need to check both:#![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))]
).