ufotofu 0.12.2

Abstractions for lazily consuming and producing sequences
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
//! In-memory queues for adding buffering to arbitrary producers and consumers.
//!
//! This module provides the [`Queue`] trait for infallible in-memory queues supporting bulk push and pop operations. You can safely consider this trait (and this module) a low-level implementation detail you likely will not interact with directly. Queues power some useful functionality, such as creating buffered versions of arbitrary producers and consumers, via the [`ProducerExt::to_buffered`](crate::ProducerExt::to_buffered) and [`ConsumerExt::to_buffered`](crate::ConsumerExt::to_buffered) methods.
//!
//! Ufotofu provides one concrete implementations of the [`Queue`] trait: the [`Contiguous`] queue, which uses a single contiguous slice of items for storage. For queue creation, see [`new_contiguous`], [`new_static`], and [`new_fixed`].
//!
//! ```
//! use ufotofu::prelude::*;
//! use ufotofu::queues::{Queue, new_contiguous};
//! let mut q = new_contiguous(vec![42; 2]);
//! // The vec will never be resized, we only use its `AsRef<[u32]>` and `AsMut<[u32]>` impls.
//! // In a real setting, use `new_fixed` instead if you want a simple heap-allocated queue.
//!
//! assert!(q.is_empty());
//! assert_eq!(q.dequeue(), None);
//! assert_eq!(q.enqueue(0), None);
//! assert_eq!(q.enqueue(1), None);
//! assert_eq!(q.dequeue(), Some(0));
//! assert_eq!(q.len(), 1);
//! assert_eq!(q.enqueue(2), None);
//! assert!(q.is_full());
//! assert_eq!(q.enqueue(3), Some(3)); // Not enqueued.
//! assert_eq!(q.dequeue(), Some(1));
//! assert_eq!(q.dequeue(), Some(2));
//! assert_eq!(q.dequeue(), None);
//! ```

#[cfg(feature = "alloc")]
use alloc::{boxed::Box, vec::Vec};

use core::cmp::min;

mod contiguous;
pub use contiguous::*;

#[cfg(feature = "alloc")]
mod unbounded;
#[cfg(feature = "alloc")]
pub use unbounded::*;
#[cfg(feature = "alloc")]
mod elastic;
#[cfg(feature = "alloc")]
pub use elastic::*;
#[cfg(feature = "alloc")]
mod unbounded_elastic;
#[cfg(feature = "alloc")]
pub use unbounded_elastic::*;
#[cfg(feature = "alloc")]
mod fault_tolerant_elastic;
#[cfg(feature = "alloc")]
pub use fault_tolerant_elastic::*;

#[cfg(feature = "dev")]
pub mod dev;

/// A trait for infallbile first-in-first-out queues.
///
/// Beyond the entirely typical [`enqueue`](Queue::enqueue) and [`dequeue`](Queue::dequeue) methods, this trait also provides ufotofu-style [`expose_slots`](Queue::expose_slots) and [`expose_items`](Queue::expose_items) methods for transferring multiple items with single method calls.
///
/// This trait describes infallible in-memory queues; its methods are synchronous and do not support error reporting.
///
/// Queues must be able to store at least one item. In other words, enqueueing into an empty queue must always succeed.
///
/// See also the [`BoundedQueue`] trait for queues of a known maximum capacity, and the [`QueueExt`] trait for helper methods on all queues.
pub trait Queue {
    /// The type of items to manage in the queue.
    type Item;

    /// Returns the number of items currently in the queue.
    fn len(&self) -> usize;

    /// Returns whether the queue is empty. Must return `true` if and only if `self.len()` returns `0`.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns whether the queue is full, i.e., whether calling `enqueue` would fail to enqueue an item.
    ///
    /// When this method is called and the queue is full, a dynamically sized queue is encouraged to interpret the call as intent to enqueue more items, and to attempt to increase its size in response.
    fn is_full(&self) -> bool;

    /// Returns the maximum number of items that can be stored in the queue at the same time, or `None` if no upper bound is known.
    fn max_capacity(&self) -> Option<usize>;

    /// Attempts to enqueue an item.
    ///
    /// Will return the item instead of enqueueing it if the queue is full at the time of calling. Enqueueing into an empty queue must always suceed.
    ///
    /// <br/>Counterpart: [`Queue::dequeue`]
    fn enqueue(&mut self, item: Self::Item) -> Option<Self::Item>;

