|
3 | 3 | //! This crate works fully on stable Rust, and also does not require the
|
4 | 4 | //! standard library. To disable references to the standard library, you must
|
5 | 5 | //! opt-out of the `std` feature using `default-features = false` in your
|
6 |
| -//! `Cargo.toml` file. |
| 6 | +//! `Cargo.toml` file. When in no-std mode, a separate `alloc` feature flag |
| 7 | +//! is available to support casting to several [`alloc`] types not included |
| 8 | +//! in [`core`]. |
7 | 9 | //!
|
8 | 10 | //! Castaway provides the following key macros:
|
9 | 11 | //!
|
|
12 | 14 | //! - [`match_type`]: Match the result of an expression against multiple
|
13 | 15 | //! concrete types.
|
14 | 16 |
|
15 |
| -#![cfg_attr(not(feature = "std"), no_std)] |
| 17 | +#![no_std] |
| 18 | + |
| 19 | +#[cfg(feature = "std")] |
| 20 | +extern crate std; |
| 21 | + |
| 22 | +#[cfg(feature = "alloc")] |
| 23 | +extern crate alloc; |
16 | 24 |
|
17 | 25 | #[doc(hidden)]
|
18 | 26 | pub mod internal;
|
@@ -79,8 +87,10 @@ pub use lifetime_free::LifetimeFree;
|
79 | 87 | /// `'static`. To mark a type as being lifetime-free and enable it to be casted
|
80 | 88 | /// to in this manner by this macro it must implement the [`LifetimeFree`]
|
81 | 89 | /// trait. This is implemented automatically for all primitive types and for
|
82 |
| -/// several `core` types. If you enable the `std` crate feature, then it will |
83 |
| -/// also be implemented for several `std` types as well. |
| 90 | +/// several [`core`] types. If you enable the `std` crate feature, then it will |
| 91 | +/// also be implemented for several [`std`] types as well. If you enable the |
| 92 | +/// `alloc` crate feature, then it will be implemented for several [`alloc`] |
| 93 | +/// types without linking to the standard library as the `std` feature would. |
84 | 94 | ///
|
85 | 95 | /// # Examples
|
86 | 96 | ///
|
@@ -278,6 +288,9 @@ macro_rules! match_type {
|
278 | 288 | mod tests {
|
279 | 289 | use super::*;
|
280 | 290 |
|
| 291 | + #[cfg(feature = "alloc")] |
| 292 | + use alloc::string::String; |
| 293 | + |
281 | 294 | #[test]
|
282 | 295 | fn cast() {
|
283 | 296 | assert_eq!(cast!(0u8, u16), Err(0u8));
|
@@ -489,7 +502,7 @@ mod tests {
|
489 | 502 | 3.2f64 => Err(v) if v == 3.2f64,
|
490 | 503 | }
|
491 | 504 |
|
492 |
| - #[cfg(feature = "std")] |
| 505 | + #[cfg(feature = "alloc")] |
493 | 506 | for String {
|
494 | 507 | String::from("hello world") => Ok(ref v) if v.as_str() == "hello world",
|
495 | 508 | "hello world" => Err("hello world"),
|
|
0 commit comments