Module samples

Module samples 

Source
Expand description

§Sample Pool Item Implementations

This module contains example PoolItem implementations that demonstrate different patterns and use cases for the library.

§Samples Overview

SampleComplexityDemonstrates
UserSessionBeginnerRc<RefCell<T>>, helper structs, basic #[pool_item] usage
ChatRoomBeginnerSimple state management, minimal boilerplate
RandomsIntermediateShutdown hooks, benchmarking patterns
RandomsBatchAdvancedGenerics, nested thread pools, custom Init types

§1. Start with UserSession

UserSession is the canonical example showing the main advantage of this library: using Rc and RefCell for shared state without locks.

use messaging_thread_pool::{ThreadPool, samples::*};

let pool = ThreadPool::<UserSession>::new(2);

// Create a session
pool.send_and_receive_once(UserSessionInit(1)).unwrap();

// Log actions
pool.send_and_receive_once(LogActionRequest(1, "Login".to_string())).unwrap();

// Get the log
let log: Vec<String> = pool
    .send_and_receive_once(GetLogRequest(1))
    .unwrap()
    .result;

§2. See ChatRoom for Minimal Boilerplate

ChatRoom shows the simplest possible #[pool_item] usage with mutable state:

use messaging_thread_pool::{ThreadPool, samples::*};

let pool = ThreadPool::<ChatRoom>::new(2);

pool.send_and_receive_once(ChatRoomInit(1)).unwrap();
pool.send_and_receive_once(PostRequest(1, "Alice".into(), "Hello!".into())).unwrap();

let history = pool.send_and_receive_once(GetHistoryRequest(1)).unwrap();
assert_eq!(history.result, vec!["Alice: Hello!"]);

§3. See Randoms for Shutdown Hooks

Randoms demonstrates the Shutdown parameter for cleanup on pool termination:

use messaging_thread_pool::{ThreadPool, samples::*};

let pool = ThreadPool::<Randoms>::new(2);
pool.send_and_receive_once(RandomsAddRequest(1)).unwrap();

// Shutdown returns responses from each item
let shutdown_responses = pool.shutdown();

§4. See RandomsBatch for Advanced Patterns

RandomsBatch shows complex patterns including:

  • Generic pool items
  • Custom initialization types (Init = "RandomsBatchAddRequest<P>")
  • Nested thread pools (a RandomsBatch contains references to another pool)
  • Mocking nested pools for testing

§Using Samples in Tests

These samples are re-exported for use in your tests and benchmarks:

use messaging_thread_pool::samples::*;

See the integration tests in tests/ for more complete examples of each pattern.

Structs§

ChatRoom
A simple chat room that manages a history of messages.
ChatRoomInit
GetHistoryRequest
GetHistoryResponse
GetLogRequest
GetLogResponse
HistoryTracker
A helper struct that needs access to the session’s data.
LogActionRequest
LogActionResponse
MeanRequest
MeanResponse
PanicRequest
PanicResponse
PostRequest
PostResponse
Randoms
A collection of random numbers managed by the thread pool.
RandomsAddRequest
Alias for backwards compatibility
RandomsBatch
A batch of Randoms items, demonstrating nested thread pools.
RandomsBatchAddRequest
Custom initialization request for RandomsBatch.
RandomsInit
Alias for backwards compatibility
RandomsThreadPool
Marker type for using a real ThreadPool<Randoms> as the inner pool.
SumOfSumsRequest
SumOfSumsResponse
SumRequest
SumResponse
UserSession
A user session that tracks actions performed by a user.
UserSessionInit

Enums§

ChatRoomApi
RandomsApi
RandomsBatchApi
UserSessionApi

Traits§

InnerThreadPool
Trait for abstracting the inner thread pool type.

Type Aliases§

ChatRoom_GetHistoryRequest_RequestResponse
ChatRoom_PostRequest_RequestResponse
RandomsBatch_SumOfSumsRequest_RequestResponse
Randoms_MeanRequest_RequestResponse
Randoms_PanicRequest_RequestResponse
Randoms_SumRequest_RequestResponse
UserSession_GetLogRequest_RequestResponse
UserSession_LogActionRequest_RequestResponse