Skip to content

Commit

Permalink
Merge branch 'main' into new-6.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
squell authored Dec 18, 2024
2 parents 10e817d + 6be5ef1 commit f905573
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 14 deletions.
27 changes: 21 additions & 6 deletions book/src/foreign-function-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Steps:
[dependencies]
```

2. Expose an extern rust function
2. Expose an extern rust function in the `lib.rs`

```rust
#[no_mangle]
Expand All @@ -97,16 +97,16 @@ Steps:
3. Create a C header file `crc_in_rust.h`

```c
#include <inttypes.h> // uint32_t, uint8_t
#include <stdint.h> // uint32_t, uint8_t
#include <stddef.h> // size_t

uint32_t crc32(const uint8_t data[], size_t data_length);
```

4. Use the rust `crc32` function in C
4. Create `main.c` and use the rust `crc32` function

```c
#include <inttypes.h> // uint32_t, uint8_t
#include <stdint.h> // uint32_t, uint8_t
#include <stddef.h> // size_t
#include <stdio.h> // printf
#include "crc_in_rust.h"
Expand All @@ -117,20 +117,35 @@ Steps:

uint32_t hash = crc32(data, data_length);

printf("Hash: 0x%d\n", hash);
printf("Hash: %d\n", hash);

return 0;
}
```
5. Give the rust function the same signature as the one defined in the header file

5. Compile and run
6. Compile the rust crate and then run

Linux & MacOS:
```sh
# Build main.c, link it to the dynamic library and output the executable called main
$ clang main.c target/debug/libcrc_in_rust.so -omain
# Run the executable
$ ./main
Hash: -1386739207
```

Windows:
```ps
# Build main.c, link it to the import library of the DLL and output the executable called main.exe
❯ clang main.c .\target\debug\crc_in_rust.dll.lib -o "main.exe"
# Move the dll to the same folder as the exe so it can find it
❯ cp .\target\debug\crc_in_rust.dll crc_in_rust.dll
# Run the executable
❯ .\main.exe
Hash: -1386739207
```

## Exercise 6.1.3: QOI Bindgen
In this exercise, we will use `cargo bindgen` to generate the FFI bindings for a C library. Bindgen will look at a C header file, and generate Rust functions, types and constants based on the C definitions.

Expand Down
61 changes: 60 additions & 1 deletion book/src/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,67 @@ With CodeLLDB installed correctly, you can also start a debug session by clickin
Play a little with setting breakpoints by clicking on a line number, making a red circle appear and stepping over/into/out of functions using the controls.
You can view variable values by hovering over them while execution is paused, or by expanding the 'Local' view under 'Variables' in the left panel during a debug session.

# Instructions for FFI module
*This part is relevant only if you're partaking in one of the modules on Rust FFI.*

For doing FFI we will need to compile some C code and for that we need a C compiler installed.
We've chosen to use `clang` in our excercises.

The prerequisite is that calling `clang` in your terminal should work. If it doesn't, follow the instructions for your platform below.

## Linux

For the bookworms using a Debian-like:
```bash
sudo apt update
sudo apt install clang
```

If you're on Arch, btw:
```bash
sudo pacman -S clang
```

For those tipping their Fedora's:
```bash
sudo dnf install clang
```

## Windows

Always make sure to select the option to add the install to `path`.

Using winget:
```ps
winget install -i -e --id LLVM.LLVM
```

For the sweethearts using chocolatey:
```ps
choco install llvm
```

For the handsome people preferring manual installation:
- Go to the releases page: https://github.com/llvm/llvm-project/releases
- Go to a recent release
- Search for LLVM-[VERSION]-win64.exe and download it
- Run the exe

## MacOS

Using brew:
```bash
brew install llvm
```

Then also add to path, e.g.:
```bash
echo 'export PATH="$(brew --prefix llvm)/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
```

# Instructions for embedded
*This part is relevant only if you're partaking in one of the workshops on embedded Rust.*
*This part is relevant only if you're partaking in one of the modules on embedded Rust.*

## Hardware
We will use the [BBC micro:bit](https://microbit.org/buy/bbc-microbit-single) V2 and either you've already got it or we will bring it with us.
Expand Down
2 changes: 1 addition & 1 deletion slides/5_1-rust-for-web.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ layout: default

```rust
/// A very long type name warrants a type alias
type AppState = State<Arc<Mutex<Vec<String>>>>;
type AppState = Arc<Mutex<Vec<String>>>;

async fn handler(
Path(name): Path<String>,
Expand Down
13 changes: 7 additions & 6 deletions slides/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f905573

Please sign in to comment.