Skip to content

Commit 96d7c6c

Browse files
committed
🚸 Don't make users declare alloc crate
1 parent 68e6025 commit 96d7c6c

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

‎src/boxed.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
//! ```
1212
//! use mayheap::{box_pool, boxed::{BoxPool, Box}};
1313
//!
14-
//! extern crate alloc;
15-
//!
1614
//! // Create a pool for u32 type with a capacity of 10.
1715
//! box_pool!(MyBoxPool: u32, 2);
1816
//!
@@ -93,14 +91,16 @@ macro_rules! box_pool {
9391

9492
impl $crate::boxed::BoxPool for $name {
9593
type Data = $ty;
96-
type BoxedValue = alloc::boxed::Box<$ty>;
94+
type BoxedValue = $crate::reexports::alloc::boxed::Box<$ty>;
9795

9896
fn alloc(&self, value: Self::Data) -> Result<$crate::boxed::Box<Self>, Self::Data> {
99-
Ok($crate::boxed::Box::new(alloc::boxed::Box::new(value)))
97+
Ok($crate::boxed::Box::new(
98+
$crate::reexports::alloc::boxed::Box::new(value),
99+
))
100100
}
101101
}
102102

103-
$crate::paste::paste! {
103+
$crate::reexports::paste::paste! {
104104
// Let's use the $capacity variable so callers don't get "unused const" warnings.
105105
#[allow(non_upper_case_globals, dead_code)]
106106
const [<__dummy__ $name>]: () = {
@@ -115,7 +115,7 @@ macro_rules! box_pool {
115115
#[macro_export]
116116
macro_rules! box_pool {
117117
($name:ident: $ty:ty, $capacity:expr) => {
118-
$crate::paste::paste! {
118+
$crate::reexports::paste::paste! {
119119
heapless::box_pool!([<$name Pool>]: $ty);
120120

121121
#[derive(Debug, Clone, PartialEq, Eq)]

‎src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
#[cfg(all(not(feature = "alloc"), not(feature = "heapless")))]
1313
compile_error!("Either the `alloc` or `heapless` feature must be enabled");
1414

15-
#[cfg(feature = "alloc")]
16-
extern crate alloc;
17-
18-
// Re-export `paste` for the macros.
15+
// Re-exports for the macros.
1916
#[doc(hidden)]
20-
pub use paste;
17+
pub mod reexports {
18+
#[cfg(feature = "alloc")]
19+
pub extern crate alloc;
20+
pub use paste;
21+
}
2122

2223
pub mod vec;
2324
pub use vec::Vec;

‎src/string.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::{cmp::Ordering, fmt, hash, iter, ops, str};
55
use crate::Vec;
66

77
#[cfg(feature = "alloc")]
8-
type Inner<const N: usize> = alloc::string::String;
8+
type Inner<const N: usize> = crate::reexports::alloc::string::String;
99
#[cfg(not(feature = "alloc"))]
1010
type Inner<const N: usize> = heapless::String<N>;
1111

@@ -357,7 +357,9 @@ macro_rules! impl_try_from_num {
357357
fn try_from(s: $num) -> Result<Self, Self::Error> {
358358
#[cfg(feature = "alloc")]
359359
{
360-
Ok(Self(alloc::string::ToString::to_string(&s)))
360+
Ok(Self(crate::reexports::alloc::string::ToString::to_string(
361+
&s,
362+
)))
361363
}
362364
#[cfg(not(feature = "alloc"))]
363365
{

‎src/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use core::{cmp::Ordering, fmt, hash, iter::FromIterator, ops, slice};
66

77
#[cfg(feature = "alloc")]
8-
pub(crate) type Inner<T, const N: usize> = alloc::vec::Vec<T>;
8+
pub(crate) type Inner<T, const N: usize> = crate::reexports::alloc::vec::Vec<T>;
99
#[cfg(not(feature = "alloc"))]
1010
pub(crate) type Inner<T, const N: usize> = heapless::Vec<T, N>;
1111

@@ -437,7 +437,7 @@ impl<T, const N: usize> FromIterator<T> for Vec<T, N> {
437437
#[derive(Clone, Debug)]
438438
pub struct IntoIter<T, const N: usize> {
439439
#[cfg(feature = "alloc")]
440-
iter: alloc::vec::IntoIter<T>,
440+
iter: crate::reexports::alloc::vec::IntoIter<T>,
441441
// FIXME: Once the fix for https://github.com/rust-embedded/heapless/issues/530 is released. We
442442
// can turn this into a wrapper around `heapless::vec::IntoIter`.
443443
#[cfg(not(feature = "alloc"))]

0 commit comments

Comments
 (0)