Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mossaka/wit-vtable
vtable for functions
https://github.com/mossaka/wit-vtable
Last synced: about 1 month ago
JSON representation
vtable for functions
- Host: GitHub
- URL: https://github.com/mossaka/wit-vtable
- Owner: Mossaka
- Created: 2022-07-20T06:15:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-07-28T18:17:55.000Z (over 2 years ago)
- Last Synced: 2023-03-04T09:44:13.470Z (over 1 year ago)
- Language: Rust
- Size: 83 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# vtable for WIT functions
This repo experiments implementing multiple callback functions in a wasm module that links to WIT bindings.
## register callback functions
```rust
fn main() -> Result<()> {
events::Events::new()
.listen("handle_event1")
.listen("handle_event2")
.exec();
Ok(())
}#[register_handler]
fn handle_event1(ev: Event) {
println!("handle_event1 {}", ev.id);
}#[register_handler]
fn handle_event2(ev: Event) {
println!("handle_event2 {}", ev.id);
}```
## run
![run result](/assets/run_result.png)## Future proposal: HTTP Router and Server
```rust
fn main() -> Result<()> {
let router = Router::builder()
.get("/", "home_handler")
.get("/users/:userId", "user_handler")
.build()?
let service = Service::new(router)?;
Server::bind("localhost:3000").serve(service);
}#[register_get]
fn home_handler(req: Request) -> Result, Infallible> {
let state = req.data::().unwrap();
println!("State value: {}", state.0);Ok(Response::new(Body::from("Home page")))
}#[register_get]
fn user_handler(req: Request) -> Result, Infallible> {
let user_id = req.param("userId").unwrap();
Ok(Response::new(Body::from(format!("Hello {}", user_id))))
}```