From 0f07a589d147b1c41ee96e706b368ed73d450ef6 Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 18 Feb 2026 14:09:50 +0100 Subject: [PATCH] feat: gate "alloc" requirement behind a feature fixes usage in bare metal no-std environments fixes #93 --- stabby-abi/Cargo.toml | 3 ++- stabby-abi/src/lib.rs | 2 ++ stabby/Cargo.toml | 3 ++- stabby/src/lib.rs | 2 ++ stabby/src/tests/traits.rs | 7 +++++++ 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/stabby-abi/Cargo.toml b/stabby-abi/Cargo.toml index c29dc71..99be617 100644 --- a/stabby-abi/Cargo.toml +++ b/stabby-abi/Cargo.toml @@ -28,7 +28,8 @@ workspace = true [features] default = ["std"] -std = ["alloc-rs"] +std = ["alloc", "alloc-rs"] +alloc = [] alloc-rs = [] experimental-ctypes = ["stabby-macros/experimental-ctypes"] libc = ["dep:libc"] diff --git a/stabby-abi/src/lib.rs b/stabby-abi/src/lib.rs index 8cf7d0e..7ce966b 100644 --- a/stabby-abi/src/lib.rs +++ b/stabby-abi/src/lib.rs @@ -31,6 +31,7 @@ extern crate alloc as alloc_rs; /// ABI-stable smart pointers and allocated data structures, with support for custom allocators. +#[cfg(feature = "alloc")] pub mod alloc; /// Extending [Non-Zero Types](core::num) to enable niches for other values than 0. pub mod num; @@ -144,6 +145,7 @@ mod fatptr; /// Closures, but ABI-stable pub mod closure; /// Futures, but ABI-stable +#[cfg(feature = "alloc")] pub mod future; mod stable_impls; /// Support for vtables for multi-trait objects diff --git a/stabby/Cargo.toml b/stabby/Cargo.toml index 064523b..e45a8c1 100644 --- a/stabby/Cargo.toml +++ b/stabby/Cargo.toml @@ -28,7 +28,8 @@ workspace = true [features] default = ["std"] -std = ["stabby-abi/std", "alloc-rs"] +std = ["stabby-abi/std", "alloc", "alloc-rs"] +alloc = ["stabby-abi/alloc"] alloc-rs = ["stabby-abi/alloc-rs"] experimental-ctypes = ["stabby-abi/experimental-ctypes"] libloading = ["dep:libloading", "std"] diff --git a/stabby/src/lib.rs b/stabby/src/lib.rs index 4e1ef22..11aca9f 100644 --- a/stabby/src/lib.rs +++ b/stabby/src/lib.rs @@ -30,6 +30,7 @@ pub use stabby_abi::{ pub use stabby_abi as abi; +#[cfg(feature = "alloc")] pub use stabby_abi::alloc::{self, boxed, collections, string, sync, vec}; pub use stabby_abi::{Dyn, DynRef}; @@ -42,6 +43,7 @@ pub use stabby_abi::tuple; stabby_unsafe_wakers = "true", deprecated = "Warning! you are using the `stabby/stabby_unsafe_wakers` feature. This could cause UB if you poll a future received from another shared library with mismatching ABI! (this API isn't actually deprecated)" )] +#[cfg(feature = "alloc")] pub mod future { pub use crate::abi::future::*; use crate::boxed::Box; diff --git a/stabby/src/tests/traits.rs b/stabby/src/tests/traits.rs index 62e962a..5ecfae6 100644 --- a/stabby/src/tests/traits.rs +++ b/stabby/src/tests/traits.rs @@ -17,6 +17,7 @@ #![cfg_attr(stabby_unsafe_wakers = "true", allow(deprecated))] pub use crate as stabby; +#[cfg(feature = "alloc")] use stabby::boxed::Box; #[stabby::stabby(checked)] @@ -76,6 +77,7 @@ pub trait MyTrait3 { extern "C" fn test2(&mut self); } +#[cfg(feature = "alloc")] impl MyTrait3> for u8 { type A = u8; type B = u8; @@ -88,6 +90,8 @@ impl MyTrait3> for u8 { extern "C" fn test(&mut self) {} extern "C" fn test2(&mut self) {} } + +#[cfg(feature = "alloc")] impl MyTrait3> for u16 { type A = u8; type B = u8; @@ -101,6 +105,7 @@ impl MyTrait3> for u16 { extern "C" fn test2(&mut self) {} } +#[cfg(feature = "alloc")] #[stabby::stabby(checked)] pub trait AsyncRead { extern "C" fn read<'a>( @@ -108,6 +113,7 @@ pub trait AsyncRead { buffer: stabby::slice::SliceMut<'a, u8>, ) -> stabby::future::DynFuture<'a, usize>; } +#[cfg(feature = "alloc")] impl AsyncRead for stabby::slice::Slice<'_, u8> { extern "C" fn read<'a>( &'a mut self, @@ -125,6 +131,7 @@ impl AsyncRead for stabby::slice::Slice<'_, u8> { } } +#[cfg(feature = "alloc")] #[test] fn dyn_traits() { let boxed = Box::new(6u8);