    /// Exposes a mutable slice of items to an async function, the function mutates it and then reports to the queue how many of these items should now be considered to have been enqueued.
    ///
    /// The slice passed to the async function must be empty if and only if the queue is full at the time.
    ///
    /// The function further returns a value of some arbitrary type `R`, the `expose_slots` method returns that value.
    ///
    /// The function must not return a number greater than the size of the slice with which it was called.
    ///
    /// The queue must not yield on its own; this function is only async so that it can evaluate the async closure it takes.
    ///
    /// <br/>Counterpart: the [`Queue::expose_items`] method.
    async fn expose_slots<F, R>(&mut self, f: F) -> R
    where
        F: AsyncFnOnce(&mut [Self::Item]) -> (usize, R);

    /// Attempts to dequeue the next item.
    ///
    /// Will return `None` if the queue is empty at the time of calling.
    ///
    /// <br/>Counterpart: [`Queue::enqueue`]
    fn dequeue(&mut self) -> Option<Self::Item>;

    /// Exposes a slice of items to an async function, the function then reports to the queue how many of these items should now be considered to have been dequeued.
    ///
    /// The slice passed to the async function must be empty if and only if the queue has no items enqueued at the time.
    ///
    /// The function further returns a value of some arbitrary type `R`, the `expose_items` method returns that value.
    ///
    /// The function must not return a number greater than the size of the slice with which it was called.
    ///
    /// The queue must not yield on its own; this function is only async so that it can evaluate the async closure it takes.
    ///
    /// <br/>Counterpart: the [`Queue::expose_slots`] method.
    async fn expose_items<F, R>(&mut self, f: F) -> R
    where
        F: AsyncFnOnce(&[Self::Item]) -> (usize, R);
}

/// A [`Queue`] of known maximum capacity. Such queues must never return [`None`] from [`max_capacity`](Queue::max_capacity).
pub trait BoundedQueue: Queue {
    /// Returns the maximum number of items that can be stored in the queue at the same time.
    fn bounded_capacity(&self) -> usize {
        self.max_capacity()
            .expect("A bounded queue must not report None as its max_capacity()")
    }

    /// Returns the remaining number of items that could be stored in the queue at the moment.
    fn available_slots(&self) -> usize {
        self.bounded_capacity() - self.len()
    }
}

/// An extension trait for [`Queue`] that provides helper methods.
/// You never need to implement this trait yourself, it merely adds methods with default implementation to existing queues.
pub trait QueueExt: Queue {
    /// Enqueues a number of items, cloned from the given buffer. Returns how many items were enqueued — returns zero when the queue is already full.
    ///
    /// <br/>Counterpart: the [`QueueExt::bulk_dequeue`] method.
    async fn bulk_enqueue(&mut self, buffer: &[Self::Item]) -> usize
    where
        Self::Item: Clone,
    {
        self.expose_slots(async |slots| {
            let amount = min(slots.len(), buffer.len());
            slots[..amount].clone_from_slice(&buffer[..amount]);

            (amount, amount)
        })
        .await
    }

    /// Dequeues a number of items, by cloning them into the given buffer. Returns how many items were dequeued — returns zero when the queue was already already.
    ///
    /// <br/>Counterpart: the [`QueueExt::bulk_enqueue`] method.
    async fn bulk_dequeue(&mut self, buffer: &mut [Self::Item]) -> usize
    where
        Self::Item: Clone,
    {
        self.expose_items(async |items| {
            let amount = min(items.len(), buffer.len());
            buffer[..amount].clone_from_slice(&items[..amount]);

            (amount, amount)
        })
        .await
    }
}

impl<Q> QueueExt for Q where Q: Queue {}

/// Creates a new queue, using the given value of type `S` as a buffer for items of type `T`, where `T: Default`.
///
/// You probably want `S` to implement `AsRef<[T]>` and `AsMut<[T]>`, otherwise the returned [`Contiguous`] does not implement [`Queue`].
///
/// See [`new_contiguous_with`] for queue creation for item types which do not implement [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_contiguous};
/// let mut q = new_contiguous(vec![42; 2]);
/// // The vec will never be resized, we only use its `AsRef<[u32]>` and `AsMut<[u32]>` impls.
/// // In a real setting, use `new_fixed` instead if you want a simple heap-allocated queue.
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert!(q.is_full());
/// assert_eq!(q.enqueue(3), Some(3)); // Not enqueued.
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// ```
pub fn new_contiguous<S, T>(buffer: S) -> Contiguous<S, T>
where
    T: Default,
{
    Contiguous::new(buffer, Default::default)
}

