Calling Javascript From My 16bit VM | Live Rust Programming
Вставка
- Опубліковано 3 жов 2024
- It turns out that calling Javascript is complicated! I need to rework my system call interface, then find quite a few fun borrow checker quirks to make this hard. It turns out this is way more of a rabbit hole than I expected!
Stream date: 2024-06-25
Support the stream: ko-fi.com/tomm...
Source: github.com/phy...
Streamed live @ / tommarkstalkscode
Follow me at coding.tommark...
Email: tom@tommarks.xyz
22:48: The borrow checker is smart enough to know you can split a struct into multiple borrows.
```
struct Ctx { vm: VM, handlers: HashMap }
let mut ctx = Ctx { vm: VM::new(), handlers: HashMap::new() };
let vm = &mut ctx.vm;
let handlers = &mut ctx.handlers;
handlers.get_mut(1).and_then(|h| h.handle(vm)); // This is OK, because man.vm can never overlap with man.handlers
```
There's also pattern-destructuring syntax if you want to be even more explicit:
```rust
struct Manager { vm: VM, handlers: HashMap }
fn do_the_thing(manager: &mut Manager) -> Result {
let Manager { vm, handlers } = manager; // vm: &mut VM, handlers: &mut HashMap
let handler = handlers.get_mut(2).unwrap(); // handler: &mut Handler
let arg = vm.registers[0];
handler.handle(vm, arg)
}
```
I keep being surprised by how clever the borrow checker is!