Myrddin Dundragon 7a83efe481 Copy is no longer required for items stored.
This is a big change. Initially items added to the RingBuffer needed to
have a Default implementation. This nessecitated the use of Copy.
However, by doing so the RingBuffer was not able to store non-Copy types
like String or Vec.

To allow for this change the buffer switched from using Default to
initialize the array to using MaybeUninit and UnsafeCell to initialize
the array and write to or read the memory location with interior mutability.
This means that the atomics are now the definitive guards like was
initially desired and the mutable reference needed for push and
pop could be removed.

Drop was added to clean up the items in the array now that they may need
to be dropped themselves.

Some convinence functions were added for users to get information about
the RingBuffer.
2026-02-15 13:23:58 -05:00
2025-10-17 10:28:05 -04:00
2025-12-13 08:40:49 -05:00
2025-06-27 08:48:57 -04:00
2025-06-27 08:48:57 -04:00
2025-06-28 16:52:04 -04:00

Example

This repository demonstrates modern, idiomatic Rust through two parts:

  1. Foundational Rust Examples covering core concepts like lifetimes, error handling, async, and testing.
  2. Advanced Async Job Runner a trait-based, concurrent task execution framework built using tokio.

Part 1: Foundational Rust Examples

A lightweight collection of examples demonstrating essential Rust features, without relying on external dependencies (except tokio).

Concepts Covered

  • Arrays and Iteration
  • Lifetimes and Borrowing
  • Error Handling with Result, ?, and custom messages
  • Async Programming using #[tokio::main]
  • File I/O with the standard library
  • Unit and async testing

Key Functions

Function Description
longest Returns the longest string in a slice without copying.
read_and_parse Reads a file and parses its content into an integer.
run_tasks Demonstrates spawning and awaiting async tasks.

Testing

Includes both synchronous and asynchronous unit tests under a #[cfg(test)] module.


Part 2: Advanced Async Job Runner

An extensible, asynchronous job system using:

  • Trait objects
  • Pinned futures (Pin<Box<dyn Future>>)
  • Tokios JoinSet for concurrent execution
  • Fully dynamic job management without macros

Why It Matters

This system demonstrates:

  • Manual implementation of async trait behavior (without async_trait)
  • Advanced type management (Box<dyn Trait>, lifetimes, Pin)
  • Idiomatic use of tokio::task::JoinSet for parallelism

Architecture

Job Trait

pub trait Job: Send + Sync {
    fn name(&self) -> &str;
    fn run<'a>(&self) -> Pin<Box<dyn Future<Output = JobResult> + Send + 'a>>
        where Self: Sync + 'a;
}

JobRunner Struct

pub struct JobRunner {
    jobs: Vec<Box<dyn Job>>
}

Manages and runs jobs concurrently.

Built-In Jobs

Job Type Behavior
FileJob (Planned) Reads from disk
SleepJob Simulates a delay
MathJob Performs a math task

Usage

let mut runner = JobRunner::new();
runner.add_job(FileJob::new());
runner.add_job(SleepJob::new());
runner.add_job(MathJob::new());

for (name, result) in runner.run_all().await {
    match result {
        Ok(msg) => println!("{name}: {msg}"),
        Err(err) => eprintln!("{name}: {err}"),
    }
}

Testing & Extending

You can add unit or integration tests using mock jobs to simulate both success and failure.

Ideas for extension:

  • Make the Jobs actually do something
  • Interjob communication using channels
  • Create a QueueRunner that will run jobs in sequence
  • Create a ParallelRunner that will run jobs concurrently (simple rename)
  • Create a system that allows QueueRunners and ParallelRunners to be combined

Requirements

  • Rust 1.76+ (for full language support)
  • Tokio 1.37+ (for async runtime)

Project Structure

src/
├── adv_async.rs  # Advanced async JobRunner
├── basic.rs      # Foundational examples
├── info.rs       # Crate information from Cargo
├── lib.rs        # Entry point for the library

Copyright 2025 Jason Travis Smith

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this library except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS

Description
Just a small sample of Rust code.
Readme 1.4 MiB
Languages
Rust 100%