/// Creates a new queue, using the given value of type `S` as a buffer for items of type `T`.
///
/// The `initialise_memory` function is used internally to ensure that all queue slots contain valid memory at all times. The specific choice of `T` returned by that function does not affect the observable semantics of the queue at all.
///
/// You probably want `S` to implement `AsRef<[T]>` and `AsMut<[T]>`, otherwise the returned [`Contiguous`] does not implement [`Queue`].
///
/// See [`new_contiguous`] for more convenient queue creation for item types implementing [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_contiguous_with};
/// let mut q = new_contiguous_with(vec![42; 2], seventeen);
/// // The vec will never be resized, we only use its `AsRef<[u32]>` and `AsMut<[u32]>` impls.
/// // In a real setting, use `new_fixed_with` instead if you want a simple heap-allocated queue.
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert!(q.is_full());
/// assert_eq!(q.enqueue(3), Some(3)); // Not enqueued.
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
///
/// fn seventeen() -> u32 { 17 }
/// ```
pub fn new_contiguous_with<S, T>(buffer: S, initialise_memory: fn() -> T) -> Contiguous<S, T> {
    Contiguous::new(buffer, initialise_memory)
}

/// Creates a new queue, using a statically sized array as a buffer for items of type `T`, where `T: Default`.
///
/// See [`new_static_with`] for array-backed queue creation for item types which do not implement [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_static};
/// let mut q = new_static::<u32, 2>();
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert!(q.is_full());
/// assert_eq!(q.enqueue(3), Some(3)); // Not enqueued.
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// ```
pub fn new_static<T, const N: usize>() -> Contiguous<[T; N], T>
where
    T: Default,
{
    Contiguous::new(core::array::from_fn(|_| T::default()), T::default)
}

/// Creates a new queue, using a statically sized array as a buffer for items of type `T`.
///
/// The `initialise_memory` function is used internally to ensure that all queue slots contain valid memory at all times. The specific choice of `T` returned by that function does not affect the observable semantics of the queue at all.
///
/// See [`new_static`] for more convenient static queue creation for item types implementing [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_static_with};
/// let mut q = new_static_with::<u32, 2>(seventeen);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert!(q.is_full());
/// assert_eq!(q.enqueue(3), Some(3)); // Not enqueued.
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
///
/// fn seventeen() -> u32 { 17 }
/// ```
pub fn new_static_with<T, const N: usize>(initialise_memory: fn() -> T) -> Contiguous<[T; N], T> {
    Contiguous::new(
        core::array::from_fn(|_| initialise_memory()),
        initialise_memory,
    )
}

/// Creates a new queue, using a `Box<[T]>` as a buffer for items of type `T`, where `T: Default`. The buffer size is fixed at creation.
///
/// See [`new_fixed_with`] for box-backed queue creation for item types which do not implement [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_fixed};
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_fixed(2);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert!(q.is_full());
/// assert_eq!(q.enqueue(3), Some(3)); // Not enqueued.
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// # }
/// ```
#[cfg(feature = "alloc")]
pub fn new_fixed<T>(capacity: usize) -> Contiguous<Box<[T]>, T>
where
    T: Default,
{
    let mut v = Vec::with_capacity(capacity);
    v.resize_with(capacity, Default::default);
    Contiguous::new(v.into_boxed_slice(), T::default)
}

/// Creates a new queue, using a `Box<[T]>` as a buffer for items of type `T`. The buffer size is fixed at creation.
///
/// The `initialise_memory` function is used internally to ensure that all queue slots contain valid memory at all times. The specific choice of `T` returned by that function does not affect the observable semantics of the queue at all.
///
/// See [`new_fixed`] for more convenient box-backed queue creation for item types implementing [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_fixed_with};
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_fixed_with(2, seventeen);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert!(q.is_full());
/// assert_eq!(q.enqueue(3), Some(3)); // Not enqueued.
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// # }
///
/// fn seventeen() -> u32 { 17 }
/// ```
#[cfg(feature = "alloc")]
pub fn new_fixed_with<T>(capacity: usize, initialise_memory: fn() -> T) -> Contiguous<Box<[T]>, T> {
    let mut v = Vec::with_capacity(capacity);
    v.resize_with(capacity, initialise_memory);
    Contiguous::new(v.into_boxed_slice(), initialise_memory)
}

