r3bl_rs_utils
This library provides utility functions:
- Thread safe asynchronous Redux library (uses
tokio
to run subscribers and middleware in parallel). The reducer functions are run single threaded. - Functions to unwrap deeply nested objects inspired by Kotlin scope functions.
- Non binary tree data structure inspired by memory arenas, that is thread safe and supports parallel tree walking.
- Capabilities to make it easier to build TUIs (Text User Interface apps) in Rust.
- And more.
π‘ To learn more about this library, please read how it was built on developerlife.com.
- You can also read all the Rust content on developerlife.com here.
- The equivalent of this library is available for TypeScript and is called r3bl-ts-utils.
Usage
Please add the following to your Cargo.toml
file:
[]
= "0.6.7"
redux
Store
is a thread safe asynchronous Redux library that uses tokio
to run subscribers and
middleware in parallel. The reducer functions are run single threaded. Here's an example of how to
use it. Let's say we have the following action enum, and state struct.
/// Action enum.
/// State.
Here's an example of the reducer function.
// Reducer function (pure).
let reducer_fn = ;
Here's an example of an async subscriber function (which are run in parallel after an action is dispatched). The following example uses a lambda that captures a shared object. This is a pretty common pattern that you might encounter when creating subscribers that share state in your enclosing block or scope.
// This shared object is used to collect results from the subscriber function & test it later.
let shared_object = new;
// This subscriber function is curried to capture a reference to the shared object.
let subscriber_fn = with;
Here are two types of async middleware functions. One that returns an action (which will get
dispatched once this middleware returns), and another that doesn't return anything (like a logger
middleware that just dumps the current action to the console). Note that both these functions share
the shared_object
reference from above.
// This middleware function is curried to capture a reference to the shared object.
let mw_returns_none = with;
// This middleware function is curried to capture a reference to the shared object.
let mw_returns_action = with;
Here's how you can setup a store with the above reducer, middleware, and subscriber functions.
// Setup store.
let mut store = new;
store
.add_reducer
.await
.add_subscriber
.await
.add_middleware
.await;
Finally here's an example of how to dispatch an action in a test. You can dispatch actions
asynchronously using dispatch_spawn()
which is "fire and forget" meaning that the caller won't
block or wait for the dispatch_spawn()
to return. Then you can dispatch actions synchronously if
that's what you would like using dispatch()
.
// Test reducer and subscriber by dispatching Add and AddPop actions asynchronously.
store.dispatch_spawn.await;
store.dispatch.await;
assert_eq!;
store.dispatch.await;
assert_eq!;
store.clear_subscribers.await;
// Test async middleware: mw_returns_action.
shared_object.lock.unwrap.clear;
store
.add_middleware
.dispatch
.await;
assert_eq!;
assert_eq!;
tree_memory_arena (non-binary tree data structure)
[Arena
] and [MTArena
] types are the implementation of a
non-binary tree data structure that is
inspired by memory arenas.
Here's a simple example of how to use the [Arena
] type:
use ;
let mut arena = new;
let node_1_value = 42 as usize;
let node_1_id = arena.add_new_node;
println!;
assert_eq!;
Here's how you get weak and strong references from the arena (tree), and tree walk:
use ;
let mut arena = new;
let node_1_value = 42 as usize;
let node_1_id = arena.add_new_node;
Here's an example of how to use the [MTArena
] type:
use ;
use ;
type ThreadResult = ;
type Handles = ;
let mut handles: Handles = Vec new;
let arena = new;
// Thread 1 - add root. Spawn and wait (since the 2 threads below need the root).
// Perform tree walking in parallel. Note the lambda does capture many enclosing variable context.
π There are more complex ways of using [
Arena
] and [MTArena
]. Please look at these extensive integration tests that put them thru their paces here.
utils
LazyMemoValues
This struct allows users to create a lazy hash map. A function must be provided that computes the values when they are first requested. These values are cached for the lifetime this struct. Here's an example.
use ;
use LazyMemoValues;
// These are copied in the closure below.
let arc_atomic_count = new;
let mut a_variable = 123;
let mut a_flag = false;
let mut generate_value_fn = new;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!; // Won't regenerate the value.
assert_eq!; // Doesn't change.
tty
This module contains a set of functions to make it easier to work with terminals.
The following is an example of how to use is_stdin_piped()
:
The following is an example of how to use readline()
:
use ;
Here's a list of functions available in this module:
readline_with_prompt()
print_prompt()
readline()
is_tty()
is_stdout_piped()
is_stdin_piped()
safe_unwrap
Functions that make it easy to unwrap a value safely. These functions are provided to improve the
ergonomics of using wrapped values in Rust. Examples of wrapped values are <Arc<RwLock<T>>
, and
<Option>
. These functions are inspired by Kotlin scope functions & TypeScript expression based
language library which can be found
here on r3bl-ts-utils
.
Here are some examples.
use ;
use ;
use ;
if let Some = parent_id_opt
Here's a list of functions that are provided:
call_if_some()
call_if_none()
call_if_ok()
call_if_err()
with()
with_mut()
unwrap_arc_write_lock_and_call()
unwrap_arc_read_lock_and_call()
Here's a list of type aliases provided for better readability:
ReadGuarded<T>
WriteGuarded<T>
color_text
ANSI colorized text https://github.com/ogham/rust-ansi-term helper methods. Here's an example.
use ;
Here's a list of functions available in this module:
print_header()
style_prompt()
style_primary()
style_dimmed()
style_error()
tui (experimental)
π§ WIP - This is an experimental module that isnβt ready yet. It is the first step towards creating
a TUI library that can be used to create sophisticated TUI applications. This is similar to Ink
library for Node.js & TypeScript (that uses React and Yoga). Or kinda like tui
built atop
crossterm
(and not termion
).
Stability
π§βπ¬ This library is in early development.
- There are extensive integration tests for code that is production ready.
- Everything else is marked experimental in the source.
Please report any issues to the issue tracker. And if you have any feature requests, feel free to add them there too π.