pub struct QueueBuilder<Target>where
Target: StQueue,{ /* private fields */ }Expand description
A type-safe builder for queues such that:
pushcan only be called correct number of times with correct types,finishcan only be called when all elements to reach theTargettype are pushed.
Further, since queues can represent any struct, QueueBuilder can be used as a generic builder for any struct or tuple.
§Example
In the following example, we want to build a queue of four elements of types u32, bool, char and String respectively.
For this, we can create a builder with QueueBuilder::<MyQueue>::new() where MyQueue is the target type to instantiate.
use orx_meta::queue::*;
type MyQueue = Queue<u32, Queue<bool, Queue<char, QueueSingle<String>>>>;
let instance = QueueBuilder::<MyQueue>::new()
.push(42)
.push(true)
.push('x')
.push("foo".to_string())
.finish();
assert_eq!(instance.as_tuple(), (&42, &true, &'x', &"foo".to_string()));This provides a convenient way to build complex types without errors while getting compiler support on what to push next.
However, it is not easy to hand-write the type alias for complex recursive queue types.
Therefore, this builder pattern is most useful when used together with the queue_of macro.
The above example could be re-written as follows with the queue_of macro.
use orx_meta::queue::*;
use orx_meta::queue_of;
type MyQueue = queue_of!(u32, bool, char, String);
let instance = QueueBuilder::<MyQueue>::new()
.push(42)
.push(true)
.push('x')
.push("foo".to_string())
.finish();
assert_eq!(instance.as_tuple(), (&42, &true, &'x', &"foo".to_string()));§Examples - Type Safety
Note that this builder pattern is type safe in the sense that neither of the following wrong implementations compiles.
Here the elements are pushed in the wrong order:
use orx_meta::queue::*;
use orx_meta::queue_of;
type MyQueue = queue_of!(u32, bool, char, String);
let instance = QueueBuilder::<MyQueue>::new()
.push(true) // wrong order!
.push(42)
.push('x')
.push("foo".to_string())
.finish();
assert_eq!(instance.as_tuple(), (&42, &true, &'x', &"foo".to_string()));And here, not all elements are pushed:
use orx_meta::queue::*;
use orx_meta::queue_of;
type MyQueue = queue_of!(u32, bool, char, String);
let instance = QueueBuilder::<MyQueue>::new()
.push(42)
.push(true)
.push('x')
.finish(); // forgot to push the String
assert_eq!(instance.as_tuple(), (&42, &true, &'x', &"foo".to_string()));