#input-stream #r-socket #string #guest #wasm-rs #protocols

无需std wasmrs-guest

wasmRS guest实现了RSocket协议,用于WebAssembly中的反应式流

16次重大发布

0.17.0 2023年10月9日
0.15.0 2023年8月17日
0.14.0 2023年7月26日
0.8.0 2023年3月22日
0.1.0 2022年12月27日

#606WebAssembly

Download history 66/week @ 2024-03-13 91/week @ 2024-03-20 95/week @ 2024-03-27 71/week @ 2024-04-03 70/week @ 2024-04-10 60/week @ 2024-04-17 640/week @ 2024-04-24 65/week @ 2024-05-01 82/week @ 2024-05-08 69/week @ 2024-05-15 51/week @ 2024-05-22 80/week @ 2024-05-29 56/week @ 2024-06-05 56/week @ 2024-06-12 68/week @ 2024-06-19 52/week @ 2024-06-26

233 每月下载量
用于 27 个crate(2 个直接使用)

Apache-2.0

200KB
5K SLoC

wasmrs-guest

该crate为使用wasmRS RSocket协议的wasmRS模块提供WebAssembly端逻辑。

用法

这是一个基本的WebAssembly模块实现,导出三个操作

  • greeting::sayHello(input: string) -> string - 返回问候语,例如 `Hello World!'
  • echo::chars(input: string) -> stream string - 返回一个字符串流,代表输入字符串中的每个字符
  • echo::reverse(input: stream string) -> stream string - 反转输入流中的每个字符串,并在流上输出
use guest::*;
use wasmrs_guest as guest;

#[no_mangle]
extern "C" fn __wasmrs_init(guest_buffer_size: u32, host_buffer_size: u32, max_host_frame_len: u32) {
  guest::init(guest_buffer_size, host_buffer_size, max_host_frame_len);

  guest::register_request_response("greeting", "sayHello", request_response);
  guest::register_request_stream("echo", "chars", request_stream);
  guest::register_request_channel("echo", "reverse", request_channel);
}

fn request_response(input: Mono<ParsedPayload, PayloadError>) -> Result<Mono<Payload, PayloadError>, GenericError> {
  Ok(async move {
    let input = deserialize::<String>(&input.await.unwrap().data).unwrap();
    let output = format!("Hello, {}!", input);
    Ok(Payload::new_data(None, Some(serialize(&output).unwrap().into())))
  }.boxed())
}

fn request_stream(
  input: Mono<ParsedPayload, PayloadError>,
) -> Result<FluxReceiver<Payload, PayloadError>, GenericError> {
  let channel = FluxChannel::<Payload, PayloadError>::new();
  let rx = channel.take_rx().unwrap();
  spawn(async move {
    let input = deserialize::<String>(&input.await.unwrap().data).unwrap();
    for char in input.chars() {
      channel
        .send(Payload::new_data(None, Some(serialize(&char).unwrap().into())))
        .unwrap();
    }
  });

  Ok(rx)
}
fn request_channel(
  mut input: FluxReceiver<ParsedPayload, PayloadError>,
) -> Result<FluxReceiver<Payload, PayloadError>, GenericError> {
  let channel = FluxChannel::<Payload, PayloadError>::new();
  let rx = channel.take_rx().unwrap();
  spawn(async move {
    while let Some(payload) = input.next().await {
      if let Err(e) = payload {
        println!("{}", e);
        continue;
      }
      let payload = payload.unwrap();
      let input = deserialize::<String>(&payload.data).unwrap();
      let output: String = input.chars().rev().collect();
      if let Err(e) = channel.send(Payload::new_data(None, Some(serialize(&output).unwrap().into()))) {
        println!("{}", e);
      }
    }
  });

  Ok(rx)
}

Apex代码生成器

NanoBus iota代码生成器使用wasmRS协议。您可以使用https://github.com/apexlang/apex CLI从这些模板构建wasmRS模块。

运行以下命令开始

$ apex new git@github.com:nanobus/iota.git -p templates/rust [your-project]

然后,编辑apex.axdl接口定义以符合您的需求,并运行apex build以生成wasmRS模块。

更多信息

WasmRS大量使用来自apex规范和生成器的代码来自动化所有样板代码。有关使用方法,请参阅入门

有关wasmRS的更多信息,请参阅核心wasmrs库。

贡献

请参阅CONTRIBUTING.md

许可证

请参阅根目录下的LICENSE.txt

依赖项

~3–11MB
~109K SLoC