Skip to content

Commit 2bac3ae

Browse files
authored
Migrate the wasmtime-slab crate to no_std (#8483)
Nothing major here other than renaming crate imports and such.
1 parent 3e4b0b9 commit 2bac3ae

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ jobs:
372372
- run: cargo check -p wasmtime-asm-macros
373373
env:
374374
CARGO_BUILD_TARGET: x86_64-unknown-none
375+
- run: cargo check -p wasmtime-slab
376+
env:
377+
CARGO_BUILD_TARGET: x86_64-unknown-none
375378

376379
# Check that wasmtime-runtime compiles with panic=abort since there's some
377380
# #[cfg] for specifically panic=abort there.

crates/slab/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,23 @@
120120
//! }
121121
//! ```
122122
123+
#![no_std]
123124
#![forbid(unsafe_code)]
124125
#![deny(missing_docs, missing_debug_implementations)]
125126

126-
use std::num::NonZeroU32;
127+
extern crate alloc;
128+
129+
use alloc::vec::Vec;
130+
use core::fmt;
131+
use core::num::NonZeroU32;
127132

128133
/// An identifier for an allocated value inside a `slab`.
129134
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
130135
#[repr(transparent)]
131136
pub struct Id(EntryIndex);
132137

133-
impl std::fmt::Debug for Id {
134-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
138+
impl fmt::Debug for Id {
139+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135140
f.debug_tuple("Id").field(&self.0.index()).finish()
136141
}
137142
}
@@ -167,11 +172,11 @@ pub struct Slab<T> {
167172
len: u32,
168173
}
169174

170-
impl<T> std::fmt::Debug for Slab<T>
175+
impl<T> fmt::Debug for Slab<T>
171176
where
172-
T: std::fmt::Debug,
177+
T: fmt::Debug,
173178
{
174-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175180
f.debug_map().entries(self.iter()).finish()
176181
}
177182
}
@@ -218,7 +223,7 @@ impl<T> Default for Slab<T> {
218223
}
219224
}
220225

221-
impl<T> std::ops::Index<Id> for Slab<T> {
226+
impl<T> core::ops::Index<Id> for Slab<T> {
222227
type Output = T;
223228

224229
#[inline]
@@ -228,7 +233,7 @@ impl<T> std::ops::Index<Id> for Slab<T> {
228233
}
229234
}
230235

231-
impl<T> std::ops::IndexMut<Id> for Slab<T> {
236+
impl<T> core::ops::IndexMut<Id> for Slab<T> {
232237
#[inline]
233238
fn index_mut(&mut self, id: Id) -> &mut Self::Output {
234239
self.get_mut(id)
@@ -282,7 +287,7 @@ impl<T> Slab<T> {
282287
// we add some amount of minimum additional capacity, since doubling
283288
// zero capacity isn't useful.
284289
const MIN_CAPACITY: usize = 16;
285-
let additional = std::cmp::max(self.entries.capacity(), MIN_CAPACITY);
290+
let additional = core::cmp::max(self.entries.capacity(), MIN_CAPACITY);
286291
self.reserve(additional);
287292
}
288293

@@ -419,7 +424,7 @@ impl<T> Slab<T> {
419424
/// deallocate an arbitrary value.
420425
#[inline]
421426
pub fn dealloc(&mut self, id: Id) -> T {
422-
let entry = std::mem::replace(
427+
let entry = core::mem::replace(
423428
self.entries
424429
.get_mut(id.0.index())
425430
.expect("id from a different slab"),
@@ -428,7 +433,7 @@ impl<T> Slab<T> {
428433
match entry {
429434
Entry::Free { .. } => panic!("attempt to deallocate an entry that is already vacant"),
430435
Entry::Occupied(value) => {
431-
let next_free = std::mem::replace(&mut self.free, Some(id.0));
436+
let next_free = core::mem::replace(&mut self.free, Some(id.0));
432437
self.entries[id.0.index()] = Entry::Free { next_free };
433438
self.len -= 1;
434439
value

0 commit comments

Comments
 (0)