/// Creates a new unbounded queue for items of type `T`, where `T: Default`.
///
/// See [`new_unbounded_with`] for unbounded queue creation for item types which do not implement [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_unbounded};
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_unbounded();
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// # }
/// ```
#[cfg(feature = "alloc")]
pub fn new_unbounded<T>() -> Unbounded<T>
where
    T: Default,
{
    Unbounded::new(T::default)
}

/// Creates a new unbounded queue for items of type `T`
///
/// The `initialise_memory` function is used internally to ensure that all queue slots contain valid memory at all times. The specific choice of `T` returned by that function does not affect the observable semantics of the queue at all.
///
/// See [`new_unbounded`] for more convenient unbounded queue creation for item types implementing [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_unbounded_with};
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_unbounded_with(seventeen);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// # }
///
/// fn seventeen() -> u32 { 17 }
/// ```
#[cfg(feature = "alloc")]
pub fn new_unbounded_with<T>(initialise_memory: fn() -> T) -> Unbounded<T> {
    Unbounded::new(initialise_memory)
}

/// Creates a new elastic queue for items of type `T` where `T: Default`.
///
/// The capacity of the queue changes dynamically based on load, bounded between the `minimum_capacity` and `maximum_capacity` specified at time of creation. **The `minimum_capacity` must be greater than 0.**
///
/// See [`new_elastic_with`] for elastic queue creation for types which do not implement [`Default`].
///
/// # Panics
///
/// Panics if `minimum_capacity > maximum_capacity`.
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_elastic};
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_elastic(1, 2);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
///
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
///
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.enqueue(3), Some(3));
/// assert!(q.is_full());
///
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// assert!(q.is_empty());
/// # }
/// ```
#[cfg(feature = "alloc")]
pub fn new_elastic<T>(minimum_capacity: usize, maximum_capacity: usize) -> Elastic<T>
where
    T: Default,
{
    Elastic::new(minimum_capacity, maximum_capacity, Default::default)
}

/// Creates a new elastic queue for items of type `T`.
///
/// The capacity of the queue changes dynamically based on load, bounded between the `minimum_capacity` and `maximum_capacity` specified at time of creation. **The `minimum_capacity` must be greater than 0.**
///
/// The `initialise_memory` function is used internally to ensure that all queue slots contain valid memory at all times. The specific choice of `T` returned by that function does not affect the observable semantics of the queue at all.
///
/// See [`new_elastic`] for more convenient elastic queue creation for types `T` implementing [`Default`].
///
/// # Panics
///
/// Panics if `minimum_capacity > maximum_capacity`.
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_elastic_with};
///
/// fn seventeen() -> u32 { 17 }
///
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_elastic_with(1, 2, seventeen);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
///
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
///
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.enqueue(3), Some(3));
/// assert!(q.is_full());
///
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// assert!(q.is_empty());
/// # }
#[cfg(feature = "alloc")]
pub fn new_elastic_with<T>(
    minimum_capacity: usize,
    maximum_capacity: usize,
    initialise_memory: fn() -> T,
) -> Elastic<T> {
    Elastic::new(minimum_capacity, maximum_capacity, initialise_memory)
}

/// Creates a new unbounded elastic queue for items of type `T` where `T: Default`.
///
/// The capacity of the queue changes dynamically based on load, with no upper bound.
///
/// See [`new_unbounded_elastic_with`] for unbounded elastic queue creation for types which do not implement [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_unbounded_elastic};
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_unbounded_elastic();
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
///
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
///
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.enqueue(3), None);
///
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), Some(3));
/// assert!(q.is_empty());
/// # }
/// ```
#[cfg(feature = "alloc")]
pub fn new_unbounded_elastic<T>() -> UnboundedElastic<T>
where
    T: Default,
{
    UnboundedElastic::new(Default::default)
}

