Skip to content

Commit fff32a1

Browse files
committed
Factor out the extern crate alloc into the guest-rust crate.
1 parent 3ad66a5 commit fff32a1

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

crates/gen-guest-rust/src/lib.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ impl Generator for RustWasm {
167167
));
168168
}
169169

170+
self.src.push_str("#[allow(unused_imports)]");
171+
self.src
172+
.push_str("use wit_bindgen_guest_rust::{alloc, vec::Vec, string::String};");
173+
170174
self.sizes.fill(iface);
171175
}
172176

@@ -510,9 +514,7 @@ impl Generator for RustWasm {
510514
} = f;
511515

512516
if needs_cleanup_list {
513-
self.src.push_str("extern crate alloc;\n");
514-
self.src
515-
.push_str("let mut cleanup_list = alloc::vec::Vec::new();\n");
517+
self.src.push_str("let mut cleanup_list = Vec::new();\n");
516518
}
517519
self.src.push_str(&String::from(src));
518520

@@ -760,15 +762,14 @@ impl FunctionBindgen<'_> {
760762
fn emit_cleanup(&mut self) {
761763
for (ptr, layout) in mem::take(&mut self.cleanup) {
762764
self.push_str(&format!(
763-
"if {layout}.size() != 0 {{\nextern crate alloc;\nalloc::alloc::dealloc({ptr}, {layout});\n}}\n"
765+
"if {layout}.size() != 0 {{\nalloc::dealloc({ptr}, {layout});\n}}\n"
764766
));
765767
}
766768
if self.needs_cleanup_list {
767769
self.push_str(
768770
"for (ptr, layout) in cleanup_list {\n
769771
if layout.size() != 0 {\n
770-
extern crate alloc;\n
771-
alloc::alloc::dealloc(ptr, layout);\n
772+
alloc::dealloc(ptr, layout);\n
772773
}\n
773774
}\n",
774775
);
@@ -1346,9 +1347,8 @@ impl Bindgen for FunctionBindgen<'_> {
13461347
let tmp = self.tmp();
13471348
let len = format!("len{}", tmp);
13481349
self.push_str(&format!("let {} = {} as usize;\n", len, operands[1]));
1349-
self.push_str("extern crate alloc;\n");
13501350
let result = format!(
1351-
"alloc::vec::Vec::from_raw_parts({} as *mut _, {1}, {1})",
1351+
"Vec::from_raw_parts({} as *mut _, {1}, {1})",
13521352
operands[0], len
13531353
);
13541354
results.push(result);
@@ -1378,21 +1378,14 @@ impl Bindgen for FunctionBindgen<'_> {
13781378
let tmp = self.tmp();
13791379
let len = format!("len{}", tmp);
13801380
self.push_str(&format!("let {} = {} as usize;\n", len, operands[1]));
1381-
self.push_str("extern crate alloc;\n");
13821381
let result = format!(
1383-
"alloc::vec::Vec::from_raw_parts({} as *mut _, {1}, {1})",
1382+
"Vec::from_raw_parts({} as *mut _, {1}, {1})",
13841383
operands[0], len
13851384
);
13861385
if unchecked {
1387-
results.push(format!(
1388-
"alloc::string::String::from_utf8_unchecked({})",
1389-
result
1390-
));
1386+
results.push(format!("String::from_utf8_unchecked({})", result));
13911387
} else {
1392-
results.push(format!(
1393-
"alloc::string::String::from_utf8({}).unwrap()",
1394-
result
1395-
));
1388+
results.push(format!("String::from_utf8({}).unwrap()", result));
13961389
}
13971390
}
13981391

