Skip to content

Commit

Permalink
make std feature conflict with hashbrown in compile error and gat…
Browse files Browse the repository at this point in the history
…e `free_list_allocator` by feature
  • Loading branch information
CrazyboyQCD committed Feb 13, 2025
1 parent 4c45a01 commit f2a6bfb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
19 changes: 11 additions & 8 deletions src/allocator/dedicated_block_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 16 additions & 9 deletions src/allocator/free_list_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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: {{
Expand Down
2 changes: 2 additions & 0 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down

0 comments on commit f2a6bfb

Please sign in to comment.