/// Creates a new unbounded elastic queue for items of type `T` where `T: Default`.
///
/// The capacity of the queue changes dynamically based on load, with no upper bound.
///
/// The `initialise_memory` function is used internally to ensure that all queue slots contain valid memory at all times. The specific choice of `T` returned by that function does not affect the observable semantics of the queue at all.
///
/// See [`new_unbounded_elastic`] for more convenient elastic queue creation for types `T` implementing [`Default`].
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_unbounded_elastic_with};
///
/// fn seventeen() -> u32 { 17 }
///
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_unbounded_elastic_with(seventeen);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
///
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
///
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.enqueue(3), None);
///
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), Some(3));
/// assert!(q.is_empty());
/// # }
/// ```
#[cfg(feature = "alloc")]
pub fn new_unbounded_elastic_with<T>(initialise_memory: fn() -> T) -> UnboundedElastic<T> {
    UnboundedElastic::new(initialise_memory)
}

/// Creates a new fault-tolerant elastic queue for items of type `T`, where `T: Default`.
///
/// The capacity of the queue changes dynamically based on load, bounded between the `minimum_capacity` and `maximum_capacity` specified at time of creation. **The `minimum_capacity` must be greater than 0.**
///
/// The capacity of the queue may be adjusted when items are [`enqueue`](Queue::enqueue)d or [`dequeue`](Queue::dequeue)d. If enqueueing an item would cause the queue to be resized and the corresponding memory allocation fails, the item is returned as though the queue were full.
///
/// See [`new_fault_tolerant_elastic_with`] for fault-tolerant elastic queue creation for types which do not implement [`Default`].
///
/// # Panics
///
/// Panics if `minimum_capacity > maximum_capacity`.
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_fault_tolerant_elastic};
///
/// fn seventeen() -> u32 { 17 }
///
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_fault_tolerant_elastic(1, 2);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
///
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
///
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.enqueue(3), Some(3));
///
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// assert!(q.is_empty());
/// # }
/// ```
#[cfg(feature = "alloc")]
pub fn new_fault_tolerant_elastic<T>(
    minimum_capacity: usize,
    maximum_capacity: usize,
) -> FaultTolerantElastic<T>
where
    T: Default,
{
    FaultTolerantElastic::new(minimum_capacity, maximum_capacity, Default::default)
}

/// Creates a new fault-tolerant elastic queue for items of type `T`.
///
/// The capacity of the queue changes dynamically with load, bounded between the `minimum_capacity` and `maximum_capacity` specified at time of creation. **The `minimum_capacity` must be greater than 0.**
///
/// The capacity of the queue may be adjusted when items are [`enqueue`](Queue::enqueue)d or [`dequeue`](Queue::dequeue)d. If enqueueing an item would cause the queue to be resized and the corresponding memory allocation fails, the item is returned as though the queue were full.
///
/// See [`new_fault_tolerant_elastic`] for more convenient fault-tolerant elastic queue creation for types `T` implementing [`Default`].
///
/// # Panics
///
/// Panics if `minimum_capacity > maximum_capacity`.
///
/// # Example
///
/// ```
/// use ufotofu::prelude::*;
/// use ufotofu::queues::{Queue, new_fault_tolerant_elastic_with};
///
/// fn seventeen() -> u32 { 17 }
///
/// # #[cfg(feature = "alloc")] {
/// let mut q = new_fault_tolerant_elastic_with(1, 2, seventeen);
///
/// assert!(q.is_empty());
/// assert_eq!(q.dequeue(), None);
///
/// assert_eq!(q.enqueue(0), None);
/// assert_eq!(q.enqueue(1), None);
/// assert_eq!(q.dequeue(), Some(0));
/// assert_eq!(q.len(), 1);
///
/// assert_eq!(q.enqueue(2), None);
/// assert_eq!(q.enqueue(3), Some(3));
/// assert!(q.is_full());
///
/// assert_eq!(q.dequeue(), Some(1));
/// assert_eq!(q.dequeue(), Some(2));
/// assert_eq!(q.dequeue(), None);
/// assert!(q.is_empty());
/// # }
#[cfg(feature = "alloc")]
pub fn new_fault_tolerant_elastic_with<T>(
    minimum_capacity: usize,
    maximum_capacity: usize,
    initialise_memory: fn() -> T,
) -> FaultTolerantElastic<T> {
    FaultTolerantElastic::new(minimum_capacity, maximum_capacity, initialise_memory)
}