@@ -1411,13 +1404,13 @@ impl Bindgen for FunctionBindgen<'_> {
14111404
let size = self.gen.sizes.size(element);
14121405
let align = self.gen.sizes.align(element);
14131406
self.push_str(&format!(
1414-
"let {layout} = core::alloc::Layout::from_size_align_unchecked({vec}.len() * {size}, {align});\n",
1407+
"let {layout} = alloc::Layout::from_size_align_unchecked({vec}.len() * {size}, {align});\n",
14151408
));
14161409
self.push_str(&format!(
1417-
"let {result} = if {layout}.size() != 0\n{{\nextern crate alloc;\nlet ptr = alloc::alloc::alloc({layout});\n",
1410+
"let {result} = if {layout}.size() != 0\n{{\nlet ptr = alloc::alloc({layout});\n",
14181411
));
14191412
self.push_str(&format!(
1420-
"if ptr.is_null()\n{{\nextern crate alloc;\nalloc::alloc::handle_alloc_error({layout});\n}}\nptr\n}}",
1413+
"if ptr.is_null()\n{{\nalloc::handle_alloc_error({layout});\n}}\nptr\n}}",
14211414
));
14221415
self.push_str(&format!("else {{\ncore::ptr::null_mut()\n}};\n",));
14231416
self.push_str(&format!("for (i, e) in {vec}.into_iter().enumerate() {{\n",));
@@ -1453,9 +1446,8 @@ impl Bindgen for FunctionBindgen<'_> {
14531446
"let {len} = {operand1};\n",
14541447
operand1 = operands[1]
14551448
));
1456-
self.push_str("extern crate alloc;\n");
14571449
self.push_str(&format!(
1458-
"let mut {result} = alloc::vec::Vec::with_capacity({len} as usize);\n",
1450+
"let mut {result} = Vec::with_capacity({len} as usize);\n",
14591451
));
14601452

14611453
self.push_str("for i in 0..");

crates/gen-rust-lib/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ pub trait RustGenerator {
213213
TypeMode::AllBorrowed(lt) | TypeMode::LeafBorrowed(lt) => {
214214
self.print_borrowed_str(lt)
215215
}
216-
TypeMode::Owned | TypeMode::HandlesBorrowed(_) => {
217-
self.push_str("alloc::string::String")
218-
}
216+
TypeMode::Owned | TypeMode::HandlesBorrowed(_) => self.push_str("String"),
219217
},
220218
}
221219
}
@@ -337,13 +335,13 @@ pub trait RustGenerator {
337335
if iface.all_bits_valid(ty) {
338336
self.print_borrowed_slice(iface, false, ty, lt);
339337
} else {
340-
self.push_str("alloc::vec::Vec<");
338+
self.push_str("Vec<");
341339
self.print_ty(iface, ty, mode);
342340
self.push_str(">");
343341
}
344342
}
345343
TypeMode::HandlesBorrowed(_) | TypeMode::Owned => {
346-
self.push_str("alloc::vec::Vec<");
344+
self.push_str("Vec<");
347345
self.print_ty(iface, ty, mode);
348346
self.push_str(">");
349347
}

crates/guest-rust/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#![no_std]
22

3-
extern crate alloc;
3+
extern crate alloc as liballoc;
44

5-
use alloc::boxed::Box;
65
use core::fmt;
76
use core::marker;
87
use core::mem;
98
use core::ops::Deref;
9+
use liballoc::boxed::Box;
1010

1111
#[cfg(feature = "macros")]
1212
pub use wit_bindgen_guest_rust_macro::{export, import};
@@ -15,6 +15,9 @@ pub use wit_bindgen_guest_rust_macro::{export, import};
1515
#[doc(hidden)]
1616
pub use bitflags;
1717

18+
// Re-export things from liballoc for convenient use.
19+
pub use liballoc::{alloc, string, vec};
20+
1821
/// A type for handles to resources that appear in exported functions.
1922
///
2023
/// This type is used as `Handle<T>` for argument types and return values of
@@ -128,7 +131,7 @@ pub unsafe trait LocalHandle: HandleType {
128131

129132
#[doc(hidden)]
130133
pub mod rt {
131-
use ::alloc::alloc::{self, Layout};
134+
use super::alloc::{self, Layout};
132135

133136
#[no_mangle]
134137
unsafe extern "C" fn cabi_realloc(

0 commit comments

Comments
 (0)