grid_rs/lib.rs
1//! # Slipstream Rust SDK (Grid)
2//!
3//! ## Overview
4//!
5//! This is a library for using host functions from the [*Slipstream
6//! runtime*](https://github.com/s3ndotxyz/runtime). It's more or less a wrapper for linked Wasm imports.
7//!
8//! ## Features
9//!
10//! * *kvs*. Provides access to the native in-runtime key-value store. You should be able to manage keys, values, and stores from within your functions. *Note:* stores are currently only available, one for each function.
11//! * *time*. We're enabling secure time for you to do things like event scheduling. This is a work in progress.
12//!
13//! We have plans to enable a few other things such as message queues and web-sockets in the near
14//! future. Stay tuned!
15//!
16//! ## Example
17//!
18//! Here's a simple example of a function that stores a username and a vector of bytes in the key-value store:
19//!
20//! ```
21//! use grid_rs::{kvs::Storage};
22//! use serde::{Deserialize, Serialize};
23//!
24//! #[grid_rs::main]
25//! fn main(input: &[u8]) -> Result<Vec<u8>, String> {
26//! let input: MyInput = match serde_json::from_slice(input) {
27//! Ok(input) => input,
28//! Err(e) => {
29//! return Err(format!(
30//! "JSON deserialization failed: {e}. Input was: {}",
31//! String::from_utf8_lossy(input)));
32//! }
33//! };
34//!
35//! Storage::put(&input.username, &input.data);
36//!
37//! let output = MyOutput {
38//! status: "success".to_string(),
39//! result: input.data,
40//! };
41//!
42//! Ok(serde_json::to_vec(&output).unwrap())
43//! }
44//!
45//! #[derive(Deserialize)]
46//! struct MyInput {
47//! username: String,
48//! data: Vec<u8>,
49//! }
50//!
51//! #[derive(Serialize)]
52//! struct MyOutput {
53//! status: String,
54//! result: Vec<u8>,
55//! }
56//! ```
57
58#![no_main]
59pub mod time;
60pub mod kvs;
61#[doc(hidden)]
62pub mod region;
63
64pub use grid_rs_macros::main;
65
66use region::Region;
67
68/// Output buffer writer.
69#[derive(Default)]
70pub struct Output;
71
72impl Output {
73 pub fn write_all(data: &[u8]) -> usize {
74 let region_ptr = Region::release_buffer(data.to_vec());
75 region_ptr as usize
76 }
77}