Skip to content

Commit

Permalink
Support no_std by gating std usage on a "std" feature flag
Browse files Browse the repository at this point in the history
This adds an enabled-by-default "std" cargo feature which can be selectively
removed to enable no_std support.

When the "std" feature is absent, feature(collections) will be enabled to
provide Vec support. This requires nightly, but since "std" is enabled by
default, this should have no impact on stable users.
  • Loading branch information
tonychain committed Jun 2, 2017
1 parent fd2af6e commit 5664d47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ categories = ["memory-management"]
name = "typed_arena"
path = "src/lib.rs"
doctest = false

[features]
default = ["std"]
std = []
24 changes: 18 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@
// 2) add and stabilize placement new.
// 3) use an iterator. This may add far too much unsafe code.

use std::cell::RefCell;
use std::cmp;
use std::iter;
use std::mem;
use std::slice;
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![cfg_attr(not(feature = "std"), feature(collections))]

#[cfg(not(feature = "std"))]
extern crate collections;

#[cfg(any(feature = "std", test))]
extern crate core;

#[cfg(not(feature = "std"))]
use collections::Vec;

use core::cell::RefCell;
use core::cmp;
use core::iter;
use core::mem;
use core::slice;

#[cfg(test)]
mod test;
Expand Down Expand Up @@ -51,7 +63,7 @@ impl<T> Arena<T> {
Arena {
chunks: RefCell::new(ChunkList {
current: Vec::with_capacity(n),
rest: vec![],
rest: Vec::new(),
}),
}
}
Expand Down

0 comments on commit 5664d47

Please sign in to comment.