MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / emit_runtime_item

Method emit_runtime_item

crates/rust/src/lib.rs:584–821  ·  view source on GitHub ↗
(&mut self, item: RuntimeItem)

Source from the content-addressed store, hash-verified

582 }
583
584 fn emit_runtime_item(&mut self, item: RuntimeItem) {
585 match item {
586 RuntimeItem::AllocCrate => {
587 uwriteln!(self.src, "extern crate alloc as alloc_crate;");
588 }
589 RuntimeItem::StdAllocModule => {
590 self.rt_module.insert(RuntimeItem::AllocCrate);
591 uwriteln!(self.src, "pub use alloc_crate::alloc;");
592 }
593 RuntimeItem::StringType => {
594 self.rt_module.insert(RuntimeItem::AllocCrate);
595 uwriteln!(self.src, "pub use alloc_crate::string::String;");
596 }
597 RuntimeItem::BoxType => {
598 self.rt_module.insert(RuntimeItem::AllocCrate);
599 uwriteln!(self.src, "pub use alloc_crate::boxed::Box;");
600 }
601 RuntimeItem::VecType => {
602 self.rt_module.insert(RuntimeItem::AllocCrate);
603 uwriteln!(self.src, "pub use alloc_crate::vec::Vec;");
604 }
605 RuntimeItem::CabiDealloc => {
606 self.rt_module.insert(RuntimeItem::StdAllocModule);
607 self.src.push_str(
608 "\
609pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) {
610 if size == 0 {
611 return;
612 }
613 unsafe {
614 let layout = alloc::Layout::from_size_align_unchecked(size, align);
615 alloc::dealloc(ptr, layout);
616 }
617}
618 ",
619 );
620 }
621
622 RuntimeItem::StringLift => {
623 self.rt_module.insert(RuntimeItem::StringType);
624 self.src.push_str(
625 "\
626pub unsafe fn string_lift(bytes: Vec<u8>) -> String {
627 if cfg!(debug_assertions) {
628 String::from_utf8(bytes).unwrap()
629 } else {
630 unsafe { String::from_utf8_unchecked(bytes) }
631 }
632}
633 ",
634 );
635 }
636
637 RuntimeItem::InvalidEnumDiscriminant => {
638 self.src.push_str(
639 "\
640pub unsafe fn invalid_enum_discriminant<T>() -> T {
641 if cfg!(debug_assertions) {

Callers 1

finish_runtime_moduleMethod · 0.80

Calls 5

runtime_pathMethod · 0.80
emit_runtime_as_traitMethod · 0.80
insertMethod · 0.45
push_strMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected