Skip to content

Commit 82b60dc

Browse files
authored
Merge pull request #58 from creativcoder/work_on_stable
move Alloc trait methods as WeeAlloc methods
2 parents 573e70c + 59116e6 commit 82b60dc

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cfg-if = "0.1.2"
1212
[dependencies.wee_alloc]
1313
path = "../wee_alloc"
1414
default-features = false
15-
features = ["use_std_for_test_debugging"]
15+
features = ["use_std_for_test_debugging", "nightly"]
1616

1717
[features]
1818
size_classes = ["wee_alloc/size_classes"]

wee_alloc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ travis-ci = { repository = "rustwasm/wee_alloc" }
1818

1919
[features]
2020
default = ["size_classes"]
21+
nightly = []
2122

2223
# Enable extra, expensive integrity allocations.
2324
extra_assertions = []

wee_alloc/src/lib.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ mod neighbors;
272272
mod size_classes;
273273

274274
use const_init::ConstInit;
275-
use core::alloc::{Alloc, AllocErr, GlobalAlloc, Layout};
275+
#[cfg(feature = "nightly")]
276+
use core::alloc::Alloc;
277+
use core::alloc::{AllocErr, GlobalAlloc, Layout};
276278
use core::cell::Cell;
277279
use core::cmp;
278280
use core::marker::Sync;
@@ -1069,13 +1071,8 @@ impl<'a> WeeAlloc<'a> {
10691071
result
10701072
})
10711073
}
1072-
}
10731074

1074-
unsafe impl<'a, 'b> Alloc for &'b WeeAlloc<'a>
1075-
where
1076-
'a: 'b,
1077-
{
1078-
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
1075+
unsafe fn alloc_impl(&self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
10791076
let size = Bytes(layout.size());
10801077
let align = if layout.align() == 0 {
10811078
Bytes(1)
@@ -1098,7 +1095,7 @@ where
10981095
})
10991096
}
11001097

1101-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
1098+
unsafe fn dealloc_impl(&self, ptr: NonNull<u8>, layout: Layout) {
11021099
let size = Bytes(layout.size());
11031100
if size.0 == 0 {
11041101
return;
@@ -1187,19 +1184,31 @@ where
11871184
}
11881185
}
11891186

1187+
#[cfg(feature = "nightly")]
1188+
unsafe impl<'a, 'b> Alloc for &'b WeeAlloc<'a>
1189+
where
1190+
'a: 'b,
1191+
{
1192+
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
1193+
self.alloc_impl(layout)
1194+
}
1195+
1196+
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
1197+
self.dealloc_impl(ptr, layout)
1198+
}
1199+
}
1200+
11901201
unsafe impl GlobalAlloc for WeeAlloc<'static> {
11911202
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
1192-
let mut me = self;
1193-
match Alloc::alloc(&mut me, layout) {
1203+
match self.alloc_impl(layout) {
11941204
Ok(ptr) => ptr.as_ptr(),
11951205
Err(AllocErr) => 0 as *mut u8,
11961206
}
11971207
}
11981208

11991209
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
12001210
if let Some(ptr) = NonNull::new(ptr) {
1201-
let mut me = self;
1202-
Alloc::dealloc(&mut me, ptr, layout);
1211+
self.dealloc_impl(ptr, layout);
12031212
}
12041213
}
12051214
}

0 commit comments

Comments
 (0)