Skip to content

Failing to compile on Rust #1511

@razorblade23

Description

@razorblade23

The problem

I am trying to compile the component which will be used in my SDK...

My SDK code is like this

pub use wit_bindgen;

pub mod rt {
    pub use wit_bindgen::rt::*;
}

#[macro_export]
macro_rules! register_plugin {
    ($plugin_type:ident) => {
        // THE FIX: 'extern crate' allows us to alias a dependency crate 
        // as a different name in the root of the current crate.
        // This makes '::wit_bindgen' point to 'hydrust_sdk' globally in this plugin.
        extern crate hydrust_sdk as wit_bindgen;

        // We call generate! through our alias
        wit_bindgen::wit_bindgen::generate!({
            inline: "
                package hydrust:protocol;
                interface types {
                    record stream-info { title: string, url: string }
                    enum error-code { network-error, invalid-url, other }
                }
                world site-provider {
                    use types.{stream-info, error-code};
                    export can-handle: func(url: string) -> bool;
                    export get-stream: func(url: string) -> result<stream-info, error-code>;
                }
            ",
            world: "site-provider",
            // We tell it the runtime is available via 'wit_bindgen'
            // (which now points to 'hydrust_sdk' via our extern crate alias)
            runtime_path: "wit_bindgen",
        });

        export!($plugin_type);
    };
}

This is my mock-provider

use hydrust_sdk::register_plugin;

register_plugin!(MockPlugin);

struct MockPlugin;

impl Guest for MockPlugin {
    fn can_handle(url: String) -> bool {
        url.contains("example.com")
    }

    fn get_stream(_url: String) -> Result<StreamInfo, ErrorCode> {
        Ok(StreamInfo {
            title: "Hydrust Mock Stream".to_string(),
            url: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8".to_string(),
        })
    }
}

