Skip to content

Commit 92ebc45

Browse files
committed
refactor: Add alloc feature to use boxes when default feature std is disabled
1 parent d1413c6 commit 92ebc45

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ repository = "https://github.com/jhg/opaque-pointer-rs/"
1313
[features]
1414
default = ["std"]
1515
std = []
16+
alloc = []
1617
c-types = []

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
#![no_std]
1212

13-
#[cfg(not(feature = "std"))]
13+
#[cfg(all(feature = "alloc", not(feature = "std")))]
1414
extern crate alloc;
15-
#[cfg(not(feature = "std"))]
15+
#[cfg(all(feature = "alloc", not(feature = "std")))]
1616
use alloc::boxed::Box;
1717

1818
#[cfg(feature = "std")]
@@ -32,6 +32,7 @@ fn panic_if_null<T>(pointer: *const T) {
3232
}
3333

3434
/// Convert type to raw pointer.
35+
#[cfg(any(feature = "alloc", feature = "std"))]
3536
#[inline]
3637
pub fn raw<T>(data: T) -> *mut T {
3738
return Box::into_raw(Box::new(data));
@@ -42,6 +43,7 @@ pub fn raw<T>(data: T) -> *mut T {
4243
/// # Safety
4344
///
4445
/// Never call it twice. That could produce a HEAP error that produce a crash.
46+
#[cfg(any(feature = "alloc", feature = "std"))]
4547
#[inline]
4648
pub unsafe fn free<T>(pointer: *mut T) {
4749
if pointer.is_null() {
@@ -60,6 +62,7 @@ pub unsafe fn free<T>(pointer: *mut T) {
6062
/// # Safety
6163
///
6264
/// Never call it twice. That could produce a HEAP error that produce a crash.
65+
#[cfg(any(feature = "alloc", feature = "std"))]
6366
#[inline]
6467
pub unsafe fn own_back<T>(pointer: *mut T) -> T {
6568
panic_if_null(pointer);

0 commit comments

Comments
 (0)