Skip to content

Commit 40b3c38

Browse files
committed
Add a code example to the wac-graph crate.
1 parent 453c6cb commit 40b3c38

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

crates/wac-graph/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
11
//! A library for defining, encoding, and decoding WebAssembly composition graphs.
2+
//!
3+
//! An example of composing two components together using a `CompositionGraph`:
4+
//!
5+
//! ```rust,no_run
6+
//! use wac_graph::{CompositionGraph, EncodeOptions, types::Package};
7+
//!
8+
//! # fn main() -> anyhow::Result<()> {
9+
//! let mut graph = CompositionGraph::new();
10+
//!
11+
//! // Register the packages with the graph
12+
//! // It is assumed that `my:package1` exports a function named `a`,
13+
//! // while `my:package2` imports a function named `b`.
14+
//! let package1 = graph.register_package(
15+
//! Package::from_file("my:package1", None, "package1.wasm")?
16+
//! )?;
17+
//! let package2 = graph.register_package(
18+
//! Package::from_file("my:package2", None, "package2.wasm")?
19+
//! )?;
20+
//!
21+
//! // Instantiate package `my:package1`
22+
//! let instantiation1 = graph.instantiate(package1)?;
23+
//!
24+
//! // Alias the `a` export of the `my:package1` instance
25+
//! let a = graph.alias_instance_export(instantiation1, "a")?;
26+
//!
27+
//! // Instantiate package `my:package2`
28+
//! let instantiation2 = graph.instantiate(package2)?;
29+
//!
30+
//! // Connect `a` to the argument `b` of the instantiation
31+
//! graph.connect_argument(a, instantiation2, "b")?;
32+
//!
33+
//! // Finally, encode the graph into a new component
34+
//! let bytes = graph.encode(EncodeOptions::default())?;
35+
//!
36+
//! # Ok(())
37+
//! # }
38+
//! ````
239
340
#![deny(missing_docs)]
441

0 commit comments

Comments
 (0)