Expand description
§databoard
Implementation of a hierarchical key-value-store with the possibility to do a remapping from a level up to its parent level.
It supports both std and no_std environments (requires alloc).
- Each entry is a
dataportport, so values can be accessed through the databoard as well as through port handles connected to the entry. - The value accessors are
async, as they wait for the value to be unlocked; the non-waiting variantstry_getandtry_setfail on a locked value instead. - The databoard implements the
tinyscriptEnvironment, so scripts can read and write its entries. - The local entries of a databoard can be saved with
backupand written back withrestore.
§Hierarchy
The following restrictions apply:
- Each level has at most one parent.
- There is no remapping or access down the hierarchy.
- The remapping is evaluated recursively up the levels.
You can do the remapping either
- fully automatic,
- fully manual or
- do an automatic remapping with manual overrides.
For controlling the access in the hierarchy there are two kinds of special keys:
- keys prefixed with an
@redirect to the top level databoard of the hierarchy, - keys prefixed with an
_restrict the access to the current databoard.
A key is resolved in the following order:
- A key
@nameis resolved asnameby the top level databoard. - A key
_nameis stored asnamein the current databoard. - A key with a manual remapping is resolved by its remapping target, see below.
- Any other key is resolved by the parent, if automatic remapping is enabled and there is a parent, otherwise it is stored in the current databoard.
§Remapping targets
| Target | Meaning |
|---|---|
{name} | the entry name of the parent |
{@name} | the entry name of the top level databoard |
{_name} | the entry name of the parent, but not further up the hierarchy |
{=} | shortcut for {key}, the entry of the parent with the same name |
literal, 42, {x: 1, y: 2} | a constant, readable with get_from_str into a type implementing FromStr |
A target that is not wrapped in braces, or that contains any of the characters :, " and ',
is a constant.
§Usage
A standalone databoard:
use databoard::prelude::*;
// instantiation of a default Databoard
let mut databoard = Databoard::default();
// setting the value creates the entry, it may fail, so it returns a `Result`
databoard.set("test", 42).await.unwrap();
// getting the value may fail, so it returns a `Result`
let value = databoard.get::<i32>("test").await.unwrap();
assert_eq!(value, 42);
// replacing the value returns the previous value
let old = databoard.replace("test", 43).await.unwrap();
assert_eq!(old, Some(42));
// removing the entry returns its port, which still holds the value
let port = databoard.remove("test").unwrap();
assert_eq!(port.try_get::<i32>(), Ok(Some(43)));Hierarchical usage:
use databoard::prelude::*;
let mut top_level = Databoard::default();
// this creates a databoard with automatic remapping to parent
let level1 = Databoard::with_parent(top_level.clone());
// some remapping rules
let mut remappings = RemappingList::default();
remappings.add("test".into(), "{test}".into()).unwrap();
remappings.add("other_test".into(), "{test}".into()).unwrap();
remappings.add("limit".into(), "100".into()).unwrap();
// this creates a databoard with manual remapping to parent using the defined remapping rules
let level2 = Databoard::with(Some(level1.clone()), Some(remappings), false);
// sets the value in the top `databoard`
top_level.set("test", 42).await.unwrap();
// but it can also be accessed from the two other levels
let value1: i32 = level1.get("test").await.unwrap();
let value2: i32 = level2.get("other_test").await.unwrap();
assert_eq!((value1, value2), (42, 42));
// a constant from the remappings
let limit: u8 = level2.get_from_str("limit").await.unwrap();
assert_eq!(limit, 100);Port handles and backup:
use databoard::prelude::*;
let mut databoard = Databoard::default();
databoard.set("counter", 0_u32).await.unwrap();
// a handle is connected to the entry, every kind of handle is available for every entry
let handle = databoard.in_out_handle::<u32>("counter").unwrap();
let backup = databoard.backup().unwrap();
*handle.write().await = Some(1);
assert_eq!(databoard.get::<u32>("counter").await.unwrap(), 1);
// restoring keeps the connection, so the handle sees the restored value
databoard.restore(&backup).unwrap();
assert_eq!(*handle.read().await, Some(0));§Features
std(default): use the standard library. Without it the crate isno_stdand requiresalloc.
The minimum supported Rust version is 1.95.0.
§License
Licensed with the fair use “NGMC” license, see license file
§Contribution
Any contribution intentionally submitted for inclusion in the work by you, shall be licensed with the same “NGMC” license, without any additional terms or conditions.
Modules§
Structs§
- Databoard
- A hierarchical, thread safe name/value store.
The name is something that can be converted into an
Arc<str>. The value can be anything that implementsAnyPortValue, which are the traitsAny,Send,Sync,DebugandClone. Each entry is stored as anInOutport. - Databoard
Backup - A backup of the entries in the local database of a
Databoard, created byDataboard::backupand written back byDataboard::restore. - Remapping
List - A mutable list of manual remappings of a
Databoardto its parent, each akeywith atarget.
Enums§
- Error
- Things that may go wrong using the
Databoard. - Remapping
Target - Target of a remapping entry, created from its textual form with
FromStr, seeRemappingListfor the syntax.
Functions§
- check_
board_ pointer - Returns the name within the braces if
keyis aDataboardpointer, seeis_board_pointer. - check_
local_ key - Returns the name without the prefix
_ifkeyis a key for the local database of aDataboard. - check_
top_ level_ key - Returns the name without the prefix
@ifkeyis a key for the top levelDataboard. - is_
board_ pointer - Returns
trueifkeyis a pointer into aDataboard, otherwisefalse. - is_
const_ assignment - Returns
trueifkeyis a constant assignment, otherwisefalse. - strip_
board_ pointer - Returns the name within the braces if
keyis aDataboardpointer, otherwiseNone.
Type Aliases§
- Arc
- A thread safe reference counting pointer.
Uses
portable-atomic, so it also works on targets without native atomic operations. - Blocking
Mutex - A mutex for short, synchronous accesses from within a critical section.
- Mutex
- An async mutex, which may be held across
.awaitpoints. - Mutex
Guard - The guard of a locked
Mutex, releasing the lock when dropped. - RawMutex
- The raw mutex used by all locks.
On
stdeach lock has its own reentrant mutex instead of one process-wide critical section. - RwLock
- An async reader-writer lock, allowing multiple readers or a single writer.
- RwLock
Read Guard - The guard of a shared read access to a
RwLock, releasing the access when dropped. - RwLock
Write Guard - The guard of an exclusive write access to a
RwLock, releasing the access when dropped.