From f2a6bfb2935aee04e80cd737eeb0f5366963e2eb Mon Sep 17 00:00:00 2001 From: CrazyboyQCD Date: Thu, 13 Feb 2025 10:50:51 +0800 Subject: [PATCH] make `std` feature conflict with `hashbrown` in compile error and gate `free_list_allocator` by feature --- .../dedicated_block_allocator/mod.rs | 19 ++++++++------ src/allocator/free_list_allocator/mod.rs | 25 ++++++++++++------- src/allocator/mod.rs | 2 ++ src/lib.rs | 4 +-- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/allocator/dedicated_block_allocator/mod.rs b/src/allocator/dedicated_block_allocator/mod.rs index 44a810a..0ae374a 100644 --- a/src/allocator/dedicated_block_allocator/mod.rs +++ b/src/allocator/dedicated_block_allocator/mod.rs @@ -101,17 +101,20 @@ impl SubAllocator for DedicatedBlockAllocator { ) { let empty = "".to_string(); let name = self.name.as_ref().unwrap_or(&empty); - - let backtrace_info = if cfg!(feature = "std") { - format!( + let backtrace_info; + #[cfg(feature = "std")] + { + backtrace_info = format!( r#" - backtrace: {} - "#, + backtrace: {} + "#, self.backtrace ) - } else { - "".to_owned() - }; + } + #[cfg(not(feature = "std"))] + { + backtrace_info = "".to_owned() + } log!( log_level, diff --git a/src/allocator/free_list_allocator/mod.rs b/src/allocator/free_list_allocator/mod.rs index 44adcd5..39384ea 100644 --- a/src/allocator/free_list_allocator/mod.rs +++ b/src/allocator/free_list_allocator/mod.rs @@ -5,12 +5,15 @@ use alloc::{ string::{String, ToString}, vec::Vec, }; -#[cfg(all(feature = "std", not(feature = "hashbrown")))] -use std::collections::{HashMap, HashSet}; + #[cfg(feature = "std")] -use std::{backtrace::Backtrace, sync::Arc}; +use std::{ + backtrace::Backtrace, + collections::{HashMap, HashSet}, + sync::Arc, +}; -#[cfg(all(not(feature = "std"), feature = "hashbrown"))] +#[cfg(feature = "hashbrown")] use hashbrown::{HashMap, HashSet}; use log::{log, Level}; @@ -377,16 +380,20 @@ impl SubAllocator for FreeListAllocator { } let empty = "".to_string(); let name = chunk.name.as_ref().unwrap_or(&empty); - let backtrace_info = if cfg!(feature = "std") { - format!( + let backtrace_info; + #[cfg(feature = "std")] + { + backtrace_info = format!( r#" backtrace: {} "#, chunk.backtrace ) - } else { - "".to_owned() - }; + } + #[cfg(not(feature = "std"))] + { + backtrace_info = "".to_owned() + } log!( log_level, r#"leak detected: {{ diff --git a/src/allocator/mod.rs b/src/allocator/mod.rs index 9a3cf46..3bc3b33 100644 --- a/src/allocator/mod.rs +++ b/src/allocator/mod.rs @@ -10,7 +10,9 @@ use crate::result::*; pub(crate) mod dedicated_block_allocator; pub(crate) use dedicated_block_allocator::DedicatedBlockAllocator; +#[cfg(any(feature = "std", feature = "hashbrown"))] pub(crate) mod free_list_allocator; +#[cfg(any(feature = "std", feature = "hashbrown"))] pub(crate) use free_list_allocator::FreeListAllocator; #[derive(PartialEq, Copy, Clone, Debug)] diff --git a/src/lib.rs b/src/lib.rs index f99da25..771147e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -222,8 +222,8 @@ #[macro_use] extern crate alloc; -#[cfg(all(not(feature = "std"), not(feature = "hashbrown")))] -compile_error!("\"hashbrown\" feature should be enabled in \"no_std\" environment."); +#[cfg(all(feature = "std", feature = "hashbrown"))] +compile_error!("\"hashbrown\" feature should not be enabled in \"std\" environment."); #[cfg(all(not(feature = "std"), feature = "visualizer"))] compile_error!("Cannot enable \"visualizer\" feature in \"no_std\" environment.");