wit-bindgen-rust
Advanced tools
| { | ||
| "git": { | ||
| "sha1": "ba933bda98214ffe2ae16c63b2b91d0932021bbc" | ||
| "sha1": "6f8b8e75254174353fc27c269827c4c239d51eff" | ||
| }, | ||
| "path_in_vcs": "crates/rust" | ||
| } |
+10
-3
@@ -68,2 +68,8 @@ # This file is automatically @generated by Cargo. | ||
| [[package]] | ||
| name = "bytes" | ||
| version = "1.11.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" | ||
| [[package]] | ||
| name = "clap" | ||
@@ -477,5 +483,5 @@ version = "4.5.53" | ||
| name = "wit-bindgen-core" | ||
| version = "0.49.0" | ||
| version = "0.50.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "886e8e938e4e9fe54143c080cbb99d7db5d19242b62ef225dbb28e17b3223bd8" | ||
| checksum = "6ee98d5747ec63897e51101c8cafad569cc639346332c0378772ef4baeea8ed2" | ||
| dependencies = [ | ||
@@ -491,5 +497,6 @@ "anyhow", | ||
| name = "wit-bindgen-rust" | ||
| version = "0.49.0" | ||
| version = "0.50.0" | ||
| dependencies = [ | ||
| "anyhow", | ||
| "bytes", | ||
| "clap", | ||
@@ -496,0 +503,0 @@ "futures", |
+8
-4
@@ -13,6 +13,6 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| [package] | ||
| edition = "2021" | ||
| rust-version = "1.82.0" | ||
| edition = "2024" | ||
| rust-version = "1.87.0" | ||
| name = "wit-bindgen-rust" | ||
| version = "0.49.0" | ||
| version = "0.50.0" | ||
| authors = ["Alex Crichton <alex@alexcrichton.com>"] | ||
@@ -33,2 +33,3 @@ build = "build.rs" | ||
| repository = "https://github.com/bytecodealliance/wit-bindgen" | ||
| resolver = "2" | ||
@@ -86,3 +87,3 @@ [features] | ||
| [dependencies.wit-bindgen-core] | ||
| version = "0.49.0" | ||
| version = "0.50.0" | ||
@@ -92,2 +93,5 @@ [dependencies.wit-component] | ||
| [dev-dependencies.bytes] | ||
| version = "1" | ||
| [dev-dependencies.futures] | ||
@@ -94,0 +98,0 @@ version = "0.3.31" |
+10
-6
| use crate::{ | ||
| classify_constructor_return_type, int_repr, to_rust_ident, ConstructorReturnType, Identifier, | ||
| InterfaceGenerator, RustFlagsRepr, | ||
| ConstructorReturnType, Identifier, InterfaceGenerator, RustFlagsRepr, | ||
| classify_constructor_return_type, int_repr, to_rust_ident, | ||
| }; | ||
@@ -9,3 +9,3 @@ use heck::*; | ||
| use wit_bindgen_core::abi::{Bindgen, Instruction, LiftLower, WasmType}; | ||
| use wit_bindgen_core::{dealias, uwrite, uwriteln, wit_parser::*, Source}; | ||
| use wit_bindgen_core::{Source, dealias, uwrite, uwriteln, wit_parser::*}; | ||
@@ -674,2 +674,3 @@ pub(super) struct FunctionBindgen<'a, 'b> { | ||
| let len = format!("len{}", tmp); | ||
| let vec = self.r#gen.path_to_vec(); | ||
| if realloc.is_none() { | ||
@@ -679,3 +680,6 @@ self.push_str(&format!("let {} = {};\n", val, operands[0])); | ||
| let op0 = operands.pop().unwrap(); | ||
| self.push_str(&format!("let {} = ({}).into_boxed_slice();\n", val, op0)); | ||
| self.push_str(&format!( | ||
| "let {} = <_ as Into<{vec}<_>>>::into({}).into_boxed_slice();\n", | ||
| val, op0 | ||
| )); | ||
| } | ||
@@ -697,3 +701,3 @@ self.push_str(&format!("let {} = {}.as_ptr().cast::<u8>();\n", ptr, val)); | ||
| let result = format!( | ||
| "{vec}::from_raw_parts({}.cast(), {1}, {1})", | ||
| "<_ as From<{vec}<_>>>::from({vec}::from_raw_parts({}.cast(), {1}, {1}))", | ||
| operands[0], len | ||
@@ -743,3 +747,3 @@ ); | ||
| let alloc = self.r#gen.path_to_std_alloc_module(); | ||
| let rt = self.gen.gen.runtime_path().to_string(); | ||
| let rt = self.r#gen.r#gen.runtime_path().to_string(); | ||
| let body = self.blocks.pop().unwrap(); | ||
@@ -746,0 +750,0 @@ let tmp = self.tmp(); |
+35
-7
| use crate::interface::InterfaceGenerator; | ||
| use anyhow::{bail, Result}; | ||
| use anyhow::{Result, bail}; | ||
| use core::panic; | ||
@@ -9,7 +9,8 @@ use heck::*; | ||
| use std::mem; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::str::FromStr; | ||
| use wit_bindgen_core::abi::{Bitcast, WasmType}; | ||
| use wit_bindgen_core::{ | ||
| dealias, name_package_module, uwrite, uwriteln, wit_parser::*, AsyncFilterSet, Files, | ||
| InterfaceGenerator as _, Source, Types, WorldGenerator, | ||
| AsyncFilterSet, Files, InterfaceGenerator as _, Source, Types, WorldGenerator, dealias, | ||
| name_package_module, uwrite, uwriteln, wit_parser::*, | ||
| }; | ||
@@ -30,3 +31,3 @@ | ||
| #[derive(Default)] | ||
| struct RustWasm { | ||
| pub struct RustWasm { | ||
| types: Types, | ||
@@ -282,7 +283,7 @@ src_preamble: Source, | ||
| impl Opts { | ||
| pub fn build(self) -> Box<dyn WorldGenerator> { | ||
| pub fn build(self) -> RustWasm { | ||
| let mut r = RustWasm::new(); | ||
| r.skip = self.skip.iter().cloned().collect(); | ||
| r.opts = self; | ||
| Box::new(r) | ||
| r | ||
| } | ||
@@ -292,2 +293,28 @@ } | ||
| impl RustWasm { | ||
| /// Generates Rust bindings from the `wit/` directory and writes | ||
| /// the result into Cargo’s `OUT_DIR`. Intended for use in `build.rs`. | ||
| /// | ||
| /// The `world` parameter specifies the world name to select. | ||
| /// It must be provided unless the main package contains exactly one world. | ||
| /// | ||
| /// Returns the full path to the generated bindings file. | ||
| pub fn generate_to_out_dir(mut self, world: Option<&str>) -> Result<PathBuf> { | ||
| let mut resolve = Resolve::default(); | ||
| println!("cargo:rerun-if-changed=wit/"); | ||
| let (pkg, _files) = resolve.push_path("wit")?; | ||
| let main_packages = vec![pkg]; | ||
| let world = resolve.select_world(&main_packages, world)?; | ||
| let mut files = Files::default(); | ||
| self.generate(&resolve, world, &mut files)?; | ||
| let out_dir = std::env::var("OUT_DIR").expect("cargo sets OUT_DIR"); | ||
| let (name, contents) = files | ||
| .iter() | ||
| .next() | ||
| .expect("exactly one file should be generated"); | ||
| let dst = Path::new(&out_dir).join(name); | ||
| std::fs::write(&dst, contents)?; | ||
| Ok(dst) | ||
| } | ||
| fn new() -> RustWasm { | ||
@@ -439,3 +466,4 @@ RustWasm::default() | ||
| self.src.push_str("mod _rt {\n"); | ||
| self.src.push_str("#![allow(dead_code, clippy::all)]\n"); | ||
| self.src | ||
| .push_str("#![allow(dead_code, unused_imports, clippy::all)]\n"); | ||
| let mut emitted = IndexSet::new(); | ||
@@ -442,0 +470,0 @@ while !self.rt_module.is_empty() { |
+137
-0
@@ -34,1 +34,138 @@ #![allow(unused_macros)] | ||
| } | ||
| #[allow(unused)] | ||
| mod newtyped_list { | ||
| use std::ops::Deref; | ||
| wit_bindgen::generate!({ | ||
| inline: r#" | ||
| package test:newtyped-list; | ||
| interface byte { | ||
| type typed-list-of-byte = list<u8>; | ||
| type newtyped-list-of-byte = list<u8>; | ||
| record rec-of-lists { | ||
| l: list<u8>, | ||
| tl: typed-list-of-byte, | ||
| nl: newtyped-list-of-byte, | ||
| } | ||
| use-list-of-byte: func(l: list<u8>) -> list<u8>; | ||
| use-typed-list-of-byte: func(tl: typed-list-of-byte) -> typed-list-of-byte; | ||
| use-newtyped-list-of-byte: func(nl: newtyped-list-of-byte) -> newtyped-list-of-byte; | ||
| use-rec-of-lists: func(t: rec-of-lists) -> rec-of-lists; | ||
| } | ||
| interface noncopy-byte { | ||
| // this will be new-typed by a non-copy struct | ||
| type noncopy-byte = u8; | ||
| type newtyped-list-of-noncopy-byte = list<noncopy-byte>; | ||
| type typed-list-of-noncopy-byte = list<noncopy-byte>; | ||
| record rec-of-lists-of-noncopy-byte { | ||
| ntl: newtyped-list-of-noncopy-byte, | ||
| tl: typed-list-of-noncopy-byte, | ||
| l: list<noncopy-byte>, | ||
| } | ||
| use-newtyped-list-of-noncopy-byte: func(nl: newtyped-list-of-noncopy-byte) -> newtyped-list-of-noncopy-byte; | ||
| use-typed-list-of-noncopy-byte: func(tl: typed-list-of-noncopy-byte) -> typed-list-of-noncopy-byte; | ||
| use-list-of-noncopy-byte: func(l: list<noncopy-byte>) -> list<noncopy-byte>; | ||
| use-rec-of-lists-of-noncopy-byte: func(t: rec-of-lists-of-noncopy-byte) -> rec-of-lists-of-noncopy-byte; | ||
| } | ||
| world test { | ||
| import byte; | ||
| export byte; | ||
| import noncopy-byte; | ||
| export noncopy-byte; | ||
| } | ||
| "#, | ||
| with: { | ||
| "test:newtyped-list/byte/newtyped-list-of-byte": crate::newtyped_list::NewtypedListOfByte, | ||
| "test:newtyped-list/noncopy-byte/noncopy-byte": crate::newtyped_list::NoncopyByte, | ||
| "test:newtyped-list/noncopy-byte/newtyped-list-of-noncopy-byte": crate::newtyped_list::NewtypedListofNoncopyByte, | ||
| } | ||
| }); | ||
| #[derive(Debug, Clone)] | ||
| pub struct NewtypedListOfByte(Vec<u8>); | ||
| impl From<Vec<u8>> for NewtypedListOfByte { | ||
| fn from(value: Vec<u8>) -> Self { | ||
| NewtypedListOfByte(value) | ||
| } | ||
| } | ||
| impl From<NewtypedListOfByte> for Vec<u8> { | ||
| fn from(value: NewtypedListOfByte) -> Self { | ||
| value.0 | ||
| } | ||
| } | ||
| impl Deref for NewtypedListOfByte { | ||
| type Target = Vec<u8>; | ||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
| #[derive(Debug, Clone)] | ||
| pub struct NoncopyByte(u8); | ||
| #[derive(Debug, Clone)] | ||
| pub struct NewtypedListofNoncopyByte(Vec<NoncopyByte>); | ||
| impl From<Vec<NoncopyByte>> for NewtypedListofNoncopyByte { | ||
| fn from(value: Vec<NoncopyByte>) -> Self { | ||
| NewtypedListofNoncopyByte(value) | ||
| } | ||
| } | ||
| impl From<NewtypedListofNoncopyByte> for Vec<NoncopyByte> { | ||
| fn from(value: NewtypedListofNoncopyByte) -> Self { | ||
| value.0 | ||
| } | ||
| } | ||
| impl Deref for NewtypedListofNoncopyByte { | ||
| type Target = Vec<NoncopyByte>; | ||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
| } | ||
| #[allow(unused)] | ||
| mod retyped_list { | ||
| use std::ops::Deref; | ||
| wit_bindgen::generate!({ | ||
| inline: r#" | ||
| package test:retyped-list; | ||
| interface bytes { | ||
| // this will be the `Bytes` type from the bytes crate | ||
| type retyped-list-as-bytes = list<u8>; | ||
| record rec-bytes { | ||
| rl: retyped-list-as-bytes, | ||
| } | ||
| use-retyped-list-as-bytes: func(ri: retyped-list-as-bytes) -> retyped-list-as-bytes; | ||
| use-rec-of-retyped-list-as-bytes: func(rl: retyped-list-as-bytes) -> retyped-list-as-bytes; | ||
| } | ||
| world test { | ||
| import bytes; | ||
| export bytes; | ||
| } | ||
| "#, | ||
| with: { | ||
| "test:retyped-list/bytes/retyped-list-as-bytes": bytes::Bytes, | ||
| } | ||
| }); | ||
| } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display