Here is first 100 lines from expanded.rs generated by the cargo-expand just to see the problem....

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use hydrust_sdk::register_plugin;
#[doc(hidden)]
use ::hydrust_sdk::wit_bindgen as wit_bindgen;
pub type StreamInfo = hydrust::protocol::types::StreamInfo;
pub type ErrorCode = hydrust::protocol::types::ErrorCode;
#[doc(hidden)]
#[allow(non_snake_case, unused_unsafe)]
pub unsafe fn _export_can_handle_cabi<T: Guest>(arg0: *mut u8, arg1: usize) -> i32 {
    unsafe {
        let result1 = {
            let len0 = arg1;
            let bytes0 = _rt::Vec::from_raw_parts(arg0.cast(), len0, len0);
            T::can_handle(_rt::string_lift(bytes0))
        };
        match result1 {
            true => 1,
            false => 0,
        }
    }
}
#[doc(hidden)]
#[allow(non_snake_case, unused_unsafe)]
pub unsafe fn _export_get_stream_cabi<T: Guest>(arg0: *mut u8, arg1: usize) -> *mut u8 {
    unsafe {
        let result1 = {
            let len0 = arg1;
            let bytes0 = _rt::Vec::from_raw_parts(arg0.cast(), len0, len0);
            T::get_stream(_rt::string_lift(bytes0))
        };
        let ptr2 = (&raw mut _RET_AREA.0).cast::<u8>();
        match result1 {
            Ok(e) => {
                *ptr2.add(0).cast::<u8>() = (0i32) as u8;
                let hydrust::protocol::types::StreamInfo { title: title3, url: url3 } = e;
                let vec4 = (title3.into_bytes()).into_boxed_slice();
                let ptr4 = vec4.as_ptr().cast::<u8>();
                let len4 = vec4.len();
                ::core::mem::forget(vec4);
                *ptr2.add(2 * ::core::mem::size_of::<*const u8>()).cast::<usize>() = len4;
                *ptr2.add(::core::mem::size_of::<*const u8>()).cast::<*mut u8>() = ptr4
                    .cast_mut();
                let vec5 = (url3.into_bytes()).into_boxed_slice();
                let ptr5 = vec5.as_ptr().cast::<u8>();
                let len5 = vec5.len();
                ::core::mem::forget(vec5);
                *ptr2.add(4 * ::core::mem::size_of::<*const u8>()).cast::<usize>() = len5;
                *ptr2.add(3 * ::core::mem::size_of::<*const u8>()).cast::<*mut u8>() = ptr5
                    .cast_mut();
            }
            Err(e) => {
                *ptr2.add(0).cast::<u8>() = (1i32) as u8;
                *ptr2.add(::core::mem::size_of::<*const u8>()).cast::<u8>() = (e.clone()
                    as i32) as u8;
            }
        };
        ptr2
    }
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn __post_return_get_stream<T: Guest>(arg0: *mut u8) {
    unsafe {
        let l0 = i32::from(*arg0.add(0).cast::<u8>());
        match l0 {
            0 => {
                let l1 = *arg0
                    .add(::core::mem::size_of::<*const u8>())
                    .cast::<*mut u8>();
                let l2 = *arg0
                    .add(2 * ::core::mem::size_of::<*const u8>())
                    .cast::<usize>();
                _rt::cabi_dealloc(l1, l2, 1);
                let l3 = *arg0
                    .add(3 * ::core::mem::size_of::<*const u8>())
                    .cast::<*mut u8>();
                let l4 = *arg0
                    .add(4 * ::core::mem::size_of::<*const u8>())
                    .cast::<usize>();
                _rt::cabi_dealloc(l3, l4, 1);
            }
            _ => {}
        }
    }
}
pub trait Guest {
    #[allow(async_fn_in_trait)]
    fn can_handle(url: _rt::String) -> bool;
    #[allow(async_fn_in_trait)]
    fn get_stream(url: _rt::String) -> Result<StreamInfo, ErrorCode>;
}
#[doc(hidden)]
pub(crate) use __export_world_site_provider_cabi;
#[repr(align(8))]
struct _RetArea([::core::mem::MaybeUninit<u8>; 5 * ::core::mem::size_of::<*const u8>()]);
static mut _RET_AREA: _RetArea = _RetArea(
    [::core::mem::MaybeUninit::uninit(); 5 * ::core::mem::size_of::<*const u8>()],
);

This is the message i get from compiler

vscode ➜ /workspaces/Hydrust/plugins/mock-provider (master) $ cargo component build --release
   Compiling proc-macro2 v1.0.106
   Compiling unicode-ident v1.0.22
   Compiling quote v1.0.44
   Compiling siphasher v1.0.1
   Compiling fastrand v2.3.0
   Compiling serde_core v1.0.228
   Compiling hashbrown v0.16.1
   Compiling foldhash v0.1.5
   Compiling equivalent v1.0.2
   Compiling bitflags v2.10.0
   Compiling semver v1.0.27
   Compiling anyhow v1.0.100
   Compiling smallvec v1.15.1
   Compiling zmij v1.0.16
   Compiling hashbrown v0.15.5
   Compiling phf_shared v0.13.1
   Compiling serde_json v1.0.149
   Compiling parking_lot_core v0.9.12
   Compiling new_debug_unreachable v1.0.6
   Compiling serde v1.0.228
   Compiling cfg-if v1.0.4
   Compiling prettyplease v0.2.37
   Compiling scopeguard v1.2.0
   Compiling phf_generator v0.13.1
   Compiling itoa v1.0.17
   Compiling memchr v2.7.6
   Compiling phf_codegen v0.13.1
   Compiling lock_api v0.4.14
   Compiling leb128fmt v0.1.0
   Compiling log v0.4.29
   Compiling id-arena v2.3.0
   Compiling unicode-xid v0.2.6
   Compiling precomputed-hash v0.1.1
   Compiling mac v0.1.1
   Compiling heck v0.5.0
   Compiling wit-bindgen-rust v0.51.0
   Compiling dtoa v1.0.11
   Compiling utf-8 v0.7.6
   Compiling futf v0.1.5
   Compiling parking_lot v0.12.5
   Compiling selectors v0.33.0
   Compiling tendril v0.4.3
   Compiling stable_deref_trait v1.2.1
   Compiling dtoa-short v0.3.5
   Compiling wit-bindgen-rust-macro v0.51.0
   Compiling string_cache v0.9.0
   Compiling servo_arc v0.4.3
   Compiling unicode-width v0.2.2
   Compiling rustc-hash v2.1.1
   Compiling syn v2.0.114
   Compiling string_cache_codegen v0.6.1
   Compiling wit-bindgen v0.51.0
   Compiling ego-tree v0.10.0
   Compiling getopts v0.2.24
   Compiling web_atoms v0.2.3
   Compiling indexmap v2.13.0
   Compiling wasmparser v0.244.0
   Compiling phf_macros v0.13.1
   Compiling serde_derive v1.0.228
   Compiling cssparser-macros v0.6.1
   Compiling derive_more-impl v2.1.1
   Compiling phf v0.13.1
   Compiling cssparser v0.36.0
   Compiling derive_more v2.1.1
   Compiling markup5ever v0.36.1
   Compiling html5ever v0.36.1
   Compiling scraper v0.25.0
   Compiling wit-parser v0.244.0
   Compiling wasm-encoder v0.244.0
   Compiling wasm-metadata v0.244.0
   Compiling wit-component v0.244.0
   Compiling wit-bindgen-core v0.51.0
   Compiling hydrust-sdk v0.1.0 (/workspaces/Hydrust/crates/hydrust-sdk)
   Compiling mock-provider v0.1.0 (/workspaces/Hydrust/plugins/mock-provider)
error[E0425]: cannot find function `maybe_link_cabi_realloc` in crate `wit_bindgen`
 --> src/lib.rs:4:1
  |
4 | register_plugin!(MockPlugin);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in `wit_bindgen`
  |
  = note: this error originates in the macro `wit_bindgen::generate` which comes from the expansion of the macro `register_plugin` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `wit_bindgen`
 --> src/lib.rs:4:1
  |
4 | register_plugin!(MockPlugin);
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of unresolved module or unlinked crate `wit_bindgen`
  |
  = help: if you wanted to use a crate named `wit_bindgen`, use `cargo add wit_bindgen` to add it to your `Cargo.toml`
  = note: this error originates in the macro `wit_bindgen::generate` which comes from the expansion of the macro `register_plugin` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0425, E0433.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `mock-provider` (lib) due to 2 previous errors

How do i fix this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions