Struct hubs::Hubs [−][src]
pub struct Hubs<T> { /* fields omitted */ }
Expand description
The Hubs data structure.
A Hubs
holds a list of Chunk
s.
Each Chunk
contains an array of values of T
.
Usage
Operation works as follows:
- You create the Hubs
- You split it, to receive the HubsProducer and HubsConsumer. These can be moved to their target threads.
- You can write on the HubsProducer by mutably borrowing single chunks with
.borrow_chunk_mut()
- When writing an element of a chunk, it is your responsibility to set Chunk::used
- You need to call
.commit()
: When a chunk is written, it has to be comitted. This has to be done regardnless whether it is full or not. - There must be no gaps of valid data in the array within a chunk. Upon commit, the chunk can be read by another thread.
- To read from the Hubs, you can get a ChunkBlock that contains all currently committed chunks.
- A ChunkBlock provides an iterator over all elements in the arrays in all chunks up to
used
.
- A ChunkBlock provides an iterator over all elements in the arrays in all chunks up to
Example
use hubs::{Hubs, HubsInitializer, HubsWriteAccess}; let hubs = Hubs::new_default(); let (mut tx,rx) = hubs.split(); // In 7 Chunks, write the first 9 elements for i in 0 .. 7{ match tx.borrow_chunk_mut(){ Some(guard) => { for k in 0 .. 9 { guard.chunk.data[k] = i*k as u64; guard.chunk.used += 1; } guard.commit(); }, None => panic!("Must not be full") }; } // Now read everything in one go let mut iter = rx.get_chunks_for_tick().into_iter(); for i in 0 .. 7{ for k in 0 .. 9{ assert_eq!(*iter.next().unwrap(), i*k as u64); } } assert_eq!(iter.next(), None);
Implementations
Create a Hubs with a HubsInitializer if the item type implements Default. Upon initialization, all fields will contain the default value.
The same as .new()
but you can define the capacity upon creation.
Take Ownership of the hubs and split it. This is necessary to move one part over to another thread.