From c8f0c7acbbcced0ae4756355366a5f3cb05cf11c Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Wed, 30 Aug 2023 12:43:48 -0500 Subject: [PATCH 1/3] allow no-std but alloc --- Cargo.toml | 3 ++- src/lib.rs | 13 +++++++++++-- src/lifetime_free.rs | 14 ++++++++------ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8094c01..a12732a 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..6d2177c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,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; @@ -278,6 +284,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 +498,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 36fc330..facd593 100644 --- a/src/lifetime_free.rs +++ b/src/lifetime_free.rs @@ -83,13 +83,15 @@ unsafe impl LifetimeFree for core::num::Wrapping {} unsafe impl LifetimeFree for core::cell::Cell {} unsafe impl LifetimeFree for core::cell::RefCell {} -#[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 {} + + #[cfg(target_has_atomic = "ptr")] + unsafe impl LifetimeFree for alloc::sync::Arc {} } From 7512ea719f683d2e509e281447cc597a79b3dd14 Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Tue, 19 Sep 2023 15:00:28 -0500 Subject: [PATCH 2/3] fix msrc issue for cfg atomic --- src/lifetime_free.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lifetime_free.rs b/src/lifetime_free.rs index facd593..32c5bca 100644 --- a/src/lifetime_free.rs +++ b/src/lifetime_free.rs @@ -92,6 +92,6 @@ mod alloc_impls { unsafe impl LifetimeFree for alloc::boxed::Box {} unsafe impl LifetimeFree for alloc::vec::Vec {} - #[cfg(target_has_atomic = "ptr")] + #[rustversion::attr(since(1.60), cfg(target_has_atomic = "ptr"))] unsafe impl LifetimeFree for alloc::sync::Arc {} } From 7ac25205772447aaa3bbaa1bd1e6e34a85e2d45c Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Tue, 19 Sep 2023 15:24:35 -0500 Subject: [PATCH 3/3] update docs --- src/lib.rs | 10 +++++++--- src/lifetime_free.rs | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6d2177c..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: //! @@ -85,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 /// diff --git a/src/lifetime_free.rs b/src/lifetime_free.rs index 32c5bca..314188a 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