QueueBuilder

Struct QueueBuilder 

Source
pub struct QueueBuilder<Target>
where Target: StQueue,
{ /* private fields */ }
Expand description

A type-safe builder for queues such that:

  • push can only be called correct number of times with correct types,
  • finish can only be called when all elements to reach the Target type 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()));

Implementations§

Source§

impl<Target> QueueBuilder<Target>
where Target: StQueue,

Source

pub fn new() -> Self

Creates a new empty builder for the Target type defined as the generic argument.

Source

pub fn push( self, element: Target::Front, ) -> QueueBuilding<Target, Target::Back, QueueSingle<Target::Front>>

Pushes the next element to build the target type.

Trait Implementations§

Source§

impl<Target> Default for QueueBuilder<Target>
where Target: StQueue,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Target> Freeze for QueueBuilder<Target>

§

impl<Target> RefUnwindSafe for QueueBuilder<Target>
where Target: RefUnwindSafe,

§

impl<Target> Send for QueueBuilder<Target>
where Target: Send,

§

impl<Target> Sync for QueueBuilder<Target>
where Target: Sync,

§

impl<Target> Unpin for QueueBuilder<Target>
where Target: Unpin,

§

impl<Target> UnwindSafe for QueueBuilder<Target>
where Target: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.