diff --git a/Cargo.toml b/Cargo.toml index adc8ae2..bd6866b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,8 @@ all-features = true [features] default = ["std"] -std = [] +std = ["alloc"] +alloc = [] [dependencies] rustversion = "1" diff --git a/src/lib.rs b/src/lib.rs index ee2fd94..8b28e44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,9 @@ //! This crate works fully on stable Rust, and also does not require the //! standard library. To disable references to the standard library, you must //! opt-out of the `std` feature using `default-features = false` in your -//! `Cargo.toml` file. +//! `Cargo.toml` file. When in no-std mode, a separate `alloc` feature flag +//! is available to support casting to several [`alloc`] types not included +//! in [`core`]. //! //! Castaway provides the following key macros: //! @@ -12,7 +14,13 @@ //! - [`match_type`]: Match the result of an expression against multiple //! concrete types. -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] + +#[cfg(feature = "std")] +extern crate std; + +#[cfg(feature = "alloc")] +extern crate alloc; #[doc(hidden)] pub mod internal; @@ -79,8 +87,10 @@ pub use lifetime_free::LifetimeFree; /// `'static`. To mark a type as being lifetime-free and enable it to be casted /// to in this manner by this macro it must implement the [`LifetimeFree`] /// trait. This is implemented automatically for all primitive types and for -/// several `core` types. If you enable the `std` crate feature, then it will -/// also be implemented for several `std` types as well. +/// several [`core`] types. If you enable the `std` crate feature, then it will +/// also be implemented for several [`std`] types as well. If you enable the +/// `alloc` crate feature, then it will be implemented for several [`alloc`] +/// types without linking to the standard library as the `std` feature would. /// /// # Examples /// @@ -278,6 +288,9 @@ macro_rules! match_type { mod tests { use super::*; + #[cfg(feature = "alloc")] + use alloc::string::String; + #[test] fn cast() { assert_eq!(cast!(0u8, u16), Err(0u8)); @@ -489,7 +502,7 @@ mod tests { 3.2f64 => Err(v) if v == 3.2f64, } - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] for String { String::from("hello world") => Ok(ref v) if v.as_str() == "hello world", "hello world" => Err("hello world"), diff --git a/src/lifetime_free.rs b/src/lifetime_free.rs index 49ffe59..e681645 100644 --- a/src/lifetime_free.rs +++ b/src/lifetime_free.rs @@ -2,7 +2,7 @@ /// types are safe to cast from non-static type parameters if their types are /// equal. /// -/// This trait is used by [`cast!`] to determine what casts are legal on values +/// This trait is used by [`cast!`](crate::cast) to determine what casts are legal on values /// without a `'static` type constraint. /// /// # Safety @@ -104,13 +104,15 @@ tuple_impls! { T0 T1 T2 T3 T4 T5 T6 T7 T8 T9, } -#[cfg(feature = "std")] -mod std_impls { +#[cfg(feature = "alloc")] +mod alloc_impls { use super::LifetimeFree; - unsafe impl LifetimeFree for String {} + unsafe impl LifetimeFree for alloc::string::String {} - unsafe impl LifetimeFree for Box {} - unsafe impl LifetimeFree for Vec {} - unsafe impl LifetimeFree for std::sync::Arc {} + unsafe impl LifetimeFree for alloc::boxed::Box {} + unsafe impl LifetimeFree for alloc::vec::Vec {} + + #[rustversion::attr(since(1.60), cfg(target_has_atomic = "ptr"))] + unsafe impl LifetimeFree for alloc::sync::Arc {} }