Skip to content

Commit 564b11f

Browse files
authored
allow no-std but alloc (#13)
There were a few types which implement `LifetimeFree` only in `std` mode. This PR lowers the requirement by introducing a new `alloc` feature that enables these traits in `no-std` mode but when `alloc` is still available. These types include `String`, `Box`, `Vec`, and `Arc`, which has an additional (automatic) cfg due to only existing on platforms with ptr-sized atomics.
1 parent 437fbda commit 564b11f

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ all-features = true
1414

1515
[features]
1616
default = ["std"]
17-
std = []
17+
std = ["alloc"]
18+
alloc = []
1819

1920
[dependencies]
2021
rustversion = "1"

src/lib.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! This crate works fully on stable Rust, and also does not require the
44
//! standard library. To disable references to the standard library, you must
55
//! 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`].
79
//!
810
//! Castaway provides the following key macros:
911
//!
@@ -12,7 +14,13 @@
1214
//! - [`match_type`]: Match the result of an expression against multiple
1315
//! concrete types.
1416
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;
1624

1725
#[doc(hidden)]
1826
pub mod internal;
@@ -79,8 +87,10 @@ pub use lifetime_free::LifetimeFree;
7987
/// `'static`. To mark a type as being lifetime-free and enable it to be casted
8088
/// to in this manner by this macro it must implement the [`LifetimeFree`]
8189
/// 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.
8494
///
8595
/// # Examples
8696
///
@@ -278,6 +288,9 @@ macro_rules! match_type {
278288
mod tests {
279289
use super::*;
280290

291+
#[cfg(feature = "alloc")]
292+
use alloc::string::String;
293+
281294
#[test]
282295
fn cast() {
283296
assert_eq!(cast!(0u8, u16), Err(0u8));
@@ -489,7 +502,7 @@ mod tests {
489502
3.2f64 => Err(v) if v == 3.2f64,
490503
}
491504

492-
#[cfg(feature = "std")]
505+
#[cfg(feature = "alloc")]
493506
for String {
494507
String::from("hello world") => Ok(ref v) if v.as_str() == "hello world",
495508
"hello world" => Err("hello world"),

src/lifetime_free.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// types are safe to cast from non-static type parameters if their types are
33
/// equal.
44
///
5-
/// This trait is used by [`cast!`] to determine what casts are legal on values
5+
/// This trait is used by [`cast!`](crate::cast) to determine what casts are legal on values
66
/// without a `'static` type constraint.
77
///
88
/// # Safety
@@ -104,13 +104,15 @@ tuple_impls! {
104104
T0 T1 T2 T3 T4 T5 T6 T7 T8 T9,
105105
}
106106

107-
#[cfg(feature = "std")]
108-
mod std_impls {
107+
#[cfg(feature = "alloc")]
108+
mod alloc_impls {
109109
use super::LifetimeFree;
110110

111-
unsafe impl LifetimeFree for String {}
111+
unsafe impl LifetimeFree for alloc::string::String {}
112112

113-
unsafe impl<T: LifetimeFree> LifetimeFree for Box<T> {}
114-
unsafe impl<T: LifetimeFree> LifetimeFree for Vec<T> {}
115-
unsafe impl<T: LifetimeFree> LifetimeFree for std::sync::Arc<T> {}
113+
unsafe impl<T: LifetimeFree> LifetimeFree for alloc::boxed::Box<T> {}
114+
unsafe impl<T: LifetimeFree> LifetimeFree for alloc::vec::Vec<T> {}
115+
116+
#[rustversion::attr(since(1.60), cfg(target_has_atomic = "ptr"))]
117+
unsafe impl<T: LifetimeFree> LifetimeFree for alloc::sync::Arc<T> {}
116118
}

0 commit comments

Comments
 (0)