Skip to content

Commit 7ea483e

Browse files
Update rust guide and tutorial examples to use registries for WITs
Signed-off-by: Kate Goldenring <[email protected]>
1 parent 33c3ac7 commit 7ea483e

File tree

6 files changed

+117
-100
lines changed

6 files changed

+117
-100
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is automatically generated by cargo-component.
2+
# It is not intended for manual editing.
3+
version = 1
4+
5+
[[package]]
6+
name = "docs:adder"
7+
registry = "ghcr.io/bytecodealliance"
8+
9+
[[package.version]]
10+
requirement = "^0.1.0"
11+
version = "0.1.0"
12+
digest = "sha256:d4cb950366f8756821c401ec496ad43b35ffb0df3b2e757eb6aa3c1fa30d38c2"

component-model/examples/tutorial/adder/Cargo.toml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ wit-bindgen-rt = { version = "0.37.0", features = ["bitflags"] }
99
[lib]
1010
crate-type = ["cdylib"]
1111

12-
[package.metadata.component]
12+
# Tell cargo-component where to find the package for the target world
13+
# The following tells cargo-component to fetch the WIT from ghcr.io/bytecodealliance/docs/adder:0.1.0
14+
[package.metadata.component.target]
15+
# The registry which contains the package
16+
registry = "ghcr.io/bytecodealliance"
17+
# The package name
1318
package = "docs:adder"
19+
# The package version
20+
version = "0.1.0"
1421

15-
[package.metadata.component.dependencies]
16-
17-
[package.metadata.component.target]
18-
path = "../wit/adder"
19-
world = "adder"
22+
# # To instead use a WIT on the local filesystem, uncomment the following lines
23+
# [package.metadata.component.target]
24+
# # Path to the directory containing the WIT
25+
# path = "../wit/adder"

