1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! A shim crate for to import items of alloc crate ergonomically.
//!
//! [Examples](https://github.com/taiki-e/alloc-shim/tree/master/examples)
//!
//! ## Usage
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! alloc-shim = "0.3.0"
//! ```
//!
//! Set the features so that `std` depends on `alloc-shim/std`, and `alloc` depends on `alloc-shim/alloc`:
//!
//! ```toml
//! [features]
//! std = ["alloc-shim/std"]
//! alloc = ["alloc-shim/alloc"]
//! ```
//!
//! Add this to your crate root (lib.rs or main.rs):
//!
//! ```rust,ignore
//! #![cfg_attr(feature = "alloc", feature(alloc))]
//! ```
//!
//! Now, you can use alloc-shim:
//!
//! ```rust
//! # #![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))]
//! #[cfg(any(feature = "alloc", feature = "std"))]
//! use alloc::prelude::*; // And more...
//! ```
//!
//! The current version of alloc-shim requires Rust 1.31 or later.
//!
//! ## Crate Features
//!
//! If not either `std` or `alloc` is specified, this crate does nothing.
//!
//! * `std`
//!   * Disabled by default.
//!   * Enable to use `std` crate.
//!
//! * `alloc`
//!   * Disabled by default.
//!   * Enable to use `alloc` crate.
//!   * Note that `std` crate is used if both `std` and `alloc` are specified at the same time.
//!   * This requires Rust Nightly.
//!
//! * `futures`
//!   * Disabled by default.
//!   * Enable to use `alloc::task`.
//!   * This requires Rust Nightly.
//!

#![doc(html_root_url = "https://docs.rs/alloc-shim/0.3.0")]
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))]
#![cfg_attr(feature = "futures", feature(futures_api))]

#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc as liballoc;

#[cfg(all(feature = "alloc", not(feature = "std")))]
mod shim {
    pub use liballoc::{
        alloc, borrow, boxed, collections, fmt, format, rc, slice, str, string, vec,
    };

    /// Synchronization primitives
    pub mod sync {
        pub use liballoc::sync::*;

        // `alloc::sync` does not include `atomic` module
        // pub use core::sync::atomic;
    }

    #[cfg(feature = "futures")]
    pub use liballoc::task;

    // FIXME(taiki-e):
    // `alloc::prelude` is required to use `#![feature(alloc)]` now.
    // Should we rewrite it so that it can be used without specifying `#![feature(alloc)]`?
    //
    // /// The alloc Prelude
    // pub mod prelude {
    //    pub use liballoc::prelude::*;
    // }
    pub use liballoc::prelude;
}

#[cfg(feature = "std")]
mod shim {
    pub use std::{alloc, borrow, boxed, collections, fmt, format, rc, slice, str, string, vec};

    /// Synchronization primitives
    pub mod sync {
        pub use std::sync::{Arc, Weak};

        // `alloc::sync` does not include `atomic` module
        // pub use std::sync::atomic;
    }

    #[cfg(feature = "futures")]
    pub use std::task;

    // The layout in the prelude module is different for `std` and `alloc`.
    /// The alloc Prelude
    pub use std::prelude::v1 as prelude;
}

#[cfg(any(feature = "alloc", feature = "std"))]
pub use self::shim::*;