From 3962897fa59daf4f8bcb03543276c89d7447a333 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 8 Jul 2024 12:01:05 +0200 Subject: [PATCH 1/2] Better default allocator handling --- Cargo.toml | 2 +- stabby-abi/build.rs | 11 ++++++++++- stabby-abi/src/alloc/allocators/mod.rs | 9 +++------ stabby-abi/src/alloc/boxed.rs | 6 ++---- stabby-abi/src/alloc/single_or_vec.rs | 2 +- stabby-abi/src/alloc/string.rs | 6 ++---- stabby-abi/src/alloc/sync.rs | 1 + stabby-abi/src/alloc/vec.rs | 7 ++----- stabby-abi/src/vtable/mod.rs | 21 ++++++++++++++++++--- stabby/src/time.rs | 3 +++ 10 files changed, 43 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 24704aa..140a64f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ license = " EPL-2.0 OR Apache-2.0" categories = ["development-tools::ffi", "no-std::no-alloc"] repository = "https://github.com/ZettaScaleLabs/stabby" readme = "stabby/README.md" -version = "36.1.1-rc7" # Track +version = "36.1.1-rc7" # Track [workspace.dependencies] stabby-macros = { path = "./stabby-macros/", version = "36.1.1-rc7", default-features = false } # Track diff --git a/stabby-abi/build.rs b/stabby-abi/build.rs index 7f3b937..dd7ae1f 100644 --- a/stabby-abi/build.rs +++ b/stabby-abi/build.rs @@ -114,7 +114,7 @@ fn main() { tuples(max_tuple).unwrap(); println!("cargo:rustc-check-cfg=cfg(stabby_nightly, values(none()))"); println!( - r#"cargo:rustc-check-cfg=cfg(stabby_default_alloc, values(none(), "RustAlloc", "LibcAlloc"))"# + r#"cargo:rustc-check-cfg=cfg(stabby_default_alloc, values("RustAlloc", "LibcAlloc", "disabled"))"# ); println!( r#"cargo:rustc-check-cfg=cfg(stabby_check_unreachable, values(none(), "true", "false"))"# @@ -123,6 +123,15 @@ fn main() { println!( r#"cargo:rustc-check-cfg=cfg(stabby_vtables, values(none(), "vec", "btree", "no_alloc"))"# ); + if std::env::var("CARGO_CFG_STABBY_DEFAULT_ALLOC").is_err() { + if std::env::var("CARGO_FEATURE_ALLOC_RS").is_ok() { + println!(r#"cargo:rustc-cfg=stabby_default_alloc="RustAlloc""#); + } else if std::env::var("CARGO_FEATURE_LIBC").is_ok() { + println!(r#"cargo:rustc-cfg=stabby_default_alloc="LibcAlloc""#); + } else { + println!(r#"cargo:rustc-cfg=stabby_default_alloc="disabled""#); + } + } if let Ok(toolchain) = std::env::var("RUSTUP_TOOLCHAIN") { if toolchain.starts_with("nightly") { println!("cargo:rustc-cfg=stabby_nightly"); diff --git a/stabby-abi/src/alloc/allocators/mod.rs b/stabby-abi/src/alloc/allocators/mod.rs index bc37d8c..c751a12 100644 --- a/stabby-abi/src/alloc/allocators/mod.rs +++ b/stabby-abi/src/alloc/allocators/mod.rs @@ -10,7 +10,7 @@ mod rust_alloc; #[cfg(feature = "alloc-rs")] pub use rust_alloc::RustAlloc; -#[cfg(any(stabby_default_alloc = "RustAlloc", feature = "alloc-rs"))] +#[cfg(stabby_default_alloc = "RustAlloc")] /// The default allocator, depending on which of the following is available: /// - RustAlloc: Rust's `GlobalAlloc`, through a vtable that ensures FFI-safety. /// - LibcAlloc: libc::malloc, which is 0-sized. @@ -19,10 +19,7 @@ pub use rust_alloc::RustAlloc; /// You can also use the `stabby_default_alloc` cfg to override the default allocator regardless of feature flags. pub(crate) type DefaultAllocator = RustAlloc; -#[cfg(any( - stabby_default_alloc = "LibcAlloc", - all(feature = "libc", not(feature = "alloc-rs")) -))] +#[cfg(stabby_default_alloc = "LibcAlloc")] /// The default allocator, depending on which of the following is available: /// - RustAlloc: Rust's `GlobalAlloc`, through a vtable that ensures FFI-safety. /// - LibcAlloc: libc::malloc, which is 0-sized. @@ -31,7 +28,7 @@ pub(crate) type DefaultAllocator = RustAlloc; /// You can also use the `stabby_default_alloc` cfg to override the default allocator regardless of feature flags. pub(crate) type DefaultAllocator = LibcAlloc; -#[cfg(not(any(stabby_default_alloc, feature = "alloc-rs", feature = "libc")))] +#[cfg(stabby_default_alloc = "disabled")] /// The default allocator, depending on which of the following is available: /// - RustAlloc: Rust's `GlobalAlloc`, through a vtable that ensures FFI-safety. /// - LibcAlloc: libc::malloc, which is 0-sized. diff --git a/stabby-abi/src/alloc/boxed.rs b/stabby-abi/src/alloc/boxed.rs index a26d6b2..f7f5c4d 100644 --- a/stabby-abi/src/alloc/boxed.rs +++ b/stabby-abi/src/alloc/boxed.rs @@ -35,10 +35,8 @@ unsafe impl Send for BoxedSlice {} // SAFETY: Same constraints as `std::boxed::Box` unsafe impl Sync for BoxedSlice {} -impl Box -where - super::DefaultAllocator: Default, -{ +#[cfg(not(stabby_default_alloc = "disabled"))] +impl Box { /// Attempts to allocate [`Self`], initializing it with `constructor`. /// /// Note that the allocation may or may not be zeroed. diff --git a/stabby-abi/src/alloc/single_or_vec.rs b/stabby-abi/src/alloc/single_or_vec.rs index c42aba8..91f9490 100644 --- a/stabby-abi/src/alloc/single_or_vec.rs +++ b/stabby-abi/src/alloc/single_or_vec.rs @@ -26,9 +26,9 @@ where inner: crate::Result, Vec>, } +#[cfg(not(stabby_default_alloc = "disabled"))] impl SingleOrVec where - DefaultAllocator: Default, Single: IDeterminantProvider>, Vec: IStable, crate::Result, Vec>: IStable, diff --git a/stabby-abi/src/alloc/string.rs b/stabby-abi/src/alloc/string.rs index 5705464..d5f51f0 100644 --- a/stabby-abi/src/alloc/string.rs +++ b/stabby-abi/src/alloc/string.rs @@ -13,10 +13,8 @@ pub struct String { pub(crate) inner: Vec, } -impl String -where - super::DefaultAllocator: Default, -{ +#[cfg(not(stabby_default_alloc = "disabled"))] +impl String { /// Constructs a new string using the default allocator. pub const fn new() -> Self { Self { inner: Vec::new() } diff --git a/stabby-abi/src/alloc/sync.rs b/stabby-abi/src/alloc/sync.rs index ee096a7..981a8e8 100644 --- a/stabby-abi/src/alloc/sync.rs +++ b/stabby-abi/src/alloc/sync.rs @@ -39,6 +39,7 @@ unsafe impl Send for Arc unsafe impl Sync for Arc {} const USIZE_TOP_BIT: usize = 1 << (core::mem::size_of::() as i32 * 8 - 1); +#[cfg(not(stabby_default_alloc = "disabled"))] impl Arc { /// Attempts to allocate [`Self`], initializing it with `constructor`. /// diff --git a/stabby-abi/src/alloc/vec.rs b/stabby-abi/src/alloc/vec.rs index 4bb34a0..bfaf1e3 100644 --- a/stabby-abi/src/alloc/vec.rs +++ b/stabby-abi/src/alloc/vec.rs @@ -63,16 +63,13 @@ pub(crate) const fn ptr_add(lhs: NonNull, rhs: usize) -> NonNull { } } -impl Vec -where - super::DefaultAllocator: Default, -{ +#[cfg(not(stabby_default_alloc = "disabled"))] +impl Vec { /// Constructs a new vector with the default allocator. This doesn't actually allocate. pub const fn new() -> Self { Self::new_in(super::DefaultAllocator::new()) } } - impl Vec { /// Constructs a new vector in `alloc`. This doesn't actually allocate. pub const fn new_in(alloc: Alloc) -> Self { diff --git a/stabby-abi/src/vtable/mod.rs b/stabby-abi/src/vtable/mod.rs index 90ad255..d10f928 100644 --- a/stabby-abi/src/vtable/mod.rs +++ b/stabby-abi/src/vtable/mod.rs @@ -41,10 +41,10 @@ pub trait IConstConstructor<'a, Source>: 'a + Copy { } } -#[cfg(all(any(feature = "libc", feature = "alloc-rs"), feature = "test"))] +#[cfg(all(not(stabby_default_alloc = "disabled"), feature = "test"))] pub use internal::{VTableRegistry, VtBtree, VtVec}; -#[cfg(any(feature = "libc", feature = "alloc-rs"))] +#[cfg(not(stabby_default_alloc = "disabled"))] pub(crate) mod internal { use crate::alloc::{boxed::BoxedSlice, collections::arc_btree::AtomicArcBTreeSet}; use core::ptr::NonNull; @@ -214,7 +214,7 @@ pub(crate) mod internal { } #[cfg(all( - any(feature = "libc", feature = "alloc-rs"), + not(stabby_default_alloc = "disabled"), any(stabby_vtables = "vec", stabby_vtables = "btree", not(stabby_vtables)) ))] #[rustversion::all(not(nightly), since(1.78.0))] @@ -229,6 +229,21 @@ pub trait IConstConstructor<'a, Source>: 'a + Copy { internal::VTABLES.insert_typed(&Self::VTABLE) } } +#[cfg(not(all( + not(stabby_default_alloc = "disabled"), + any(stabby_vtables = "vec", stabby_vtables = "btree", not(stabby_vtables)) +)))] +#[rustversion::all(not(nightly), since(1.78.0))] +/// Implementation detail for stabby's version of dyn traits. +/// Any type that implements a trait `ITrait` must implement `IConstConstructor` for `stabby::dyn!(Ptr)::from(value)` to work. +pub trait IConstConstructor<'a, Source>: 'a + Copy { + /// The vtable. + const VTABLE: Self; + /// Returns the reference to the vtable + fn vtable() -> &'a Self { + core::compile_error!("stabby's dyn traits need an allocator for non-nightly versions of Rust starting `1.78.0` and until https://github.com/rust-lang/rfcs/pull/3633 lands in stable.") + } +} /// Implementation detail for stabby's version of dyn traits. pub trait TransitiveDeref { /// Deref transitiverly. diff --git a/stabby/src/time.rs b/stabby/src/time.rs index 644ee84..d3792e9 100644 --- a/stabby/src/time.rs +++ b/stabby/src/time.rs @@ -31,9 +31,12 @@ impl Duration { nanos: ((micros % 1000000) * 1000) as u32, } } + /// Construct a new [`Duration`]. + /// /// # Panics /// if `secs` is negative. + #[cfg(feature = "std")] pub fn from_secs_f64(secs: f64) -> Self { assert!(secs >= 0.); Self { From eba7ed3f7851260492ddcef4083302c68f5fee81 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 8 Jul 2024 12:05:10 +0200 Subject: [PATCH 2/2] [skip ci] bump version --- CHANGELOG.md | 3 +++ Cargo.toml | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e4d806..858ec9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 36.1.1-rc8 (api=2.0.0, abi=2.0.0) +- Improve `DefaultAllocator` handling + # 36.1.1-rc7 (api=2.0.0, abi=2.0.0) - BREAKING CHANGES: - The in-place constructors for `Box` and `Arc` now require the initializer function to return a result, yielding the uninitialized allocation if allocation succeeded but initialization reported a failure. diff --git a/Cargo.toml b/Cargo.toml index 140a64f..a2d17c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,12 +33,12 @@ license = " EPL-2.0 OR Apache-2.0" categories = ["development-tools::ffi", "no-std::no-alloc"] repository = "https://github.com/ZettaScaleLabs/stabby" readme = "stabby/README.md" -version = "36.1.1-rc7" # Track +version = "36.1.1-rc8" # Track [workspace.dependencies] -stabby-macros = { path = "./stabby-macros/", version = "36.1.1-rc7", default-features = false } # Track -stabby-abi = { path = "./stabby-abi/", version = "36.1.1-rc7", default-features = false } # Track -stabby = { path = "./stabby/", version = "36.1.1-rc7", default-features = false } # Track +stabby-macros = { path = "./stabby-macros/", version = "36.1.1-rc8", default-features = false } # Track +stabby-abi = { path = "./stabby-abi/", version = "36.1.1-rc8", default-features = false } # Track +stabby = { path = "./stabby/", version = "36.1.1-rc8", default-features = false } # Track abi_stable = "0.11.0" libc = "0.2"