Skip to content

Commit 2bd04ee

Browse files
committed
Implement feedback
1 parent 9c9d847 commit 2bd04ee

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

.zed/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"exercises/4-multitasking/3-asynchronous-multitasking/1-async-channels/Cargo.toml",
2929
"exercises/4-multitasking/3-asynchronous-multitasking/2-async-chat/Cargo.toml",
3030
"exercises/5-rust-for-web/1-rust-for-web-servers/1-lettuce-crop/Cargo.toml",
31+
"exercises/5-rust-for-web/3-rust-in-the-browser/1-lettuce-crop-wasm/Cargo.toml",
3132
"exercises/6-rust-for-systems-programming/1-foreign-function-interface/1-crc-in-c/Cargo.toml",
3233
"exercises/6-rust-for-systems-programming/1-foreign-function-interface/2-crc-in-rust/Cargo.toml",
3334
"exercises/6-rust-for-systems-programming/1-foreign-function-interface/3-tweetnacl-bindgen/Cargo.toml",

book/src/rust-in-the-browser.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ In exercise 5.1.1, we build a web server that hosts an image cropping service. B
55

66
In this exercise, we will create a new version of our Lettuce Crop website that crops images with [WebAssembly](https://webassembly.org/). WebAssembly allows you to run compiled code in a safe sandboxed environment in the browser. This means we will not need a dedicated server anymore, as the website will only consist of static files which we can be hosted using any HTTP server. You could even host it for free using [GitHub pages](https://pages.github.com/)!
77

8-
### 3.2.1.A Building with Wasm Pack
8+
### 5.3.1.A Building with Wasm Pack
99
In `exercises/5-rust-for-web/3-rust-in-the-browser/1-lettuce-crop-wasm` we have set up a basic WebAssembly project. As you can see in the `Cargo.toml`, the project has been configured as a dynamic library (`"cdylib"`). We've also added the `wasm-bindgen` crate as a dependency, which is used to generate WebAssembly bindings.
1010

1111
To build the project, we will use `wasm-pack`. First, install `wasm-pack` with:
@@ -22,7 +22,7 @@ Now, a bunch of files should appear in the `assets/pkg` folder:
2222
- Some `.d.ts` files, which describe the TypeScript types of the generated bindings
2323
- A `.js` files, which contains the JavaScript bindings for our WebAssembly binary
2424

25-
### 3.2.1.B Interacting with JavaScript
25+
### 5.3.1.B Interacting with JavaScript
2626
So what functionality does the compiled WebAssembly currently include? In `lib.rs` you can see two functions: an extern `alert()` function, and a `hello()` function. Both of these functions have been annotated with `#[wasm_bindgen]` to indicate that we want to bind them with WebAssembly. Extern functions will be bound to existing JavaScript methods, in this case the [window's `alert()` function](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert) which shows a popup dialog.
2727

2828
Let's add the WebAssembly to our website. Add the following JavaScript in the `<body>` of the `index.html` to load the WebAssembly binary and call our `hello()` function when we press the submit button:
@@ -40,7 +40,7 @@ Let's add the WebAssembly to our website. Add the following JavaScript in the `<
4040

4141
To try out the website, you can use any HTTP server that is able to serve local files. You could use `axum` to host the files like we did in exercise 5.1.1, but you can also use for example `npx http-server` if you have `npm` installed.
4242

43-
### 3.2.1.C Cropping images
43+
### 5.3.1.C Cropping images
4444
Let's add a `crop_image(bytes: Vec<u8>, max_size: u32) -> Vec<u8>` function to our Rust library that will crop our images. You can use the same logic as in exercise 5.1.1 (part D and E) to create a `DynamicImage` from the input bytes, crop it, and export it as WebP. Mark the function with `#[wasm_bindgen]` and rebuild the library to generate WebAssembly bindings for it.
4545

4646
If you look at the generated JavaScript bindings, you will see that the `Vec<u8>`s for the `crop_image` function have been turned into `Uint8Array`s. We will need to write some JavaScript to read the user's selected image and give it to our `crop_image` as a `Uint8Array`.
@@ -67,7 +67,7 @@ window.location.href = URL.createObjectURL(new Blob([cropped_bytes]));
6767
```
6868
If you select an invalid file, you will get an error in the browser console. Feel free to add some better error handling by using a [try-catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch), and by validating whether `image.files[0]` exists before reading it. It would also be nice to verify that `max_size` has a sensible value.
6969

70-
### 3.2.1.D Using the web-sys crate (bonus)
70+
### 5.3.1.D Using the web-sys crate (bonus)
7171
Instead of using JavaScript to interact with the HTML document or manually binding extern JavaScript functions using `#[wasm_bindgen]` like we saw with `alert()`, we can also use the [`web-sys`](https://crates.io/crates/web-sys) crate. This crate provides bindings for the JavaScript web APIs available in the browser. However, most of these APIs have to be manually enabled with individual features.
7272

7373
Add the `web-sys` crate to your project with all the needed features enabled:

0 commit comments

Comments
 (0)