Struct hubs::Hubs[][src]

pub struct Hubs<T> { /* fields omitted */ }
Expand description

The Hubs data structure. A Hubs holds a list of Chunks. 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.

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.

Creates a new Hubs with the given initializer. The created Hubs has a fixed capacity to hold Chunks, that cannot be changed after creation. After initialization, you can use .split() to split the Hubs in a consumer and producer. These can be moved to another thread.

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.

Get the fixed capacity of the hubs. This equals the allocated chunks. Since we need a bit of space between the read and write barrier to handle the wrap around case, you can store one chunk less than the capacity before you need to read from the hubs.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.