Update no_std support for Rust 2018 Edition.

With Rust 2018 Edition, the `mod std` trick to alias `core` names to
`std` no longer works, so switch to just having the code use `core`
explicitly.

So instead, switch to just using `core::*` for things that in core.
This is more consistent with other Rust no_std code. And it allows
us to enable `no_std` mode unconditionally in the crates that support
it, which makes testing a little easier.

There actually three cases:

 - For things in std and also in core, like `cmp`: Just use them via
   `core::*`.

 - For things in std and also in alloc, like `Vec`: Import alloc as std, as
   use them from std. This allows them to work on both stable (which
   doesn't provide alloc, but we don't support no_std mode anyway) and
   nightly.

 - For HashMap and similar which are not in core or alloc, import them in
   the top-level lib.rs files from either std or the third-party hashmap_core
   crate, and then have the code use super::hashmap_core.

Also, no_std support continues to be "best effort" at this time and not
something most people need to be testing.
This commit is contained in:
Dan Gohman
2019-01-07 11:04:58 -08:00
parent 50a045363c
commit aeb9161e2c
118 changed files with 322 additions and 355 deletions

View File

@@ -14,6 +14,7 @@ edition = "2018"
[features]
default = ["std"]
std = []
core = []
[badges]
maintenance = { status = "experimental" }

View File

@@ -3,9 +3,10 @@
use crate::iter::{Iter, IterMut};
use crate::keys::Keys;
use crate::EntityRef;
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
use std::slice;
use core::marker::PhantomData;
use core::ops::{Index, IndexMut};
use core::slice;
use std::boxed::Box;
/// A slice mapping `K -> V` allocating dense entity references.
///
@@ -140,6 +141,7 @@ where
mod tests {
use super::*;
use crate::primary::PrimaryMap;
use std::vec::Vec;
// `EntityRef` impl for testing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]

View File

@@ -1,9 +1,9 @@
//! A double-ended iterator over entity references and entities.
use crate::EntityRef;
use std::iter::Enumerate;
use std::marker::PhantomData;
use std::slice;
use core::iter::Enumerate;
use core::marker::PhantomData;
use core::slice;
/// Iterate over all keys in order.
pub struct Iter<'a, K: EntityRef, V>

View File

@@ -1,10 +1,10 @@
//! A double-ended iterator over entity references.
//!
//! When `std::iter::Step` is stabilized, `Keys` could be implemented as a wrapper around
//! `std::ops::Range`, but for now, we implement it manually.
//! When `core::iter::Step` is stabilized, `Keys` could be implemented as a wrapper around
//! `core::ops::Range`, but for now, we implement it manually.
use crate::EntityRef;
use std::marker::PhantomData;
use core::marker::PhantomData;
/// Iterate over all keys in order.
pub struct Keys<K: EntityRef> {

View File

@@ -50,17 +50,15 @@
clippy::use_self
)
)]
// Turns on no_std and alloc features if std is not available.
#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
#![cfg_attr(not(feature = "std"), feature(alloc))]
/// This replaces `std` in builds with `core`.
#[cfg(not(feature = "std"))]
mod std {
extern crate alloc;
pub use self::alloc::{boxed, string, vec};
pub use core::*;
}
#[macro_use]
extern crate alloc as std;
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
// Re-export core so that the macros works with both std and no_std crates
#[doc(hidden)]

View File

@@ -1,8 +1,8 @@
//! Small lists of entity references.
use crate::packed_option::ReservedValue;
use crate::EntityRef;
use std::marker::PhantomData;
use std::mem;
use core::marker::PhantomData;
use core::mem;
use std::vec::Vec;
/// A small list of entity references allocated from a pool.

View File

@@ -3,9 +3,9 @@
use crate::iter::{Iter, IterMut};
use crate::keys::Keys;
use crate::EntityRef;
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
use std::slice;
use core::marker::PhantomData;
use core::ops::{Index, IndexMut};
use core::slice;
use std::vec::Vec;
/// A mapping `K -> V` for densely indexed entity references.

View File

@@ -7,8 +7,8 @@
//! This module provides a `PackedOption<T>` for types that have a reserved value that can be used
//! to represent `None`.
use std::fmt;
use std::mem;
use core::fmt;
use core::mem;
/// Types that have a reserved value which can't be created any other way.
pub trait ReservedValue: Eq {

View File

@@ -3,10 +3,11 @@ use crate::boxed_slice::BoxedSlice;
use crate::iter::{Iter, IterMut};
use crate::keys::Keys;
use crate::EntityRef;
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
use std::slice;
use core::iter::FromIterator;
use core::marker::PhantomData;
use core::ops::{Index, IndexMut};
use core::slice;
use std::boxed::Box;
use std::vec::Vec;
/// A primary mapping `K -> V` allocating dense entity references.

View File

@@ -2,7 +2,7 @@
use crate::keys::Keys;
use crate::EntityRef;
use std::marker::PhantomData;
use core::marker::PhantomData;
use std::vec::Vec;
/// A set of `K` for densely indexed entity references.
@@ -80,7 +80,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use std::u32;
use core::u32;
// `EntityRef` impl for testing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]

View File

@@ -9,9 +9,9 @@
use crate::map::SecondaryMap;
use crate::EntityRef;
use std::mem;
use std::slice;
use std::u32;
use core::mem;
use core::slice;
use core::u32;
use std::vec::Vec;
/// Trait for extracting keys from values stored in a `SparseMap`.