component-model/examples/tutorial/adder/src/bindings.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ mod _rt {
122122
/// ```
123123
#[allow(unused_macros)]
124124
#[doc(hidden)]
125-
macro_rules! __export_adder_impl {
125+
macro_rules! __export_root_impl {
126126
($ty:ident) => {
127127
self::export!($ty with_types_in self);
128128
};
@@ -133,19 +133,19 @@ macro_rules! __export_adder_impl {
133133
};
134134
}
135135
#[doc(inline)]
136-
pub(crate) use __export_adder_impl as export;
136+
pub(crate) use __export_root_impl as export;
137137
#[cfg(target_arch = "wasm32")]
138138
#[unsafe(
139-
link_section = "component-type:wit-bindgen:0.41.0:docs:[email protected]:adder:encoded world"
139+
link_section = "component-type:wit-bindgen:0.41.0:root:component:root:encoded world"
140140
)]
141141
#[doc(hidden)]
142142
#[allow(clippy::octal_escapes)]
143-
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 203] = *b"\
144-
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07P\x01A\x02\x01A\x02\x01\
143+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 199] = *b"\
144+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07M\x01A\x02\x01A\x02\x01\
145145
B\x02\x01@\x02\x01xy\x01yy\0y\x04\0\x03add\x01\0\x04\0\x14docs:adder/[email protected]\x05\
146-
\0\x04\0\x16docs:adder/[email protected]\x04\0\x0b\x0b\x01\0\x05adder\x03\0\0\0G\x09pr\
147-
oducers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\x06\
148-
0.41.0";
146+
\0\x04\0\x13root:component/root\x04\0\x0b\x0a\x01\0\x04root\x03\0\0\0G\x09produc\
147+
ers\x01\x0cprocessed-by\x02\x0dwit-component\x070.227.1\x10wit-bindgen-rust\x060\
148+
.41.0";
149149
#[inline(never)]
150150
#[doc(hidden)]
151151
pub fn __link_custom_section_describing_imports() {

component-model/examples/tutorial/calculator/Cargo.toml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
99
[lib]
1010
crate-type = ["cdylib"]
1111

12-
[package.metadata.component]
13-
package = "docs:calculator"
14-
15-
[package.metadata.component.target.dependencies]
16-
"docs:adder" = { path = "../wit/adder" } # directory containing the WIT package
17-
12+
# Tell cargo-component where to find the package for the target world
13+
# The following tells cargo-component to fetch the WIT from a local directory
1814
[package.metadata.component.target]
15+
# Path to the directory containing the WIT
1916
path = "../wit/calculator"
17+
# World within the package to target
2018
world = "calculator"
2119

22-
[package.metadata.component.dependencies]
20+
[package.metadata.component.target.dependencies]
21+
# Tell cargo-component where to find the package for the dependencies
22+
"docs:adder" = { path = "../wit/adder" }
23+
24+
## To instead fetch the package and it's dependencies from a registry, uncomment the following lines
25+
# [package.metadata.component.target]
26+
# package = "docs:calculator"
27+
# version = "0.1.0"
28+
# registry = "ghcr.io/bytecodealliance"
29+
# world = "calculator"
Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,177 @@
1-
// Generated by `wit-bindgen` 0.25.0. DO NOT EDIT!
1+
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
22
// Options used:
3-
#[allow(dead_code)]
3+
// * runtime_path: "wit_bindgen_rt"
4+
#[rustfmt::skip]
5+
#[allow(dead_code, clippy::all)]
46
pub mod docs {
5-
#[allow(dead_code)]
67
pub mod adder {
7-
#[allow(dead_code, clippy::all)]
8+
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
89
pub mod add {
910
#[used]
1011
#[doc(hidden)]
11-
#[cfg(target_arch = "wasm32")]
12-
static __FORCE_SECTION_REF: fn() =
13-
super::super::super::__link_custom_section_describing_imports;
12+
static __FORCE_SECTION_REF: fn() = super::super::super::__link_custom_section_describing_imports;
1413
use super::super::super::_rt;
1514
#[allow(unused_unsafe, clippy::all)]
1615
pub fn add(x: u32, y: u32) -> u32 {
1716
unsafe {
1817
#[cfg(target_arch = "wasm32")]
1918
#[link(wasm_import_module = "docs:adder/[email protected]")]
20-
extern "C" {
19+
unsafe extern "C" {
2120
#[link_name = "add"]
22-
fn wit_import(_: i32, _: i32) -> i32;
21+
fn wit_import0(_: i32, _: i32) -> i32;
2322
}
24-
2523
#[cfg(not(target_arch = "wasm32"))]
26-
fn wit_import(_: i32, _: i32) -> i32 {
24+
unsafe extern "C" fn wit_import0(_: i32, _: i32) -> i32 {
2725
unreachable!()
2826
}
29-
let ret = wit_import(_rt::as_i32(&a), _rt::as_i32(&b));
27+
let ret = unsafe { wit_import0(_rt::as_i32(&x), _rt::as_i32(&y)) };
3028
ret as u32
3129
}
3230
}
3331
}
3432
}
3533
}
36-
#[allow(dead_code)]
34+
#[rustfmt::skip]
35+
#[allow(dead_code, clippy::all)]
3736
pub mod exports {
38-
#[allow(dead_code)]
3937
pub mod docs {
40-
#[allow(dead_code)]
4138
pub mod calculator {
42-
#[allow(dead_code, clippy::all)]
39+
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
4340
pub mod calculate {
4441
#[used]
4542
#[doc(hidden)]
46-
#[cfg(target_arch = "wasm32")]
47-
static __FORCE_SECTION_REF: fn() =
48-
super::super::super::super::__link_custom_section_describing_imports;
43+
static __FORCE_SECTION_REF: fn() = super::super::super::super::__link_custom_section_describing_imports;
4944
use super::super::super::super::_rt;
5045
#[repr(u8)]
51-
#[derive(Clone, Copy, Eq, PartialEq)]
46+
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
5247
pub enum Op {
5348
Add,
5449
}
5550
impl ::core::fmt::Debug for Op {
56-
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
51+
fn fmt(
52+
&self,
53+
f: &mut ::core::fmt::Formatter<'_>,
54+
) -> ::core::fmt::Result {
5755
match self {
5856
Op::Add => f.debug_tuple("Op::Add").finish(),
5957
}
6058
}
6159
}
62-
6360
impl Op {
6461
#[doc(hidden)]
6562
pub unsafe fn _lift(val: u8) -> Op {
6663
if !cfg!(debug_assertions) {
6764
return ::core::mem::transmute(val);
6865
}
69-
7066
match val {
7167
0 => Op::Add,
72-
7368
_ => panic!("invalid enum discriminant"),
7469
}
7570
}
7671
}
77-
7872
#[doc(hidden)]
7973
#[allow(non_snake_case)]
8074
pub unsafe fn _export_eval_expression_cabi<T: Guest>(
8175
arg0: i32,
8276
arg1: i32,
8377
arg2: i32,
8478
) -> i32 {
85-
#[cfg(target_arch = "wasm32")]
86-
_rt::run_ctors_once();
87-
let result0 =
88-
T::eval_expression(Op::_lift(arg0 as u8), arg1 as u32, arg2 as u32);
79+
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
80+
let result0 = T::eval_expression(
81+
Op::_lift(arg0 as u8),
82+
arg1 as u32,
83+
arg2 as u32,
84+
);
8985
_rt::as_i32(result0)
9086
}
9187
pub trait Guest {
9288
fn eval_expression(op: Op, x: u32, y: u32) -> u32;
9389
}
9490
#[doc(hidden)]
95-
96-
macro_rules! __export_docs_calculator_calculate_0_1_0_cabi{
97-
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {
98-
99-
#[export_name = "docs:calculator/[email protected]#eval-expression"]
100-
unsafe extern "C" fn export_eval_expression(arg0: i32,arg1: i32,arg2: i32,) -> i32 {
101-
$($path_to_types)*::_export_eval_expression_cabi::<$ty>(arg0, arg1, arg2)
102-
}
103-
};);
104-
}
91+
macro_rules! __export_docs_calculator_calculate_0_1_0_cabi {
92+
($ty:ident with_types_in $($path_to_types:tt)*) => {
93+
const _ : () = { #[unsafe (export_name =
94+
"docs:calculator/[email protected]#eval-expression")] unsafe extern
95+
"C" fn export_eval_expression(arg0 : i32, arg1 : i32, arg2 :
96+
i32,) -> i32 { unsafe { $($path_to_types)*::
97+
_export_eval_expression_cabi::<$ty > (arg0, arg1, arg2) } } };
98+
};
99+
}
105100
#[doc(hidden)]
106101
pub(crate) use __export_docs_calculator_calculate_0_1_0_cabi;
107102
}
108103
}
109104
}
110105
}
106+
#[rustfmt::skip]
111107
mod _rt {
112-
108+
#![allow(dead_code, clippy::all)]
113109
pub fn as_i32<T: AsI32>(t: T) -> i32 {
114110
t.as_i32()
115111
}
116-
117112
pub trait AsI32 {
118113
fn as_i32(self) -> i32;
119114
}
120-
121115
impl<'a, T: Copy + AsI32> AsI32 for &'a T {
122116
fn as_i32(self) -> i32 {
123117
(*self).as_i32()
124118
}
125119
}
126-
127120
impl AsI32 for i32 {
128121
#[inline]
129122
fn as_i32(self) -> i32 {
130123
self as i32
131124
}
132125
}
133-
134126
impl AsI32 for u32 {
135127
#[inline]
136128
fn as_i32(self) -> i32 {
137129
self as i32
138130
}
139131
}
140-
141132
impl AsI32 for i16 {
142133
#[inline]
143134
fn as_i32(self) -> i32 {
144135
self as i32
145136
}
146137
}
147-
148138
impl AsI32 for u16 {
149139
#[inline]
150140
fn as_i32(self) -> i32 {
151141
self as i32
152142
}
153143
}
154-
155144
impl AsI32 for i8 {
156145
#[inline]
157146
fn as_i32(self) -> i32 {
158147
self as i32
159148
}
160149
}
161-
162150
impl AsI32 for u8 {
163151
#[inline]
164152
fn as_i32(self) -> i32 {
165153
self as i32
166154
}
167155
}
168-
169156
impl AsI32 for char {
170157
#[inline]
171158
fn as_i32(self) -> i32 {
172159
self as i32
173160
}
174161
}
175-
176162
impl AsI32 for usize {
177163
#[inline]
178164
fn as_i32(self) -> i32 {
179165
self as i32
180166
}
181167
}
182-
183168
#[cfg(target_arch = "wasm32")]
184169
pub fn run_ctors_once() {
185170
wit_bindgen_rt::run_ctors_once();
186171
}
187172
}
188-
189-
/// Generates `#[no_mangle]` functions to export the specified type as the
190-
/// root implementation of all generated traits.
173+
/// Generates `#[unsafe(no_mangle)]` functions to export the specified type as
174+
/// the root implementation of all generated traits.
191175
///
192176
/// For more information see the documentation of `wit_bindgen::generate!`.
193177
///
@@ -204,31 +188,34 @@ mod _rt {
204188
/// ```
205189
#[allow(unused_macros)]
206190
#[doc(hidden)]
207-
208191
macro_rules! __export_calculator_impl {
209-
($ty:ident) => (self::export!($ty with_types_in self););
210-
($ty:ident with_types_in $($path_to_types_root:tt)*) => (
211-
$($path_to_types_root)*::exports::docs::calculator::calculate::__export_docs_calculator_calculate_0_1_0_cabi!($ty with_types_in $($path_to_types_root)*::exports::docs::calculator::calculate);
212-
)
192+
($ty:ident) => {
193+
self::export!($ty with_types_in self);
194+
};
195+
($ty:ident with_types_in $($path_to_types_root:tt)*) => {
196+
$($path_to_types_root)*::
197+
exports::docs::calculator::calculate::__export_docs_calculator_calculate_0_1_0_cabi!($ty
198+
with_types_in $($path_to_types_root)*:: exports::docs::calculator::calculate);
199+
};
213200
}
214201
#[doc(inline)]
215202
pub(crate) use __export_calculator_impl as export;
216-
217203
#[cfg(target_arch = "wasm32")]
218-
#[link_section = "component-type:wit-bindgen:0.25.0:calculator:encoded world"]
204+
#[unsafe(
205+
link_section = "component-type:wit-bindgen:0.41.0:docs:[email protected]:calculator:encoded world"
206+
)]
219207
#[doc(hidden)]
208+
#[allow(clippy::octal_escapes)]
220209
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 308] = *b"\
221210
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xb3\x01\x01A\x02\x01\
222-
A\x04\x01B\x02\x01@\x02\x01ay\x01by\0y\x04\0\x03add\x01\0\x03\x01\x14docs:adder/\
223-
[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01\
224-
xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\x01\x1fdocs:calculator/calculat\
225-
[email protected]\x05\x01\x04\x01\x20docs:calculator/[email protected]\x04\0\x0b\x10\x01\0\x0a\
226-
calculator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070\
227-
.208.1\x10wit-bindgen-rust\x060.25.0";
228-
211+
A\x04\x01B\x02\x01@\x02\x01xy\x01yy\0y\x04\0\x03add\x01\0\x03\0\x14docs:adder/ad\
212+
[email protected]\x05\0\x01B\x04\x01m\x01\x03add\x04\0\x02op\x03\0\0\x01@\x03\x02op\x01\x01\
213+
xy\x01yy\0y\x04\0\x0feval-expression\x01\x02\x04\0\x1fdocs:calculator/calculate@\
214+
0.1.0\x05\x01\x04\0\x20docs:calculator/[email protected]\x04\0\x0b\x10\x01\0\x0ac\
215+
alculator\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\x070.\
216+
227.1\x10wit-bindgen-rust\x060.41.0";
229217
#[inline(never)]
230218
#[doc(hidden)]
231-
#[cfg(target_arch = "wasm32")]
232219
pub fn __link_custom_section_describing_imports() {
233220
wit_bindgen_rt::maybe_link_cabi_realloc();
234221
}

0 commit comments

Comments
 (0)