$ show tock/libtock-rs
This is the Rust userland library for Tock OS, providing safe abstractions over Tock syscalls (command, subscribe, allow) and a comprehensive set of driver APIs for peripherals such as GPIO, I2C, SPI, console, sensors, display, and networking. It includes a runtime with startup code for ARM, RISC-V, and x86 processes, a build system for generating Tock Binary Format (TBF) and TAB archives, and a full unit-testing framework with a fake kernel and fake drivers that enable testing Tock applications on a host machine. The architecture is trait-based, with drivers generic over a `Syscalls` implementation, allowing compile-time dependency injection for testing. This repo is not a standalone application but a library and framework for building Tock processes. It demonstrates high craftsmanship in its safe resource management patterns (e.g., `share::scope` for scoped buffer allocation) and includes a standalone micro-formatting crate (`ufmt`) that is 6-40x smaller than `core::fmt`.
The LLM identified 2 files worth highlighting for reuse:
Implements the `share::scope` pattern for safe, scoped resource management of kernel buffers and subscriptions. It provides a `scope` function that takes a callback with `Handle` objects for `AllowRo`, `AllowRw`, and `Subscribe`. When the callback returns, all resources are automatically released (unallowed/unsubscribed) without requiring dropping in the application code. This pattern avoids heap allocation and ensures no resource leaks, making it highly reusable for any embedded system that needs to temporarily lease resources to a kernel or driver.
A standalone Rust crate providing a lightweight formatting system for `#![no_std]` environments. It implements a `uwrite!` macro and a `uDisplay` trait analogous to `core::fmt::Display` but generates 6–40x smaller code. The implementation avoids the complex generics and trait objects of `core::fmt`, instead using monomorphization with const generics for zero-cost abstraction. It includes implementations for primitive types, tuples, arrays, and core types, and provides a `Write` trait and a `FixedMemoryWriter` for writing to byte buffers. This crate can be extracted and used in any no_std project that needs minimal binary size.