Expand description
Safe SDK for writing host functions.
These types encapsulate C ABI pointer conversion and Zig Arena memory allocation, so host function implementations can be safe Rust with zero raw pointer manipulation.
§Quick reference
| Type | Direction | C ABI signature | Safe Rust type |
|---|---|---|---|
HostStr | Zig → Rust | ptr: *const u8, len: usize | &str |
JsStr | Rust → Zig | -> JsStr (ptr+len repr C) | String / &str |
JsStrField | Rust → Zig | struct field (ptr+len pair) | String / &str |
§Example: sync string function
ⓘ
use js2rust_bridge::sdk::{HostStr, JsStr};
#[unsafe(no_mangle)]
pub unsafe extern "C" fn host_concat(
s1_ptr: *const u8, s1_len: usize,
s2_ptr: *const u8, s2_len: usize,
) -> JsStr {
let s1 = HostStr::from_raw(s1_ptr, s1_len);
let s2 = HostStr::from_raw(s2_ptr, s2_len);
JsStr::new(&format!("{}{}", &s1, &s2))
}§Example: async struct return with string field
ⓘ
use js2rust_bridge::sdk::{HostStr, JsStrField};
#[repr(C)]
pub struct MyResult {
pub id: i64,
pub name: JsStrField,
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn fetch_user(
name_ptr: *const u8, name_len: usize,
) -> MyResult {
let name = HostStr::from_raw(name_ptr, name_len);
let user = runtime().block_on(lookup_user(&name));
MyResult {
id: user.id,
name: JsStrField::new(&user.name),
}
}Structs§
- HostStr
- A string parameter received from Zig via C ABI (ptr+len pair).
- JsStr
- Return type for sync host functions that return a string.
- JsStr
Field - A string field inside an async host function’s C ABI return struct.